3

I need to call a DLL file advapi32.dll in java so that i can use its CryptEncrypt function for Encryption. Is it possible to access the functions of the dll files through JNI or is there any other better ways

advapi32.dll is found in system32 folder of windows

I tried using

System.loadLibrary("advapi32");

and

Runtime.getRuntime().loadLibrary("C:/Windows/System32/crypt32.dll");

Runtime.getRuntime().loadLibrary() giving the following errors

Exception in thread "main" java.lang.UnsatisfiedLinkError: no C:/Windows/System32/crypt32.dll in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.Runtime.loadLibrary(Unknown Source)
at mail.encrypt.main(encrypt.java:17)
Arun Michael
  • 220
  • 1
  • 12
  • What the possible ways to do X is a question that doesn't our mold here on SO. – David Heffernan Nov 27 '18 at 08:36
  • 1
    See my answer here https://stackoverflow.com/questions/52944462/java-call-function-from-a-dll/52948018#52948018 (the edit of course doesn't apply to your question). – user2543253 Nov 27 '18 at 10:00

1 Answers1

1

Specify the lib path using below command

java -Djava.library.path=C:/Windows/System32/

Or use below hacking way inside code

System.setProperty( "java.library.path", "C:/Windows/System32/" );
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); 
fieldSysPath.setAccessible( true ); 
fieldSysPath.set( null, null );
shizhen
  • 10,269
  • 9
  • 43
  • 73