📄 advapi32.java
字号:
* 0x00000002 Restrict type to REG_SZ.
*
* This parameter can also include one or more of the following values.
* Value Meaning
* RRF_NOEXPAND
* 0x10000000 Do not automatically expand environment strings if the value is of type REG_EXPAND_SZ.
* RRF_ZEROONFAILURE
* 0x20000000 If pvData is not NULL, set the contents of the buffer to zeroes on failure.
* pdwType
* [out] A pointer to a variable that receives a code indicating the type of data stored in the specified value. For a list of the possible type codes, see Registry Value Types. This parameter can be NULL if the type is not required.
* pvData
* [out] A pointer to a buffer that receives the value's data. This parameter can be NULL if the data is not required.
*
* If the data is a string, the function checks for a terminating null character. If one is not found, the string is stored with a null terminator if the buffer is large enough to accommodate the extra character. Otherwise, the function fails and returns ERROR_MORE_DATA.
* pcbData
* [in, out] A pointer to a variable that specifies the size of the buffer pointed to by the pvData parameter, in bytes. When the function returns, this variable contains the size of the data copied to pvData.
*
* The pcbData parameter can be NULL only if pvData is NULL.
*
* If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, this size includes any terminating null character or characters. For more information, see Remarks.
*
* If the buffer specified by pvData parameter is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by pcbData. In this case, the contents of the pvData buffer are undefined.
*
* If pvData is NULL, and pcbData is non-NULL, the function returns ERROR_SUCCESS and stores the size of the data, in bytes, in the variable pointed to by pcbData. This enables an application to determine the best way to allocate a buffer for the value's data.
*
* If hKey specifies HKEY_PERFORMANCE_DATA and the pvData buffer is not large enough to contain all of the returned data, the function returns ERROR_MORE_DATA and the value returned through the pcbData parameter is undefined. This is because the size of the performance data can change from one call to the next. In this case, you must increase the buffer size and call RegGetValue again passing the updated buffer size in the pcbData parameter. Repeat this until the function succeeds. You need to maintain a separate variable to keep track of the buffer size, because the value returned by pcbData is unpredictable.
*
* Return Values
*
* If the function succeeds, the return value is ERROR_SUCCESS.
*
* If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
* Remarks
*
* An application typically calls RegEnumValue to determine the value names and then RegGetValue to retrieve the data for the names.
*
* If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper null-terminating characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it; otherwise, it may overwrite a buffer. (Note that REG_MULTI_SZ strings should have two null-terminating characters.)
*
* If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, and the ANSI version of this function is used (either by explicitly calling RegGetValueA or by not defining UNICODE before including the Windows.h file), this function converts the stored Unicode string to an ANSI string before copying it to the buffer pointed to by pvData.
*
* When calling this function with hkey set to the HKEY_PERFORMANCE_DATA handle and a value string of a specified object, the returned data structure sometimes has unrequested objects. Do not be surprised; this is normal behavior. You should always expect to walk the returned data structure to look for the requested object.
*
* To compile an application that uses this function, define WIN32_WINNT as 0x0600 or later. For more information, see Using the SDK Headers.
*
* </pre>
*
* @throws NativeException
* @throws IllegalAccessException
*/
public static RegData RegGetValue(HKEY hKey, String lpSubKey,
String lpValue, int dwFlags, RegData lRegData)
throws NativeException, IllegalAccessException {
if (lRegData == null) {
lRegData = new RegData(1024);
}
if (nRegGetValue == null) {
nRegGetValue = new JNative(DLL_NAME, "RegGetValueA");
nRegGetValue.setRetVal(Type.INT);
}
int cur = 0;
nRegGetValue.setParameter(cur++, hKey.getValue());
if (lpSubKey == null) {
nRegGetValue.setParameter(cur++, NullPointer.NULL);
} else {
nRegGetValue.setParameter(cur++, Type.STRING, lpSubKey);
}
if (lpValue == null) {
nRegGetValue.setParameter(cur++, NullPointer.NULL);
} else {
nRegGetValue.setParameter(cur++, Type.STRING, lpValue);
}
nRegGetValue.setParameter(cur++, dwFlags);
nRegGetValue.setParameter(cur++, lRegData.getLpType().getPointer());
nRegGetValue.setParameter(cur++, lRegData.getLpData());
nRegGetValue.setParameter(cur++, lRegData.getLpcbData().getPointer());
nRegGetValue.invoke();
mLastErrorCode = nRegGetValue.getRetVal();
lRegData.setErrorCode(getLastErrorCode());
return lRegData;
}
/**
*
* <b>RegEnumValue</b>
*
* <pre>
*
* Enumerates the values for the specified open registry key. The function copies one indexed value name and data block for the key each time it is called.
*
* LONG RegEnumValue(
* HKEY hKey,
* DWORD dwIndex,
* LPTSTR lpValueName,
* LPDWORD lpcValueName,
* LPDWORD lpReserved,
* LPDWORD lpType,
* LPBYTE lpData,
* LPDWORD lpcbData
* );
*
* Parameters
*
* hKey
* [in] A handle to an open registry key. The key must have been opened with the KEY_QUERY_VALUE access right. For more information, see Registry Key Security and Access Rights.
*
* This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys:
*
* HKEY_CLASSES_ROOT
* HKEY_CURRENT_CONFIG
* HKEY_CURRENT_USER
* HKEY_LOCAL_MACHINE
* HKEY_PERFORMANCE_DATA
* HKEY_USERS
*
* Windows Me/98/95: This parameter can also be the following value:
*
* HKEY_DYN_DATA
*
* dwIndex
* [in] The index of the value to be retrieved. This parameter should be zero for the first call to the RegEnumValue function and then be incremented for subsequent calls.
*
* Because values are not ordered, any new value will have an arbitrary index. This means that the function may return values in any order.
* lpValueName
* [out] A pointer to a buffer that receives the name of the value, including the terminating null character.
*
* For more information, see Registry Element Size Limits.
* lpcValueName
* [in, out] A pointer to a variable that specifies the size of the buffer pointed to by the lpValueName parameter, in TCHARs. This size should include the terminating null character. When the function returns, the variable pointed to by lpcValueName contains the number of characters stored in the buffer. The count returned does not include the terminating null character.
* lpReserved
* This parameter is reserved and must be NULL.
* lpType
* [out] A pointer to a variable that receives a code indicating the type of data stored in the specified value. For a list of the possible type codes, see Registry Value Types. The lpType parameter can be NULL if the type code is not required.
* lpData
* [out] A pointer to a buffer that receives the data for the value entry. This parameter can be NULL if the data is not required.
*
* If lpData is NULL and lpcbData is non-NULL, the function stores the size of the data, in bytes, in the variable pointed to by lpcbData. This enables an application to determine the best way to allocate a buffer for the data.
* lpcbData
* [in, out] A pointer to a variable that specifies the size of the buffer pointed to by the lpData parameter, in bytes. When the function returns, the variable pointed to by the lpcbData parameter contains the number of bytes stored in the buffer.
*
* This parameter can be NULL only if lpData is NULL.
*
* If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, this size includes any terminating null character or characters. For more information, see Remarks.
*
* If the buffer specified by lpData is not large enough to hold the data, the function returns ERROR_MORE_DATA and stores the required buffer size in the variable pointed to by lpcbData. In this case, the contents of lpData are undefined.
*
* Registry value names are limited to 32767 bytes. The ANSI version of this function treats this param as a USHORT value. Therefore, if you specify a value greater than 32767 bytes, there is an overflow and the function may return ERROR_MORE_DATA.
*
* Return Values
*
* If the function succeeds, the return value is ERROR_SUCCESS.
*
* If the function fails, the return value is a nonzero error code defined in Winerror.h. You can use the FormatMessage function with the FORMAT_MESSAGE_FROM_SYSTEM flag to get a generic description of the error.
* Remarks
*
* To enumerate values, an application should initially call the RegEnumValue function with the dwIndex parameter set to zero. The application should then increment dwIndex and call the RegEnumValue function until there are no more values (until the function returns ERROR_NO_MORE_ITEMS).
*
* The application can also set dwIndex to the index of the last value on the first call to the function and decrement the index until the value with index 0 is enumerated. To retrieve the index of the last value, use the RegQueryInfoKey function.
*
* While using RegEnumValue, an application should not call any registry functions that might change the key being queried.
*
* If the data has the REG_SZ, REG_MULTI_SZ or REG_EXPAND_SZ type, the string may not have been stored with the proper null-terminating characters. Therefore, even if the function returns ERROR_SUCCESS, the application should ensure that the string is properly terminated before using it; otherwise, it may overwrite a buffer. (Note that REG_MULTI_SZ strings should have two null-terminating characters.)
*
* To determine the maximum size of the name and data buffers, use the RegQueryInfoKey function.
*
* </pre>
*
* @param hKey
* @param dwIndex
* @param lRegValue
* @return
* @throws NativeException
* @throws IllegalAccessException
*/
public static RegValue RegEnumValue(HKEY hKey, int dwIndex,
RegValue lRegValue) throws NativeException, IllegalAccessException {
if (lRegValue == null) {
lRegValue = new RegValue(512, 1024);
}
if (nRegEnumValue == null) {
nRegEnumValue = new JNative(DLL_NAME, "RegEnumValueA");
nRegEnumValue.setRetVal(Type.INT);
}
nRegEnumValue.setParameter(0, hKey.getValue());
nRegEnumValue.setParameter(1, dwIndex);
nRegEnumValue.setParameter(2, lRegValue.getLpValueName());
nRegEnumValue.setParameter(3, lRegValue.getLpcValueName().getPointer());
nRegEnumValue.setParameter(4, NullPointer.NULL);
nRegEnumValue.setParameter(5, lRegValue.getLpType().getPointer());
nRegEnumValue.setParameter(6, lRegValue.getLpData());
nRegEnumValue.setParameter(7, lRegValue.getLpcbData().getPointer());
nRegEnumValue.invoke();
mLastErrorCode = nRegEnumValue.getRetVal();
lRegValue.setErrorCode(getLastErrorCode());
return lRegValue;
}
public static void main(String[] args) throws NativeException,
IllegalAccessException {
System.err.println("Testing Registry access");
HKEY hKey = RegOpenKeyEx(HKEY.HKEY_CLASSES_ROOT, "CLSID",
REGSAM.KEY_READ.or(REGSAM.KEY_QUERY_VALUE));
if (hKey == null) {
System.err.println("error : " + getLastErrorCode());
return;
} else {
System.err.println("hKey : " + hKey.getValue());
}
listKey(hKey);
RegCloseKey(hKey);
}
static int cpt = 0;
/**
* @param hKey
* @throws NativeException
* @throws IllegalAccessException
*/
public static void listKey(HKEY hKey) throws NativeException,
IllegalAccessException {
if (cpt > 50) {
return;
} else {
//cpt++;
}
RegQueryKey lKey = RegQueryInfoKey(hKey, null);
// RegKey lRegData;
// System.err.println("Number of values : " +
// lKey.getLpcValues().getValue());
for (int i = 0; i < lKey.getLpcValues().getValue(); i++) {
RegValue regValue = RegEnumValue(hKey, i, null);
RegData lRegData = null;
String keyValueName = regValue.getLpValueName().getAsString();
if (keyValueName == null || keyValueName.length() == 0) {
System.err.println("Query value : (default)");
} else {
System.err.println("Query value : " + keyValueName);
}
lRegData = RegQueryValueEx(hKey, keyValueName, null);
String val = "";
RegValueTypes valType = RegValueTypes.fromInt(lRegData.getLpType().getValue());
switch (valType) {
case REG_DWORD:
val = lRegData.getLpData().getAsInt(0) + " - (0x" + Integer.toHexString(lRegData.getLpData().getAsInt(0))+")";
break;
case REG_DWORD_LITTLE_ENDIAN:
val = lRegData.getLpData().getAsShort(0) + 256*lRegData.getLpData().getAsShort(2) + "";
break;
case REG_BINARY:
byte [] p = lRegData.getLpData().getMemory();
for(int j = 0; j < lRegData.getLpcbData().getValue(); j++) {
val += ("0x" + Integer.toHexString(p[j]) + ", ");
}
val = val.substring(0, val.length()-2);
break;
default:
val = lRegData.getLpData().getAsString();
break;
}
System.err.println("Type : " + valType);
System.err.println("Value : " + val);
}
for (int i = 0; i < lKey.getLpcSubKeys().getValue(); i++) {
RegKey regKey = RegEnumKeyEx(hKey, i, null);
if (regKey.getErrorCode() != 0 || cpt > 50) {
break;
} else {
System.err.println("Key : "
+ regKey.getLpValueName().getAsString());
HKEY lhKey = RegOpenKeyEx(hKey, regKey.getLpValueName()
.getAsString(), REGSAM.KEY_READ
.or(REGSAM.KEY_QUERY_VALUE));
listKey(lhKey);
RegCloseKey(lhKey);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -