问题在于使用JNA lib编写REG_DWORD参数值。
我的代码是:
public static int setKeyValue(WinReg.HKEY hkey, String path, String name, String value) {
int code;
int type = getKeyType(hkey, path, name);
char[] data = value.toCharArray();
WinReg.HKEYByReference readKey = new WinReg.HKEYByReference();
code = Advapi32.INSTANCE.RegOpenKeyEx(hkey, path, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, readKey);
if (code != WinError.ERROR_SUCCESS) {
throw new Win32Exception(code);
}
if (!valueExists(hkey, path, name)) {
return -2;
}
try {
code = Advapi32.INSTANCE.RegSetValueEx(readKey.getValue(), name, 0, type, data, data.length * 2);
if (code == WinNT.ERROR_SUCCESS) return 0;
} finally {
code = Advapi32.INSTANCE.RegCloseKey(readKey.getValue());
if (code != W32Errors.ERROR_SUCCESS) throw new Win32Exception(code);
}
return -1;
}
因此,REG_SZ变量没有问题,但是当我尝试写入REG_DWORD时,regedit.exe中的msg为:无效参数DWORD(32位)
我正在使用Java 8,JNA 4.2.1 lib。任何帮助请。
答案 0 :(得分:0)
REG_DWORD的长度必须固定= 4;
switch (type) {
case WinNT.REG_DWORD :
data = Character.toChars(Integer.parseInt(value));
dataLength = 4;
break;
case WinNT.REG_SZ :
dataLength *= 2;
break;
default: break;
}