-
[ 스크랩 ] 레지스트리 레퍼함수 사용..Programming 2009. 3. 17. 01:40
출처 : http://itka.kr/zbxe/debugprog/2727
레지스트리는 매번 작성하기에는 굉장히 불편하다. 기능이 많다 보니 인수도 많아지게 되지만 자주 쓰는 기능이 값을 읽고 쓰는 것에 불과 하다면 간단한 함수로 정리해 두면 계속 편리하게 사용할수 있다 shreg.cpp에 이 4개의 함수를 정의하고, shreg.h에 함수의 원형과 함께 자주 사용하는 루트키에 대한 짧은 메크로 상수를 정의하여 사용 하면 편리하다.
Shreg.cpp
#include
//정수값 읽기 UINT SHRegReadInt(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) return 0; } //문자열 읽기 BOOL SHRegReadString(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) { lstrcpy(lpRet, lpDefault); return FALSE; } RegCloseKey(key); return TRUE; } //정수값 쓰기 BOOL SHRegWriteInt(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; } //문자열 쓰기 BOOL SHRegWriteString(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, lstrlen(lpData)+1) !=ERROR_SUCCESS) return FALSE; RegCloseKey(key); return TRUE; } Shreg.h
#define SHLM HKEY_LOCAL_MACHINE #define SHCU HKEY_CURRENT_USER #define SHCR HKEY_CLASSES_ROOT UNIT SHRegReadInt(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, INT nDefault); BOOL SHRegReadString(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpDefault, LPCTSTR lpRet, DWORD nSize); BOOL SHRegWriteInt(HKEY hKey, LPCTSRT lpKey, LPCTSTR lpValue, UINT nData); BOOL SHRegWriteString(HKEY hKey, LPCTSTR lpKey, LPCTSTR lpValue, LPCTSTR lpData);
Shregtest.cpp
#include
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); HINSTANCE g_hInst; HWND hWndMain; LPCTSTR lpszClass=TEXT("ShRegTest"); int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpszCmdParam,int nCmdShow) { HWND hWnd; MSG Message; WNDCLASS WndClass; g_hInst=hInstance; WndClass.cbClsExtra=0; WndClass.cbWndExtra=0; WndClass.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); WndClass.hInstance=hInstance; WndClass.lpfnWndProc=WndProc; WndClass.lpszClassName=lpszClass; WndClass.lpszMenuName=NULL; WndClass.style=CS_HREDRAW | CS_VREDRAW; RegisterClass(&WndClass); hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,(HMENU)NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); while (GetMessage(&Message,NULL,0,0)) { TranslateMessage(&Message); DispatchMessage(&Message); } return (int)Message.wParam; } #include "shreg.h" #define KEY "SoftWare\\ITKA\\TEST\\" LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM lParam) { RECT rt; static HWND hEdit; TCHAR str[256]; switch (iMessage){ case WM_CREATE: rt.left=SHRegReadInt(SHCU,KEY"Position","Left",0); rt.top=SHRegReadInt(SHCU,KEY"Position","Top",0); rt.right=SHRegReadInt(SHCU,KEY"Position","Right",300); rt.bottom=SHRegReadInt(SHCU,KEY"Position","Bottom",200); MoveWindows(hWnd, rt.left, rt.top, rt.right-rt.left, rt.bottom-rt.top, TRUE); hEdit=CreateWindows("edit",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL, 10, 10, 200, 25, hWnd, (HMENU)100, g_hInst,NULL); SHRegReadString(SHCU,KEY"Edit","Str","문자열",str,256); SetWindowsText(hEdit,str); return 0; case WM_DESTROY: GetWindowsRect(hWnd,&rt); SHRegWriteInt(SHCU,KEY"Position","Left",rt.left); SHRegWriteInt(SHCU,KEY"Position","Top",rt.top); SHRegWriteInt(SHCU,KEY"Position","Right",rt.right); SHRegWriteInt(SHCU,KEY"Position","bottom",rt.bottom); GetWindowsText(hEdit,str,256); SHRegWriteString(SHCU,KEY"Edit","Str",str); PostQuitMessage(0); return 0; } return(DefWindowsProc(hWnd,iMessage,wParam,lParam)); } 'Programming' 카테고리의 다른 글
[펌] Dll injection Tutorial (0) 2009.03.25 [펌] Dll injection (0) 2009.03.25 [ 스크랩 ] 레지스트리 : RegCreateKeyEx (0) 2009.03.17 [트리...] 하위구조를 나타나내는 가장 기본적인 방식.. (0) 2009.03.02 Tips & Tricks: Web Server Settings for Web Application Projects now can be stored per user as well as per project (0) 2008.12.15