Registry를 읽고/쓰는 함수를 만들어 놓고, 재활용 할 수 있도록
dll로 만들어 보자.
registry.h
registry.cpp
사용 예
오랜만에 윈도우 프로그래밍을 하려니 버벅되네요 ^^;
Visual Studio를 실행 한 김에 예전에 만들어둔 Http Socket Client를 손대봤습니다.
테스트를 하다보니 버그가 있더군요. ㅡ.ㅡ
역시 Windows 환경에서 디버깅이 정말 쉽네요.
문제가 있는지 조차 모르던 코드를 실행만 했더니서 튀어 나오네요.
물론 디버깅 하기까지는 좀 걸렸습니다. ㅎㅎ
dll로 만들어 보자.
registry.h
#define REGISTERY_API extern "C" __declspec(dllexport)
REGISTERY_API BOOL RegReadInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue,INT nDefault);
REGISTERY_API BOOL RegReadString (HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpDefault, LPTSTR lpRet, DWORD nSize);
REGISTERY_API BOOL RegWriteInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, UINT nData);
REGISTERY_API BOOL RegWriteString( HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpData);
REGISTERY_API BOOL RegReadInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue,INT nDefault);
REGISTERY_API BOOL RegReadString (HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpDefault, LPTSTR lpRet, DWORD nSize);
REGISTERY_API BOOL RegWriteInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, UINT nData);
REGISTERY_API BOOL RegWriteString( HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpData);
registry.cpp
#include "registry.h"
#include <string.h>
#include <tchar.h>
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
//정수 읽음
REGISTERY_API BOOL RegReadInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue,INT nDefault)
{
HKEY key;
DWORD dwDisp;
UINT Result;
DWORD Size;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &key, &dwDisp)
!= ERROR_SUCCESS)
return 0;
Size = sizeof(LONG);
if (RegQueryValueEx(key, lpValue, 0, NULL, (LPBYTE) &Result, &Size)
!= ERROR_SUCCESS)
{
Result = nDefault;
}
RegCloseKey(key);
return TRUE;
}
//문자열 읽음
REGISTERY_API BOOL RegReadString(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpDefault,
LPTSTR lpRet, DWORD nSize)
{
HKEY key;
DWORD dwDisp;
DWORD Size;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &key,&dwDisp)
!= ERROR_SUCCESS)
return FALSE;
Size = nSize;
if (RegQueryValueEx(key, lpValue, 0, NULL, (LPBYTE) lpRet, &Size)
!= ERROR_SUCCESS)
{
memcpy( lpRet, lpDefault, _tcslen(lpDefault) * sizeof(TCHAR) );
// _tcscpy(lpRet, lpDefault);
return FALSE;
}
RegCloseKey(key);
return TRUE;
}
//정수값 씀
REGISTERY_API BOOL RegWriteInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, UINT nData)
{
HKEY key;
DWORD dwDisp;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key,&dwDisp)
!= ERROR_SUCCESS)
return FALSE;
if (RegSetValueEx(key, lpValue, 0, REG_DWORD, (LPBYTE) &nData, sizeof(UINT))
!= ERROR_SUCCESS)
return FALSE;
RegCloseKey(key);
return TRUE;
}
//문자열 씀
REGISTERY_API BOOL RegWriteString( HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpData)
{
HKEY key;
DWORD dwDisp;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key, &dwDisp)
!= ERROR_SUCCESS)
return FALSE;
if (RegSetValueEx(key, lpValue, 0, REG_SZ, (LPBYTE) lpData, _tcslen(lpData) * sizeof(TCHAR))
!= ERROR_SUCCESS)
return FALSE;
RegCloseKey(key);
return TRUE;
}
#include <string.h>
#include <tchar.h>
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
//정수 읽음
REGISTERY_API BOOL RegReadInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue,INT nDefault)
{
HKEY key;
DWORD dwDisp;
UINT Result;
DWORD Size;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &key, &dwDisp)
!= ERROR_SUCCESS)
return 0;
Size = sizeof(LONG);
if (RegQueryValueEx(key, lpValue, 0, NULL, (LPBYTE) &Result, &Size)
!= ERROR_SUCCESS)
{
Result = nDefault;
}
RegCloseKey(key);
return TRUE;
}
//문자열 읽음
REGISTERY_API BOOL RegReadString(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpDefault,
LPTSTR lpRet, DWORD nSize)
{
HKEY key;
DWORD dwDisp;
DWORD Size;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ, NULL, &key,&dwDisp)
!= ERROR_SUCCESS)
return FALSE;
Size = nSize;
if (RegQueryValueEx(key, lpValue, 0, NULL, (LPBYTE) lpRet, &Size)
!= ERROR_SUCCESS)
{
memcpy( lpRet, lpDefault, _tcslen(lpDefault) * sizeof(TCHAR) );
// _tcscpy(lpRet, lpDefault);
return FALSE;
}
RegCloseKey(key);
return TRUE;
}
//정수값 씀
REGISTERY_API BOOL RegWriteInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, UINT nData)
{
HKEY key;
DWORD dwDisp;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key,&dwDisp)
!= ERROR_SUCCESS)
return FALSE;
if (RegSetValueEx(key, lpValue, 0, REG_DWORD, (LPBYTE) &nData, sizeof(UINT))
!= ERROR_SUCCESS)
return FALSE;
RegCloseKey(key);
return TRUE;
}
//문자열 씀
REGISTERY_API BOOL RegWriteString( HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpData)
{
HKEY key;
DWORD dwDisp;
if(RegCreateKeyEx(hKey, lpKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &key, &dwDisp)
!= ERROR_SUCCESS)
return FALSE;
if (RegSetValueEx(key, lpValue, 0, REG_SZ, (LPBYTE) lpData, _tcslen(lpData) * sizeof(TCHAR))
!= ERROR_SUCCESS)
return FALSE;
RegCloseKey(key);
return TRUE;
}
사용 예
typedef BOOL (*FP_REGREADSTRING)(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpDefault,
LPTSTR lpRet, DWORD nSize);
const TCHAR *LPKEY = TEXT("Software\\newtype.pe.kr\\where");
const TCHAR *LPCATEGORY = TEXT("Category");
const TCHAR *LPNAME = TEXT("Name");
int main()
{
FP_REGREADSTRING RegReadString = NULL;
HMODULE hModule = LoadLibrary( TEXT("registry.dll") );
if ( hModule > 0 )
RegReadString = (FP_REGREADSTRING)GetProcAddress( hModule, "RegReadString");
if ( RegReadString == NULL )
{
int i = GetLastError();
FreeLibrary(hModule);
return 0;
}
TCHAR lpCategory[MAX_PATH]={0,};
RegReadString( HKEY_CURRENT_USER, LPKEY, LPCATEGORY, TEXT("내컴퓨터"), lpCategory, sizeof(lpCategory) );
FreeLibrary(hModule);
return 0;
}
LPTSTR lpRet, DWORD nSize);
const TCHAR *LPKEY = TEXT("Software\\newtype.pe.kr\\where");
const TCHAR *LPCATEGORY = TEXT("Category");
const TCHAR *LPNAME = TEXT("Name");
int main()
{
FP_REGREADSTRING RegReadString = NULL;
HMODULE hModule = LoadLibrary( TEXT("registry.dll") );
if ( hModule > 0 )
RegReadString = (FP_REGREADSTRING)GetProcAddress( hModule, "RegReadString");
if ( RegReadString == NULL )
{
int i = GetLastError();
FreeLibrary(hModule);
return 0;
}
TCHAR lpCategory[MAX_PATH]={0,};
RegReadString( HKEY_CURRENT_USER, LPKEY, LPCATEGORY, TEXT("내컴퓨터"), lpCategory, sizeof(lpCategory) );
FreeLibrary(hModule);
return 0;
}
오랜만에 윈도우 프로그래밍을 하려니 버벅되네요 ^^;
Visual Studio를 실행 한 김에 예전에 만들어둔 Http Socket Client를 손대봤습니다.
테스트를 하다보니 버그가 있더군요. ㅡ.ㅡ
역시 Windows 환경에서 디버깅이 정말 쉽네요.
문제가 있는지 조차 모르던 코드를 실행만 했더니서 튀어 나오네요.
물론 디버깅 하기까지는 좀 걸렸습니다. ㅎㅎ
반응형
'Dev > C, C++' 카테고리의 다른 글
socket connect timeout (2) | 2007.05.29 |
---|---|
URL Encoding/Decoding 함수 (1) | 2007.04.05 |
애플리케이션 개발시의 메모리 디버깅 : 메모리 누수 발견 기법 (0) | 2007.03.19 |
http socket client (0) | 2006.11.23 |
유용한 소켓 강좌 (0) | 2006.09.20 |