Main Page   File List   Globals  

registryhelper.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2002 Ingo Richter and others.
00003  * All rights reserved.   This program and the accompanying materials
00004  * are made available under the terms of the Common Public License v1.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.eclipse.org/legal/cpl-v10.html
00007  * 
00008  * Contributors:
00009  *    Ingo Richter - Initial implementation.
00010  */
00011 #include "registryhelper.h"
00012 #include <assert.h>
00013 
00014 /*
00015  * Describe here
00016  * @param lpSubkey
00017  * @param lpClass
00018  * @param lpData
00019  */
00020 int CreateKey(LPCTSTR lpSubKey, LPTSTR lpClass, LPTSTR lpData)
00021 {
00022     HKEY keyHandle;
00023     DWORD lpdw;
00024 
00025     //In order to set the value you must specify the length of the lpData.
00026     int aLen = strlen(lpData) + 1;
00027 
00028     //This will create the key if it does not exist or update it if it does.
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         //This will write the data to the newly created key or update it.
00036         rtn = RegSetValueEx(keyHandle, lpClass, 0, REG_SZ, (LPBYTE)lpData, aLen);
00037         //Good programming practice, close what you open.
00038         rtn = RegCloseKey(keyHandle);
00039     }
00040 
00041     return rtn;
00042 }
00043 
00044 /*
00045  * Describe here
00046  * @param hKey
00047  * @param sPsth
00048  * @param lpSubKey
00049  * @param dataBuffer
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         // Something read from Registry
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 }

Generated on Sun Apr 6 13:49:03 2003 for EclipseLauncher by doxygen1.2.17