00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "registryhelper.h"
00012 #include <assert.h>
00013
00014
00015
00016
00017
00018
00019
00020 int CreateKey(LPCTSTR lpSubKey, LPTSTR lpClass, LPTSTR lpData)
00021 {
00022 HKEY keyHandle;
00023 DWORD lpdw;
00024
00025
00026 int aLen = strlen(lpData) + 1;
00027
00028
00029 long rtn = RegCreateKeyEx(HKEY_CURRENT_USER,
00030 lpSubKey, 0, "Anything",
00031 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &keyHandle, &lpdw);
00032
00033 if(rtn == ERROR_SUCCESS)
00034 {
00035
00036 rtn = RegSetValueEx(keyHandle, lpClass, 0, REG_SZ, (LPBYTE)lpData, aLen);
00037
00038 rtn = RegCloseKey(keyHandle);
00039 }
00040
00041 return rtn;
00042 }
00043
00044
00045
00046
00047
00048
00049
00050
00051 LPTSTR QueryKey(HKEY hKey, LPTSTR sPsth, LPCTSTR lpSubKey, LPTSTR databuffer, int dataBufferSize)
00052 {
00053 DWORD lpcbData = dataBufferSize;
00054 DWORD lpType;
00055
00056 assert(sPsth != NULL);
00057 assert(lpSubKey != NULL);
00058 assert(databuffer != NULL);
00059
00060 ZeroMemory(databuffer, dataBufferSize);
00061
00062 if(RegOpenKeyEx(hKey, sPsth, 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
00063 {
00064 TCHAR* buffer = (TCHAR*) malloc(dataBufferSize);
00065 int resultCode = RegQueryValueEx(hKey, lpSubKey, NULL, &lpType, (BYTE*)buffer, &lpcbData);
00066 if (resultCode == ERROR_MORE_DATA)
00067 {
00068 free(buffer);
00069 buffer = (TCHAR*) malloc(lpcbData);
00070 RegQueryValueEx(hKey, lpSubKey, NULL, &lpType, (BYTE*)buffer, &lpcbData);
00071 }
00072
00073
00074 if (lpcbData != (DWORD) dataBufferSize)
00075 {
00076 assert(lpcbData <= (DWORD) dataBufferSize);
00077 lstrcpyn(databuffer, buffer, lpcbData);
00078 }
00079
00080 free(buffer);
00081 }
00082
00083 RegCloseKey(hKey);
00084
00085 return databuffer;
00086 }