trying to make I_NetLogonControl2 work with JNA

43 views
Skip to first unread message

Amritanshu

unread,
Oct 23, 2018, 8:36:46 AM10/23/18
to Java Native Access
Hello,
I am using JNA version 5.0.0 to invoke a Win32 API part of NetAPI32.dll

The function prototype for the API looks like this :

DWORD _stdcall I_NetLogonControl2( LPCWSTR ServerName, DWORD FunctionCode, DWORD QueryLevel, LPBYTE Data, LPBYTE *Buffer ); [0]

For the FunctionCode (NETLOGON_CONTROL_TC_VERIFY) I am using, Data is expected to be an address of WCHAR* string that is null terminated (the data type is LPWSTR *).

While the Instance is declared as follows in my code

static interface NetAPI extends StdCallLibrary {
            int I_NetLogonControl2( WString serverName, int functionCode, int queryLevel, PointerByReference data, PointerByReference buffer);
            NetAPI INSTANCE = (NetAPI) Native.loadLibrary("netapi32", NetAPI.class, W32APIOptions.DEFAULT_OPTIONS); 
}


The call to the function is made using the following code snippet:

     String domainName =  new String("fabrikam.com");
     PointerByReference buffer = new PointerByReference();
     Pointer ptr = new Memory((domainName.length() + 1) * Native.WCHAR_SIZE);
     ptr.setWideString(0, domainName);
     PointerByReference data = new PointerByReference();
     data.setPointer(ptr);
     int i = NetAPI.INSTANCE.I_NetLogonControl2(null, 10, 2, data, buffer);

It throws an IMA exception :

Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:425)
at com.sun.jna.Function.invoke(Function.java:360)

The exception is being thrown while accessing the second last argument - 'data'. What am I missing ? How do I go about debugging this issue.
Instead of SetWideString, I have tried to manually set the memory in ptr to the expected values directly byte-by-byte it didn't make any difference.

Any suggestions will help.

Many thanks!

Václav Haisman

unread,
Oct 23, 2018, 9:51:53 AM10/23/18
to jna-...@googlegroups.com
On Tue, 23 Oct 2018 at 14:36, Amritanshu <amrit...@gmail.com> wrote:
Hello,
I am using JNA version 5.0.0 to invoke a Win32 API part of NetAPI32.dll

The function prototype for the API looks like this :

DWORD _stdcall I_NetLogonControl2( LPCWSTR ServerName, DWORD FunctionCode, DWORD QueryLevel, LPBYTE Data, LPBYTE *Buffer ); [0]

LPBYTE is just BYTE *

 

For the FunctionCode (NETLOGON_CONTROL_TC_VERIFY) I am using, Data is expected to be an address of WCHAR* string that is null terminated (the data type is LPWSTR *).

While the Instance is declared as follows in my code

static interface NetAPI extends StdCallLibrary {
            int I_NetLogonControl2( WString serverName, int functionCode, int queryLevel, PointerByReference data, PointerByReference buffer);

`PointerByReference data` is `BYTE ** data`. You only want `Pointer data` here, I think.
 
            NetAPI INSTANCE = (NetAPI) Native.loadLibrary("netapi32", NetAPI.class, W32APIOptions.DEFAULT_OPTIONS); 
}


The call to the function is made using the following code snippet:

     String domainName =  new String("fabrikam.com");
     PointerByReference buffer = new PointerByReference();
     Pointer ptr = new Memory((domainName.length() + 1) * Native.WCHAR_SIZE);
     ptr.setWideString(0, domainName);
     PointerByReference data = new PointerByReference();
     data.setPointer(ptr);
     int i = NetAPI.INSTANCE.I_NetLogonControl2(null, 10, 2, data, buffer);

It throws an IMA exception :

Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:425)
at com.sun.jna.Function.invoke(Function.java:360)

The exception is being thrown while accessing the second last argument - 'data'. What am I missing ? How do I go about debugging this issue.
Instead of SetWideString, I have tried to manually set the memory in ptr to the expected values directly byte-by-byte it didn't make any difference.

Any suggestions will help.

Many thanks!

--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
VH

Amritanshu

unread,
Oct 23, 2018, 10:21:01 AM10/23/18
to Java Native Access
`PointerByReference data` is `BYTE ** data`. You only want `Pointer data` here, I think.

No. That's what one would think while giving it a cursory look.  

Have a look at: 


As stated before:
For the FunctionCode (NETLOGON_CONTROL_TC_VERIFY) I am using, Data is expected to be an address of WCHAR* string that is null terminated (the data type is LPWSTR *).

Here is how it is used in C/C++.

   int wmain(int argc, wchar_t **argv ) {
      ...
      LPBYTE domainName = (LPBYTE)argv[1];
      int ret = I_NetLogonControl2(NULL, NETLOGON_CONTROL_TC_VERIFY, 2, (LPBYTE)&domainName, (LPBYTE*)&buffer);
}

mbla...@doppel-helix.eu

unread,
Oct 23, 2018, 11:18:58 AM10/23/18
to Java Native Access
Václav is right, the binding is not correct. However it would most probably work, as the API expects a wchar_t**:

Carries input data that depends on the value specified in the FunctionCode parameter. The NETLOGON_CONTROL_REDISCOVER and NETLOGON_CONTROL_TC_QUERY function codes specify the trusted domain name (the data type is LPWSTR *).

The problem is in the use of Pointer#setPointer here:

String domainName = new String("fabrikam.com");
PointerByReference buffer = new PointerByReference();
Pointer ptr = new Memory((domainName.length() + 1) * Native.WCHAR_SIZE);
ptr.setWideString(0, domainName);
PointerByReference data = new PointerByReference();
data.setPointer(ptr); // This should be data.setValue(ptr);
// or directly instantiate via:
// PointerByReference data = new PointerByReference(ptr);
int i = NetAPI.INSTANCE.I_NetLogonControl2(null, 10, 2, data, buffer);


Hope that helps,

Matthias

Amritanshu

unread,
Oct 23, 2018, 11:52:52 PM10/23/18
to jna-...@googlegroups.com
Thanks Matthias. I will give this a try and update everyone. 
Reply all
Reply to author
Forward
0 new messages