📄 regconnect.c
字号:
#include <windows.h>
#include <string.h>
#include "jni.h"
#include ".\java\GetKey.h" //JNI implementation of RegDBGetKeyValueEx
#include ".\java\SetKey.h" //JNI implementation of RegDBSetKeyValueEx
// Global Variable to store the DLL's Instance handle
HINSTANCE ghDLLInst ;
char *rgchModuleName = "RegConnect"; //Name of module
// Function prototypes
int WINAPI RegDBGetKeyValueEx(LPCTSTR, HKEY ,LPCTSTR, LPCTSTR, LPTSTR) ;
int WINAPI RegDBSetKeyValueEx(LPCTSTR, HKEY ,LPCTSTR, LPCTSTR, LPCTSTR) ;
// -----------------------------------------------------------------
// Function: LibMain
//
// Purpose : This is the DLL's entry point. It is analogous to WinMain for applications.
//
// Params : hInstance == The handle to the DLL's instance.
// wDataSeg == Basically it is a pointer to the DLL's data segment.
// wHeapSize == Size of the DLL's heap in bytes.
// lpszCmdLine == The command line passed to the DLL by Windows. This is rarely used.
//
// Returns : 1 indicating DLL initialization is successful.
//
// Comments: LibMain is called by Windows. Do not call it in your application!
// -----------------------------------------------------------------
//int FAR PASCAL LibMain (HANDLE hInstance, WORD wDataSeg, WORD wHeapSize, LPSTR lpszCmdLine)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved) // reserved
{
ghDLLInst = hinstDLL ;
// Perform actions based on the reason for calling.
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
break;
case DLL_THREAD_DETACH:
// Do thread-specific cleanup.
break;
case DLL_PROCESS_DETACH:
// Perform any necessary cleanup.
break;
}
return TRUE; // Successful DLL_PROCESS_ATTACH.
}
//////////////////////////////////////////////////////////////////////////
// Function: RegDBGetKeyValueEx
//
// Description: Retrieves the string associated with the specified key in a remote registry.
//
// Parameters:
// lpMachineName - Name of machine we want to interrogate
// hRegKey - Registry key type, i.e. HKEY_LOCAL_MACHINE
// lpKeyName - Tree to Key we want to get i.e. software\ibm\db2
// lpValueName - Value we want to get i.e. DB2 path name
// lpszKeyValue - Value we will get back - may need pre-dimensioning
// Returns:
// non zero error code on failure, 0 on success
//////////////////////////////////////////////////////////////////////////
int WINAPI RegDBGetKeyValueEx(LPCTSTR lpMachineName, HKEY hRegKey, LPCTSTR lpKeyName, LPCTSTR lpValueName, LPTSTR lpszKeyValue)
{
// Local variables
HKEY hRemoteKey = NULL ;
HKEY hKey = NULL ;
DWORD dwKeySize; // Size of key value
DWORD dwKeyDataType; // Type of data stored in key
LONG lRC; // Return code
char cMachine[128] = {0} ;
// Initialize variables
dwKeyDataType = 0;
dwKeySize = 0;
if(lpMachineName)
if(lpMachineName[0] != 0)
wsprintf(cMachine,"\\\\%s",lpMachineName) ;
// Connect to the remote registry
lRC = RegConnectRegistry(cMachine, hRegKey, &hRemoteKey) ;
if (lRC != ERROR_SUCCESS)
return lRC ;
//Open the key
lRC = RegOpenKeyEx(hRemoteKey, lpKeyName, 0, KEY_ALL_ACCESS, &hKey);
if (lRC == ERROR_SUCCESS)
{
// Got key, get value. First, get the size of the key.
lRC = RegQueryValueEx(hKey, lpValueName, NULL, &dwKeyDataType, NULL, &dwKeySize);
if(dwKeyDataType == REG_DWORD) //DWORD
{
char cData[128] = {0} ;
lRC = RegQueryValueEx(hKey, lpValueName, NULL, &dwKeyDataType, (LPBYTE)cData, &dwKeySize);
ltoa((LONG)cData[0], lpszKeyValue, 10) ;
}
else
// Now get the actual key value
lRC = RegQueryValueEx(hKey, lpValueName, NULL, &dwKeyDataType, (LPBYTE)lpszKeyValue, &dwKeySize);
}
RegCloseKey(hKey) ;
RegCloseKey(hRemoteKey) ;
return lRC ;
} // End of function RegDBGetKeyValueEx
//////////////////////////////////////////////////////////////////////////
// Function: RegDBSetKeyValueEx
//
// Description: Sets the string associated with the specified key in a remote registry.
//
// Parameters:
// lpMachineName - Name of machine we want to interrogate
// hRegKey - Registry key type, i.e. HKEY_LOCAL_MACHINE
// lpKeyName - Tree to Key we want to set i.e. software\ibm\db2
// lpValueName - Value key we want to set i.e. DB2 path name
// lpszKeyValue - Value we will set in the registry
// Returns:
// non zero error code on failure, 0 on success
//////////////////////////////////////////////////////////////////////////
int WINAPI RegDBSetKeyValueEx(LPCTSTR lpMachineName, HKEY hRegKey, LPCTSTR lpKeyName, LPCTSTR lpValueName, LPCTSTR lpszKeyValue)
{
// Local variables
HKEY hRemoteKey = NULL ;
HKEY hKey = NULL ;
LONG lRC; // Return code
char cMachine[128] = {0} ;
DWORD dwDisp;
if(lpMachineName)
if(lpMachineName[0] != 0)
wsprintf(cMachine,"\\\\%s",lpMachineName) ;
// Connect to the remote registry
lRC = RegConnectRegistry(cMachine, hRegKey, &hRemoteKey) ;
if (lRC != ERROR_SUCCESS)
return lRC ;
// Initialize variables
lRC = RegCreateKeyEx(hRemoteKey, lpKeyName, 0, REG_NONE,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp) ;
if (lRC == ERROR_SUCCESS)
{
// Got key, set value.
lRC = RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (LPBYTE)lpszKeyValue, strlen(lpszKeyValue) + 1) ;
RegCloseKey(hKey) ;
}
RegCloseKey(hRemoteKey) ;
return lRC ;
} // End of function RegDBGetKeyValueEx
// Java implementation of RegDBGetKeyValueEx
JNIEXPORT jobject JNICALL Java_GetKey_RegDBGetKeyValueEx(JNIEnv* env, jobject this_obj,
jstring jMachineName, jint hRegKey, jstring jKeyName, jstring jValueName)
{
LPCTSTR lpMachineName ;
LPCTSTR lpKeyName ;
LPCTSTR lpValueName ;
char cKeyValue[255] ;
jobject ret ;
lpMachineName = (*env)->GetStringUTFChars(env, jMachineName, NULL) ;
lpKeyName = (*env)->GetStringUTFChars(env, jKeyName, NULL) ;
lpValueName = (*env)->GetStringUTFChars(env, jValueName, NULL) ;
RegDBGetKeyValueEx(lpMachineName, (HKEY)hRegKey, lpKeyName, lpValueName, cKeyValue) ;
(*env)->ReleaseStringUTFChars(env, jMachineName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jKeyName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jValueName, NULL) ;
ret = (*env)->NewStringUTF(env, cKeyValue) ;
return ret ;
} // End of function Java_GetKey_RegDBGetKeyValueEx
// Java implementation of RegDBSetKeyValueEx
JNIEXPORT jint JNICALL Java_SetKey_RegDBSetKeyValueEx(JNIEnv* env, jobject this_obj,
jstring jMachineName, jint hRegKey, jstring jKeyName, jstring jValueName, jstring jKeyValue)
{
LPCTSTR lpMachineName ;
LPCTSTR lpKeyName ;
LPCTSTR lpValueName ;
LPCTSTR lpszKeyValue ;
int iRet ;
lpMachineName = (*env)->GetStringUTFChars(env, jMachineName, NULL) ;
lpKeyName = (*env)->GetStringUTFChars(env, jKeyName, NULL) ;
lpValueName = (*env)->GetStringUTFChars(env, jValueName, NULL) ;
lpszKeyValue = (*env)->GetStringUTFChars(env, jKeyValue, NULL) ;
iRet = RegDBSetKeyValueEx(lpMachineName, (HKEY)hRegKey, lpKeyName, lpValueName, lpszKeyValue) ;
(*env)->ReleaseStringUTFChars(env, jMachineName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jKeyName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jValueName, NULL) ;
(*env)->ReleaseStringUTFChars(env, jKeyValue, NULL) ;
return iRet ;
} // End of function Java_SetKey_RegDBGetKeyValueEx
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -