📄 advapi32.java
字号:
public static int getLastErrorCode() {
try {
return Integer.parseInt(mLastErrorCode);
} catch (Throwable e) {
e.printStackTrace();
return -1;
}
}
/**
* <pre>
* RegOpenKeyEx
*
* Opens the specified registry key. Note that key names are not case sensitive.
*
* LONG RegOpenKeyEx(
* HKEY hKey,
* LPCTSTR lpSubKey,
* DWORD ulOptions,
* REGSAM samDesired,
* PHKEY phkResult
* );
*
* Parameters
*
* hKey
* [in] A handle to an open registry key. 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_USER
* HKEY_LOCAL_MACHINE
* HKEY_USERS
*
* Windows Me/98/95: This parameter can also be the following key:
*
* HKEY_DYN_DATA
*
* lpSubKey
* [in] The name of the registry subkey to be opened.
*
* Key names are not case sensitive.
*
* If this parameter is NULL or a pointer to an empty string, the function will open a new handle to the key identified by the hKey parameter.
*
* For more information, see Registry Element Size Limits.
* ulOptions
* This parameter is reserved and must be zero.
* samDesired
* [in] A mask that specifies the desired access rights to the key. The function fails if the security descriptor of the key does not permit the requested access for the calling process. For more information, see Registry Key Security and Access Rights.
* phkResult
* [out] A pointer to a variable that receives a handle to the opened key. If the key is not one of the predefined registry keys, call the RegCloseKey function after you have finished using the handle.
*
* 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
*
* Unlike the RegCreateKeyEx function, the RegOpenKeyEx function does not create the specified key if the key does not exist in the registry.
*
* If your service or application impersonates different users, do not use this function with HKEY_CURRENT_USER. Instead, call the RegOpenCurrentUser function.
*
* A single registry key can be opened only 65534 times. When attempting the 65535th open operation, this function fails with ERROR_NO_SYSTEM_RESOURCES.
* </pre>
*
* @throws NativeException
* @throws IllegalAccessException
*/
public static HKEY RegOpenKeyEx(HKEY key, String lpSubKey, REGSAM samDesired)
throws NativeException, IllegalAccessException {
if (nRegOpenKeyEx == null) {
nRegOpenKeyEx = new JNative(DLL_NAME, "RegOpenKeyExA"); // Use ANSI
// Version
// of
// RegOpenKeyEx
nRegOpenKeyEx.setRetVal(Type.INT);
}
HKEY phkResult = new HKEY(0);
nRegOpenKeyEx.setParameter(0, key.getValue());
nRegOpenKeyEx.setParameter(1, Type.STRING, lpSubKey);
nRegOpenKeyEx.setParameter(2, 0);
nRegOpenKeyEx.setParameter(3, samDesired.getValue());
nRegOpenKeyEx.setParameter(4, phkResult.getPointer());
nRegOpenKeyEx.invoke();
mLastErrorCode = nRegOpenKeyEx.getRetVal();
if ("0".equals(mLastErrorCode)) {
return phkResult;
} else {
return null;
}
}
/**
* <pre>
* RegCloseKey
*
* Closes a handle to the specified registry key.
*
* LONG RegCloseKey(
* HKEY hKey
* );
*
* Parameters
*
* hKey
* [in] A handle to the open key to be closed. The handle must have been opened by the RegCreateKeyEx, RegOpenKeyEx, or RegConnectRegistry function.
*
* 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
*
* The handle for a specified key should not be used after it has been closed, because it will no longer be valid. Key handles should not be left open any longer than necessary.
*
* The RegCloseKey function does not necessarily write information to the registry before returning; it can take as much as several seconds for the cache to be flushed to the hard disk. If an application must explicitly write registry information to the hard disk, it can use the RegFlushKey function. RegFlushKey, however, uses many system resources and should be called only when necessary.
*
* </pre>
*
* @throws NativeException
* @throws IllegalAccessException
*/
public static boolean RegCloseKey(HKEY hKey) throws NativeException,
IllegalAccessException {
if (nRegCloseKey == null) {
nRegCloseKey = new JNative(DLL_NAME, "RegCloseKey");
nRegCloseKey.setRetVal(Type.INT);
}
nRegCloseKey.setParameter(0, hKey.getValue());
nRegCloseKey.invoke();
mLastErrorCode = nRegCloseKey.getRetVal();
return "0".equals(mLastErrorCode);
}
/**
* <pre>
* RegEnumKeyEx
*
* Enumerates the subkeys of the specified open registry key. The function retrieves information about one subkey each time it is called.
*
* LONG RegEnumKeyEx(
* HKEY hKey,
* DWORD dwIndex,
* LPTSTR lpName,
* LPDWORD lpcName,
* LPDWORD lpReserved,
* LPTSTR lpClass,
* LPDWORD lpcClass,
* PFILETIME lpftLastWriteTime
* );
*
* Parameters
*
* hKey
* [in] A handle to an open registry key. The key must have been opened with the KEY_ENUMERATE_SUB_KEYS 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 subkey to retrieve. This parameter should be zero for the first call to the RegEnumKeyEx function and then incremented for subsequent calls.
*
* Because subkeys are not ordered, any new subkey will have an arbitrary index. This means that the function may return subkeys in any order.
* lpName
* [out] A pointer to a buffer that receives the name of the subkey, including the terminating null character. The function copies only the name of the subkey, not the full key hierarchy, to the buffer.
*
* For more information, see Registry Element Size Limits.
* lpcName
* [in, out] A pointer to a variable that specifies the size of the buffer specified by the lpName parameter, in TCHARs. This size should include the terminating null character. When the function returns, the variable pointed to by lpcName 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.
* lpClass
* [in, out] A pointer to a buffer that receives the null-terminated class string of the enumerated subkey. This parameter can be NULL.
* lpcClass
* [in, out] A pointer to a variable that specifies the size of the buffer specified by the lpClass parameter, in TCHARs. The size should include the terminating null character. When the function returns, lpcClass contains the number of characters stored in the buffer. The count returned does not include the terminating null character. This parameter can be NULL only if lpClass is NULL.
* lpftLastWriteTime
* [out] A pointer to a variable that receives the time at which the enumerated subkey was last written. This parameter can be NULL.
*
* Return Values
*
* If the function succeeds, the return value is ERROR_SUCCESS.
*
* If the function fails, the return value is a system error code. If there are no more subkeys available, the function returns ERROR_NO_MORE_ITEMS.
*
* If the lpName buffer is too small to receive the name of the key, the function returns ERROR_MORE_DATA.
* Remarks
*
* To enumerate subkeys, an application should initially call the RegEnumKeyEx function with the dwIndex parameter set to zero. The application should then increment the dwIndex parameter and call RegEnumKeyEx until there are no more subkeys (meaning the function returns ERROR_NO_MORE_ITEMS).
*
* The application can also set dwIndex to the index of the last subkey on the first call to the function and decrement the index until the subkey with the index 0 is enumerated. To retrieve the index of the last subkey, use the RegQueryInfoKey function.
*
* While an application is using the RegEnumKeyEx function, it should not make calls to any registration functions that might change the key being enumerated.
*
* </pre>
*
* @param hKey
* @param dwIndex
* @param lKey
* @return
* @throws NativeException
* @throws IllegalAccessException
*/
public static RegKey RegEnumKeyEx(HKEY hKey, int dwIndex, RegKey lKey)
throws NativeException, IllegalAccessException {
if (lKey == null) {
lKey = new RegKey(255, 1024);
}
if (nRegEnumKey == null) {
nRegEnumKey = new JNative(DLL_NAME, "RegEnumKeyExA");
nRegEnumKey.setRetVal(Type.INT);
}
int cur = 0;
nRegEnumKey.setParameter(cur++, hKey.getValue());
nRegEnumKey.setParameter(cur++, dwIndex);
nRegEnumKey.setParameter(cur++, lKey.getLpValueName());
nRegEnumKey.setParameter(cur++, lKey.getLpcValueName().getPointer());
nRegEnumKey.setParameter(cur++, NullPointer.NULL);
nRegEnumKey.setParameter(cur++, lKey.getLpData());
nRegEnumKey.setParameter(cur++, lKey.getLpcbData().getPointer());
nRegEnumKey.setParameter(cur++, lKey.getLpLastWriteTime().getPointer());
nRegEnumKey.invoke();
mLastErrorCode = nRegEnumKey.getRetVal();
lKey.setErrorCode(getLastErrorCode());
return lKey;
}
/**
*
* <pre>
* RegQueryInfoKey
*
* Retrieves information about the specified registry key.
*
* LONG RegQueryInfoKey(
* HKEY hKey,
* LPTSTR lpClass,
* LPDWORD lpcClass,
* LPDWORD lpReserved,
* LPDWORD lpcSubKeys,
* LPDWORD lpcMaxSubKeyLen,
* LPDWORD lpcMaxClassLen,
* LPDWORD lpcValues,
* LPDWORD lpcMaxValueNameLen,
* LPDWORD lpcMaxValueLen,
* LPDWORD lpcbSecurityDescriptor,
* PFILETIME lpftLastWriteTime
* );
*
* 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 key:
*
* HKEY_DYN_DATA
*
* lpClass
* [out] A pointer to a buffer that receives the key class. This parameter can be NULL.
* lpcClass
* [in, out] A pointer to a variable that specifies the size of the buffer pointed to by the lpClass parameter, in characters.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -