0

I am trying to access dll procedure through java but my java method is unable to find the procedure. The dll file is loaded successfully but the the procedure from C# code named Login I can't call.

Below is the def of Procedure in ADHelper.dll:

 public static ADHelper.LoginResult Login(string UserName, string Password)
    {
      if (!ADHelper.IsUserValid(UserName, Password))
        return ADHelper.LoginResult.LOGIN_USER_DOESNT_EXIST;
      DirectoryEntry user = ADHelper.GetUser(UserName);
      if (user == null)
        return ADHelper.LoginResult.LOGIN_USER_DOESNT_EXIST;
      int userAccountControl = Convert.ToInt32(RuntimeHelpers.GetObjectValue(user.Properties["userAccountControl"][0]));
      user.Close();
      return !ADHelper.IsAccountActive(userAccountControl) ? ADHelper.LoginResult.LOGIN_USER_ACCOUNT_INACTIVE : ADHelper.LoginResult.LOGIN_OK;
    }

The dll file name is ADHelper.dll. The LoginResult is enum type:

public enum LoginResult
    {
      LOGIN_OK,
      LOGIN_USER_DOESNT_EXIST,
      LOGIN_USER_ACCOUNT_INACTIVE,
    }

Below is my java program to normally call the procedure:

package dllTest;

import com.sun.jna.*;

public class DllTester {




         public interface ADHelper extends Library {    

             public final int LOGIN_OK=1;
             public final int LOGIN_USER_DOESNT_EXIST=2;
             public final int LOGIN_USER_ACCOUNT_INACTIVE=3;


               public int Login(String user, String pass);
           }
           public static void main(String[] args) {


            ADHelper objADH = (ADHelper) Native.loadLibrary("ADHelper", ADHelper.class);
            System.out.println(objADH.getClass().getDeclaredMethods()); 
            objADH.Login("ashish", "asdas");


           }

}

Now, it is gives following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'Login': The specified procedure could not be found.

Do tell me if any more details are needed.

The solution is based upon this methodolgy:

enums/constants handling in java for dll.

NOTE: I have included the dll file in system32 for testing purpose, as for easy access too. the dll file is loading but the Login function is not calling.

The output of SOP line in java is :

[Ljava.lang.reflect.Method;@145d068
Ashish Sharma
  • 332
  • 7
  • 32

1 Answers1

1

The problem here is that your DLL is a .Net DLL, which is not a native DLL. JNA only loads and understands native DLLs, that is, DLLs built to function outside the .Net framework.

What this means is that you need a different glue between Java and .Net. I have successfully worked with Jni4net and IKVM, and there are a few others, you might want to look at those.

Lolo
  • 4,277
  • 2
  • 25
  • 24
  • Hello @Lolo I have worked on Jni4net and it's working in core java project but when I convert it to the web project it is giving exception of unsatisfiedLinkError. Can you please help me. Here is the additional information: [unsatisfiedLinkError exception while...](http://stackoverflow.com/questions/30612127/unsatisfiedlinkerror-exception-while-working-with-dll-and-java-jni4net) . – Ashish Sharma Jun 05 '15 at 06:23