Jump to content
Excelsior Forums
tkleiber

How to map windows types

Recommended Posts

Hi!

Is there any documentation how I have to map windows types to java types for using xFunction?

Especially I need examples for following types:

HKEY

DWORD

REGSAM

PHKEY

LONG

LPCVOID

LPDWORD

LPBYTE

I need to read the registry, one example is:

?public String getKey(String i_sHKey, String i_sValueName) {

? ?try {

? ? ?// UNICODE-Variante RegQueryValueExW funtioniert nicht unter XP

? ? ?xFunction oFunction = new xFunction("advapi32",

? ? ? ? ?"int RegQueryValueExA(CSTRING, CSTRING, CSTRING, CSTRING, CSTRING, int)");

? ? ?System.out.println("i_sHKey>" + i_sHKey + "<");

? ? ?System.out.println("i_sValueName>" + i_sValueName + "<");

? ? ?int intPcbData = 32000;

? ? ?Argument[] arrArgs = new Argument[6];

? ? ?arrArgs[0] = new Argument(i_sHKey, Argument.CSTRING);

? ? ?arrArgs[1] = new Argument(i_sValueName, Argument.CSTRING);

? ? ?arrArgs[2] = new Argument("", Argument.CSTRING);

? ? ?arrArgs[3] = new Argument("", Argument.CSTRING);

? ? ?arrArgs[4] = new Argument("", Argument.CSTRING, intPcbData);

? ? ?arrArgs[5] = new Argument(intPcbData);

? ? ?int intRet = ((Integer) oFunction.invoke(arrArgs)).intValue();

? ? ?String sData = (String) arrArgs[4].getValue();

? ? ?System.out.println("intRet>" + intRet + "<");

? ? ?System.out.println("sData>" + sData + "<");

? ? ?return (sData);

? ?} catch (xFunctionException e) {

? ? ?System.out.println(e.getMessage());

? ? ?return (null);

? ?}

?}

But I get:

i_sHKey>HKEY_CURRENT_USER<

i_sValueName>Software\IKB\OracleFileServer<

intRet>87<

sData><

Return value of 87 means wrong parameter.

Share this post


Link to post
Share on other sites

You are to map Windows types to usual C types first, then map those C types to Java ones. There are several ways how to map Windows types to C:

1) Refer to MSDN's page 'Windows Data Types'

2) Search in the .h-files of C library, particularly in wtypes.h

3) Create a simple .c file that accesses the required function, preprocess it and see the preprocessor's output (.i file).

Turning back to your example, almost all arguments are incorrect. Please refer to the description of the function in MSDN and learn the meaning of each argument.

The first one should be handle (int), either predefined or received from other functions. The third should be Pointer.NULL. The fourth is 'int*'. Providing a buffer (arguments 5 and 6) in such way is good only if the value that would be written is always C string. However, this buffer should be universal - any value (for example, int or double) may be written. Universal buffers are usually made from arrays of char or int, for example

Pointer p = (Pointer)Argument.create("int*", new int[4]); //16-bytes buffer;
...
char[] cArr = (char[])p.castTo("char*").createArray(16); //read as char[]
Double d = (Double)p.castTo("double*).deref(); //read as double

.

Share this post


Link to post
Share on other sites

I have changed third argument to int in the signature and use Pointer.NULL:

//      arrArgs[2] = new Argument("", Argument.CSTRING);

     arrArgs[2] = Pointer.NULL;

I get:

The argument number '2' is not compatible with the one specified in signature.

Could it be that an int can not be null?

Share this post


Link to post
Share on other sites

How do I mix Pointer and Argument in my Array? I have 6 parameter for the function, so I think I have to use an array. If I create a Pointer array, how I have to define second parameter? Following code:

     Pointer[] arrPtr = new Pointer[6];

     arrPtr[0] = Pointer.createPointerTo(i_intKey);

     arrPtr[1] = (Pointer) new Argument(i_sValueName, Argument.CSTRING);

...

compiles fine, but gives runtime error java.lang.ClassCastExcepiton in the line for arrPtr[1].

Share this post


Link to post
Share on other sites

Pointer is a subclass of Argument, it is just special kind of argument with an additional functionality. So, Argument[] array may contain both pointers and simple arguments.

Share this post


Link to post
Share on other sites

Ok - have defined an array of Argument:

     Argument[] arrArgs = new Argument[6];

...

     arrArgs[3] = (Pointer)Argument.create("int*", new int[4]);

...

How I can cast this later back to a String? Class Argument does not contain deref or cast method! Thank you!

Share this post


Link to post
Share on other sites

It seems you are confused with the difference of actual and formal types. A Pointer stored into Argument[] array does not cease from being Pointer!

In your example, the code shall be something like:

Argument[] arrArgs = new Argument[6]; 
... 
Pointer p3 = (Pointer)Argument.create("int*", new int[4]);
arrArgs[3] = p3;
...
char[] cArr =  (char[])p3.castTo("char*).createArray(16);
String str = new String(cArr);

.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×