3 messages in net.java.dev.jna.usersWindows Registry Handles/Pointers
FromSent OnAttachments
Dale...@coats.comMay 22, 2008 4:55 am 
Timothy WallMay 22, 2008 5:44 am 
Dale...@coats.comMay 22, 2008 6:37 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:Windows Registry Handles/PointersActions...
From:Dale...@coats.com (Dale@coats.com)
Date:May 22, 2008 4:55:51 am
List:net.java.dev.jna.users

I found some C++ code that I want to map to Java in JNA, but I get conflicting signals for mapping from the code and the MSDN api. I've tried many variations, but have not hit on it yet.

There are two calls: RegOpenKeyEx, which is supposed to populate "hKey". RegQueryValueEx, which uses the "hKey" from the previous call.

I can get a "good" return code from the first call, but when the hKey is used in the second call, I have seen the following: rc.intValue() == 2 // errorMessage = "ERROR_FILE_NOT_FOUND"; rc.intValue() == 6 // errorMessage = "ERROR_INVALID_HANDLE"; rc.intValue() == 234 // errorMessage = "ERROR_MORE_DATA";

Right now I'm stuck on "6", but I've been on "234".

So yeah, it seems like the JNA mapping recommendations and the C++ code give different ideas about how to map. All of one or all of the other didn't work for me, and bits of both got me farther, but not out of the woods.

This one would be pretty helpful to all, I think (reading the registry). So hopefully I'll get a nudge in the right direction, or better.

--Dale--

//////////CPP///////////////////

int GetProductType() { HKEY hKey;

LPCTSTR szKey = _T("ProductType"); DWORD dwType = 0; TCHAR szProductType[32]; DWORD dwSize = 32;

int iRes = -1; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {

if(RegQueryValueEx(hKey, szKey, NULL, &dwType, (LPBYTE)szProductType, &dwSize) == ERROR_SUCCESS) { if (dwType == REG_SZ) { if (lstrcmpi(_T("WINNT"), szProductType) == 0) iRes = ENV_WINWRKSTN; } } RegCloseKey(hKey); } return iRes; }

////////////Java//////////////////

public static int GetProductType(){ Advapi32 advapi32 = Advapi32.INSTANCE;

//int hKey = 0; // Gives "ERROR_INVALID_PARAMETER" PointerByReference hKey = new PointerByReference(); String lpValueName = "ProductType"; int lpType = 0; char[] lpData = new char[32]; //int lpcbData = 32; Gives EXCEPTION_ACCESS_VIOLATION PointerByReference lpcbData = new PointerByReference();

int iRes = -1; int regOpenRC = advapi32.RegOpenKeyExW( Advapi32.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, Advapi32.KEY_QUERY_VALUE, hKey) ; System.out.println("hKey [" + hKey + "]"); if(regOpenRC == Advapi32.ERROR_SUCCESS) { int regQueryRC = advapi32.RegQueryValueExW( hKey, lpValueName, null, lpType, lpData, lpcbData); if(regQueryRC == Advapi32.ERROR_SUCCESS) { System.out.println("lpType [" + lpType + "]"); System.out.println("lpData [" + new String(lpData) + "]"); } else { System.out.println("regQueryRC [" + regQueryRC + "]"); } advapi32.RegCloseKey(hKey); } else { System.out.println("regOpenRC [" + regOpenRC + "]"); } return iRes; }

////////////MAPPING/////////////////////

Advapi32 INSTANCE = (Advapi32) Native.loadLibrary("Advapi32", Advapi32.class, DEFAULT_OPTIONS);

/** * <pre> * LONG WINAPI RegOpenKeyExW( * __in HKEY hKey, it can be one of the following predefined keys:HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS * __in_opt LPCTSTR lpSubKey, The name of the registry subkey to be opened. * __reserved DWORD ulOptions, This parameter is reserved and must be zero. * __in REGSAM samDesired, A mask that specifies the desired access rights to the key. * __out PHKEY phkResult, A pointer to a variable that receives a handle to the opened key. *); *If the function fails, the return value is a nonzero error code *</pre> */ int RegOpenKeyExW(int hKey, String lpSubKey, int ulOptions, int samDesired, PointerByReference phkResult);

/** * <pre> * LONG WINAPI RegQueryValueExW( * __in HKEY hKey, A handle to an open registry key. * __in_opt LPCTSTR lpValueName, The name of the registry value. * __reserved LPDWORD lpReserved, This parameter is reserved and must be NULL. * __out_opt LPDWORD lpType, A pointer to a variable that receives a code indicating the type of data stored in the specified value. * __out_opt LPBYTE lpData, A pointer to a buffer that receives the value's data. * __inout_opt LPDWORD lpcbData, A pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. *); *If the function succeeds, the return value is ERROR_SUCCESS. *</pre> */ int RegQueryValueExW(PointerByReference hKey, String lpValue, Pointer lpReserved, int lpType, char[] lpData, PointerByReference lpcbData);

/** * <pre> * LONG WINAPI RegCloseKey( * __in HKEY hKey, A handle to the open key to be closed. *); *</pre> */ NativeLong RegCloseKey(PointerByReference hKey);