Jnaerator and WinAPI

274 views
Skip to first unread message

Kai

unread,
Jul 27, 2010, 11:52:08 PM7/27/10
to NativeLibs4Java
Hello,

I have tried to use JNAerator on the WinReg.h of the Windows SDK to
create an access to Registry functions. somehow this doesn't work as I
expect. It looks like JNAerator is ignoring #include statements.
Trying it with WinReg.h alone it is producing very few code and
complains about undeclared types like WINAPI and such. I can progress
a little further when adding these missing defines and types manually
to the header file, but I feel like this is not the right approach.

Can someone help me to get this working? a little walkthrough? I'm not
that firm with C++, my world is Java, but of course I can handle basic
instructions involving M$ Visual Studio or such...

Cheers,
Kai

Olivier Chafik

unread,
Jul 29, 2010, 7:42:26 AM7/29/10
to us...@jna.dev.java.net, nativel...@googlegroups.com
Hello Kai,

I'm answering you on the us...@jna.dev.java.net mailing list, as it's the place where I believe most JNAerator users are :-) (let's please continue this thread over there)


Hello,

I have tried to use JNAerator on the WinReg.h of the Windows SDK to
create an access to Registry functions. somehow this doesn't work as I
expect.

You should have a look at JNA's contrib packages, which I believe already include a Windows registry API.
 
It looks like JNAerator is ignoring #include statements.
Trying it with WinReg.h alone it is producing very few code and
complains about undeclared types like WINAPI and such. I can progress
a little further when adding these missing defines and types manually
to the header file, but I feel like this is not the right approach.

Parsing system headers, whether on Windows or on Unix, is still a bit hard for JNAerator, as these headers typically exhibit the worst features of C/C++, and often the most compiler-specific ones...

So JNAerator does need a bit of help every once in a while. You can use the -v (verbose) switch to understand what's going on (outputs files with the preprocessor output, the content that was successfully parsed, the parsing errors...) and try defining macros and types. I'll write a wiki page with more info about this soon.

Can someone help me to get this working? a little walkthrough? I'm not
that firm with C++, my world is Java, but of course I can handle basic
instructions involving M$ Visual Studio or such...

I've actually tried it and found a few issues in JNAerator, which I've just fixed.

To parse WinReg.h you'll need two files : config.jnaerator and GUID.h (attached to this message).
The first contains the "command line" arguments needed by JNAerator, and the second is a hack to force the definition of the GUID struct (I'm still investigating why this hack is needed).

First, build JNAerator from sources to get my latest fixes : http://code.google.com/p/nativelibs4java/wiki/Build
Then, put the two attached files in some directory and launch JNAerator in this directory (it will pick up config.jnaerator automatically) :

java -jar libraries/jnaerator/jnaerator/target/jnaerator-0.9.5-SNAPSHOT-shaded.jar

You'll get a neat WinReg.jar file (look inside the .jnaerator file for more options).

Cheers
--
Olivier
config.jnaerator
GUID.h

Kai Hackemesser

unread,
Jul 30, 2010, 8:12:18 AM7/30/10
to nativel...@googlegroups.com
Hello, Olivier,

Thank you for caring for me :)

> To parse WinReg.h you'll need two files : config.jnaerator and GUID.h
> (attached to this message).
> The first contains the "command line" arguments needed by JNAerator,
> and the second is a hack to force the definition of the GUID struct
> (I'm still investigating why this hack is needed).

I did so, just slightly modification for the paths to the includes,
library and package name


> First, build JNAerator from sources to get my latest fixes :
> http://code.google.com/p/nativelibs4java/wiki/Build
> Then, put the two attached files in some directory and launch
> JNAerator in this directory (it will pick up config.jnaerator
> automatically) :
>
> java -jar
> libraries/jnaerator/jnaerator/target/jnaerator-0.9.5-SNAPSHOT-shaded.jar
>
> You'll get a neat WinReg.jar file (look inside the .jnaerator file for
> more options).
>

So far yes, I got the jar, and it doesn't look too bad. But the output
from JNAerator still looks suspicious that there is something not
working perfect. Do you think I should ignore this or is there something
I forgot?

Cheers,
Kai

C:\Programme\JNAerator>java -jar jnaerator-0.9.5-SNAPSHOT-shaded.jar
31.07.2010 00:02:34 com.ochafik.admin.visualstudio.VisualStudioUtils getProp
INFO: [environment] ProgramFiles=C:\Programme
31.07.2010 00:02:34 com.ochafik.admin.visualstudio.VisualStudioUtils getProp
INFO: [environment] VISUAL_STUDIO_HOME=C:\Programme\Microsoft Visual
Studio 9.0
31.07.2010 00:02:34 com.ochafik.admin.visualstudio.VisualStudioUtils getProp
INFO: [environment] ProgramFiles=C:\Programme
31.07.2010 00:02:34 com.ochafik.admin.visualstudio.VisualStudioUtils getProp
INFO: [environment] WINDOWS_SDK_HOME=C:\Programme\Microsoft
SDKs\Windows\v7.1
31.07.2010 00:02:34 com.ochafik.admin.visualstudio.VisualStudioUtils getProp
INFO: [environment] VISUAL_STUDIO_INCLUDES=C:\Programme\Microsoft Visual
Studio 9.0\VC\include;C:\Programme\Microsoft SDKs\Windows\v7.1\Include
Struct with no field : XSAVE_AREA
Struct with no field : CONTEXT_EX
Struct with no field : TOKEN_USER
Struct with no field : TOKEN_MANDATORY_LABEL
Struct with no field : TOKEN_CONTROL
Struct with no field : TOKEN_ORIGIN
Struct with no field : JOBOBJECT_BASIC_AND_IO_ACCOUNTING_INFORMATION
Struct with no field : IMAGE_ROM_HEADERS
Struct with no field : TRANSACTION_SUPERIOR_ENLISTMENT_INFORMATION
Struct with no field : FILE_ALLOCATION_INFO
Struct with no field : FILE_END_OF_FILE_INFO
Struct with no field : FILE_IO_PRIORITY_HINT_INFO
Struct with no field : MAT2
Struct with no field : POINTFX
Struct with no field : ENUMLOGFONTEXDVA
Struct with no field : ENUMLOGFONTEXDVW
Struct with no field : ENUMTEXTMETRICA
Struct with no field : ENUMTEXTMETRICW
Struct with no field : tagABORTPATH
Struct with no field : EMRSETCOLORADJUSTMENT
Struct with no field : tagEMRLINETO
Struct with no field : EMROFFSETCLIPRGN
Struct with no field : tagEMRFILLPATH
Struct with no field : tagEMREXCLUDECLIPRECT
Struct with no field : tagEMRSETVIEWPORTORGEX
Struct with no field : tagEMRSETVIEWPORTEXTEX
Struct with no field : EMRSETWORLDTRANSFORM
Struct with no field : tagEMRELLIPSE
Struct with no field : EMRROUNDRECT
Struct with no field : tagEMRARC
Struct with no field : EMRPIXELFORMAT
Struct with no field : MINMAXINFO
Struct with no field : WINDOW_BUFFER_SIZE_RECORD
Struct with no field : SERVICE_TIMECHANGE_INFO
----------
1. ERROR in file:\\\win32\advapi32\SERVICE_FAILURE_ACTIONSW.java (at line 4)
import win32.advapi32.Advapi32Library.SC_ACTION;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import win32.advapi32.Advapi32Library.SC_ACTION cannot be resolved
----------
----------
2. WARNING in file:\\\win32\advapi32\Advapi32Library.java (at line 3)
import com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper is
never used
----------
----------
3. ERROR in file:\\\win32\advapi32\SERVICE_FAILURE_ACTIONSA.java (at line 4)
import win32.advapi32.Advapi32Library.SC_ACTION;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import win32.advapi32.Advapi32Library.SC_ACTION cannot be resolved
----------
----------
4. ERROR in file:\\\win32\advapi32\UPDATELAYEREDWINDOWINFO.java (at line 3)
import win32.advapi32.Advapi32Library.BLENDFUNCTION;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import win32.advapi32.Advapi32Library.BLENDFUNCTION cannot be resolved
----------
5. ERROR in file:\\\win32\advapi32\UPDATELAYEREDWINDOWINFO.java (at line 5)
import win32.advapi32.Advapi32Library.POINT;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import win32.advapi32.Advapi32Library.POINT cannot be resolved
----------
6. ERROR in file:\\\win32\advapi32\UPDATELAYEREDWINDOWINFO.java (at line 6)
import win32.advapi32.Advapi32Library.RECT;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import win32.advapi32.Advapi32Library.RECT cannot be resolved
----------
7. ERROR in file:\\\win32\advapi32\UPDATELAYEREDWINDOWINFO.java (at line 7)
import win32.advapi32.Advapi32Library.SIZE;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The import win32.advapi32.Advapi32Library.SIZE cannot be resolved


Kai Hackemesser

unread,
Jul 31, 2010, 3:52:34 AM7/31/10
to nativel...@googlegroups.com
Hi, Olivier,


To parse WinReg.h you'll need two files : config.jnaerator and GUID.h (attached to this message).
The first contains the "command line" arguments needed by JNAerator, and the second is a hack to force the definition of the GUID struct (I'm still investigating why this hack is needed).

First, build JNAerator from sources to get my latest fixes : http://code.google.com/p/nativelibs4java/wiki/Build
Then, put the two attached files in some directory and launch JNAerator in this directory (it will pick up config.jnaerator automatically) :

java -jar libraries/jnaerator/jnaerator/target/jnaerator-0.9.5-SNAPSHOT-shaded.jar

You'll get a neat WinReg.jar file (look inside the .jnaerator file for more options).

I tried to work with the output now over the last two days. I have extended your GUID.h to remove some more conversion errors I found in the created sources. In attachment you'll see what I have added and changed in the files you sent me.

I now try to make use of the generated library As an example of the generated code here one method of the generated code:

/**
 * JNA Wrapper for library <b>advapi32</b><br>
 * This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
 * a tool written by <a href="http://ochafik.free.fr/">Olivier Chafik</a> that <a
 * href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
 * For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a
 * href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
 */
public interface Advapi32Library extends Library {
    public static final java.lang.String JNA_LIBRARY_NAME = LibraryExtractor.getLibraryPath("advapi32", true,
            Advapi32Library.class);
    public static final NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(Advapi32Library.JNA_LIBRARY_NAME,
            com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);
    public static final Advapi32Library INSTANCE = (Advapi32Library) Native.loadLibrary(
            Advapi32Library.JNA_LIBRARY_NAME, Advapi32Library.class,
            com.ochafik.lang.jnaerator.runtime.MangledFunctionMapper.DEFAULT_OPTIONS);


    /**
     * Original signature : <code>__stdcall LSTATUS RegOpenKeyExA(HKEY, LPCSTR, DWORD, REGSAM, PHKEY)</code><br>
     * <i>native declaration : C:\Programme\Microsoft SDKs\Windows\v7.1\Include\WinReg.h:640</i>
     */
    @Mangling({ "_Z13RegOpenKeyExAP6HKEY__Pc5DWORD5DWORDPP6HKEY__",
            "?RegOpenKeyExA@@YA4LONGPA6HKEY__PAD5DWORD5DWORDPAPA6HKEY__@Z" })
    NativeLong RegOpenKeyExA(HKEY hKey, ByteBuffer lpSubKey, int ulOptions, int samDesired, HKEY phkResult[]);

    /// Pointer to unknown (opaque) type
    public static class HKEY extends PointerType {
        public HKEY(Pointer address) {
            super(address);
        }
        public HKEY() {
            super();
        }
    };

}



the original signature is

LONG WINAPI RegOpenKeyEx(
  __in        HKEY hKey,
  __in_opt    LPCTSTR lpSubKey,
  __reserved  DWORD ulOptions,
  __in        REGSAM samDesired,
  __out       PHKEY phkResult
);

(see http://msdn.microsoft.com/en-us/library/ms724897%28v=VS.85%29.aspx)

I'm not exactly sure how to create the HKEY object. For the beginning I would like to create one of the predefined HKEYS, for example HKEY_CLASSES_ROOT, which has the static handle 0x80000001.

How do I initialize that properly?

In an earlier jnaeration I had a signature that accepted an int instead of a PointerType, and that was returning successful (0) on calls to the method above. With this PointerType HKEY my calls always return error code 6, invalid handle.

Cheers,
Kai

Olivier Chafik

unread,
Jul 31, 2010, 5:01:42 AM7/31/10
to nativel...@googlegroups.com
Hi Kai,

As I said before, JNA's us...@jna.dev.java.net mailing list might prove more resourceful for your JNA questions, but I'll do my best to answer you here :-)

2010/7/31 Kai Hackemesser <kai.hac...@gmail.com>
the original signature is

LONG WINAPI RegOpenKeyEx(
  __in        HKEY hKey,
  __in_opt    LPCTSTR lpSubKey,
  __reserved  DWORD ulOptions,
  __in        REGSAM samDesired,
  __out       PHKEY phkResult
);

(see http://msdn.microsoft.com/en-us/library/ms724897%28v=VS.85%29.aspx)

I'm not exactly sure how to create the HKEY object. For the beginning I would like to create one of the predefined HKEYS, for example HKEY_CLASSES_ROOT, which has the static handle 0x80000001.

How do I initialize that properly?

Have you tried new HKEY(Pointer.createConstant(0x80000001L)) ?

In an earlier jnaeration I had a signature that accepted an int instead of a PointerType, and that was returning successful (0) on calls to the method above. With this PointerType HKEY my calls always return error code 6, invalid handle.

Maybe you've used some code similar to the one above with an int constant (0x80000001) that was sign-extended to a long (createConstant takes a long), hence producing an unexpected value... ?

Anyway, an int would work only on 32 bits platforms.

Cheers
--
Olivier

Reply all
Reply to author
Forward
0 new messages