4

Found solution, leaving post, in case someone will find it useful.

Now it seems so oblivious... but when you pass the structure by its Pointer in some native function, it simply does not know that it need to reread itself from native memory.
Everything you need to do, in cases like mine, is to call structures method autoRead() after native method returns, and structure will reread itself from native memory.


Here is some example:

// Original C code
typedef struct _SomeStructure {
  int x, y;
} SomeStructure;

void DoSomesthing(BYTE* data, int param);   //returns lots of data types, 
                                            //including SomeStructure    

// Equivalent JNA mapping
class SomeStructure extends Structure { public int x, y; }
void DoSomesthing(Pointer data, int param);
...
SomeStructure st = new SomeStructure();
DoSomesthing(st.getPointer(), 100);
st.autoRead(); //structure will be filled

This trick will work only if you need to get some structure from native function.

_______________________________________________________________________________

Currently I`m working on exstension of JNA, so it could use MSCAPI, and I faced a little problem.

MSCAPI functions designed in quite specific way, so the work completely different way with different params. In particularly

BOOL WINAPI CryptGetProvParam(
__in HCRYPTPROV hProv,
__in DWORD dwParam,
__out BYTE *pbData,
__inout DWORD *pdwDataLen,
__in DWORD dwFlags
);

function returns PROV_ENUMALGS structure in pbData param, when PROV_ENUMALGS flag is set, but in other cases it could return handles, 0t-strings and lots of other stuff.
So I mapped it with:

boolean CryptGetProvParam(
HCRYPTPROV hProv,
int dwParam,
Pointer pbData,
IntByReference pdwDataLen,
int dwFlags
);

But, when Im tried to call it with PROV_ENUMALGS.getPointer() (as pbData param) structure was not filling, so I ended up with having overloaded function with pdData type PROV_ENUMALGS. Its working, but feeling, that there is some better way to do it, then having overloaded function, does not leave me for a week...

So, guys, any suggestions?

PS Sorry for my bad English, and formatting skills.

Perhaps i wasn`t very clear first time, here is some more code:
HCRYPTPROV hProv = JMSCAPIUtil.contextVerify(null, 420);

    PROV_ENUMALGS algs = new PROV_ENUMALGS();       

    if(Advapi32.INSTANCE.CryptGetProvParam(hProv,
            Advapi32.PP_ENUMALGS,
            algs,//.getPointer(),
            new IntByReference(algs.size()),
            Advapi32.CRYPT_FIRST)){
        System.out.println(algs.aiAlgid+"\t"+algs.dwBitLen+"\t"
                + new String(algs.szName,0,algs.dwNameLen.intValue()));
    }else{
        int err=Kernel32.INSTANCE.GetLastError();
        System.out.println(err);
    }       

    JMSCAPIUtil.contextRelease(hProv);  

Here is result:


26154 256 GOST 28147

Obviously, it calls

boolean CryptGetProvParam(
        HCRYPTPROV hProv,           
        int dwParam,                
        PROV_ENUMALGS pbData,           
        IntByReference pdwDataLen,  
        int dwFlags                         
        );

mapping.

If ,// deleted, it calls

boolean CryptGetProvParam(
        HCRYPTPROV hProv,           
        int dwParam,                
        Pointer pbData,                 
        IntByReference pdwDataLen,  
        int dwFlags                     
        );

mapping, and result will be:

0   0   

Apparently, structure does not fills, when passed by pointer.

3
  • When you use dwParam in PP_ENUMALGS mode, you can define pbData as byte[] array type. Define a variable with cast PROV_ENUMALGS onto preallocated memory using constructor Structure(Pointer p) i.e. PROV_ENUMALGS prov = PROV_ENUMALGS(buffer) where the buffer (a Memory or a Pointer) stores the output comes from pbData. An example on how to do this can be studied from stackoverflow.com/questions/9949798/jna-event-log-reader
    – ee.
    Apr 5, 2012 at 1:38
  • @ee. Still does not works. Found better, much simpler solution.
    – 0x002Ah
    Apr 5, 2012 at 19:55
  • 1
    JNA writes a Structure's Java fields to native memory and populates them from native memory around native calls. By default it does this automatically whenever it can recognize it's dealing with a Structure and the Structure's autoXXX property has not been turned off. As you discovered, if you use a Pointer rather than a Structure (the latter is preferred), JNA has no idea that there exists a Structure that needs to by synched.
    – technomage
    Apr 6, 2012 at 14:30

0

Browse other questions tagged or ask your own question.