3 messages in net.java.dev.jna.usersRe: [jna-users] Buffer to Structure
FromSent OnAttachments
Thomas BörkelMar 19, 2008 5:06 am 
Timothy WallMar 19, 2008 5:33 am 
Thomas BörkelMar 19, 2008 8:42 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Buffer to StructureActions...
From:Timothy Wall (twal@dev.java.net)
Date:Mar 19, 2008 5:33:02 am
List:net.java.dev.jna.users

See comments below.

On Mar 19, 2008, at 8:07 AM, Thomas Börkel wrote:

HI!

I am trying to call a function, that takes a buffer as parameter. This buffer could be anything, but is today a specific struct.

The function can be called with buffer length 0 to determine the needed buffer size. Then you call it again with the correct allocated buffer.

There are plenty of Win32 function that work like this.

But how do I later convert the buffer to the Structure? I only know a way to convert a Pointer to a Structure (with useMemory()).

This is the function: BOOL WINAPI QueryServiceStatusEx( __in SC_HANDLE hService, __in SC_STATUS_TYPE InfoLevel, __out_opt LPBYTE lpBuffer, __in DWORD cbBufSize, __out LPDWORD pcbBytesNeeded );

When I declare lpBuffer as type of the Structure and call the function first with cbBufSize = 0 and then with cbBufSize = pcbBytesNeeded, it works. But that's only for the current implementation.

For this, I have declared the function like this:

public boolean QueryServiceStatusEx(Pointer hService, int InfoLevel, SERVICE_STATUS_PROCESS lpBuffer, int cbBufSize, IntByReference pcbBytesNeeded);

But I'd like it more like this, to make it more future proof:

Not sure what you mean by future-proof. I would think declaring a specific structure preferable to a generic buffer. If the parameter accepts different types you can always declare a different method for each acceptable type.

public boolean QueryServiceStatusEx(Pointer hService, int InfoLevel, byte[] lpBuffer, int cbBufSize, IntByReference pcbBytesNeeded);

But then how to convert lpBuffer to the Structure?

The following should do the trick (or something similar; I'm not looking at the javadoc right now):

MyStructure s ...; s.getPointer().write(0, lpBuffer, 0, lpBuffer.length); s.read();

You could also use a direct byte buffer, then call Structure.useMemory() with Native.getDirectBufferPointer().