Hi all,
Good day.
Please am just learning application programming using the Win32 Api. l wrote a small program just like a crackme to save user key in a text file when key is correct and not to ask for the key next time the app is run. But should ask if the file is not found or the contents tampered. But it always ask for key every time the app is run.
l had been playing with CreateFile, ReadFile and WriteFile Functions but it doesn't work.
l had used OPEN_EXISTING as well as OPEN_ALWAYS in CreateFile function to Create the text file with user key but the app always ask for key even when the data file(.txt) exists.
l just need a guideline, proper API or sample code on how to achieve that. My code is a little big and not straight forward for me to put in here.
Thanks.
ADDENDUM/MODIFICATION
@Thomas1965, @freddie
Ok. Am confused here, and l don't know if am the one to be confused or the IDE or Microsoft themselves. Am using MS Visual studio 2010
BREAK DOWN.
I want it in a way that when the app is started, it should check if the keyfile exists. if exists, read the content and compare it with a keybuffer. if not it should ask for key.
code snippet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
WM_CREATE:
keyFile=(L"C:\\key.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
//last error here gives me 0.
if (keyFile != INVALID_HANDLE_VALUE){
if (GetLastError() == ERROR_ALREADY_EXISTS){
// Read file here
SetFilePointer(keyFile,0,0,FILE_BEGIN);
ReadFile(keyFile,szKeyRead,11,&dwBytes,NULL);
if(lstrcmp(szKeyRead,szKeyBuffer)==0){
//Nothing and continue if key is correct.
}else{
DialogBox(hInst,MAKEINTRESOURCE(IDD_REG),hWnd,Reg);//Dialog key if key is not correct
}
}else if(keyFile == INVALID_HANDLE_VALUE || keyFile==NULL|| keyFile==(HANDLE)ERROR_FILE_NOT_FOUND){
DialogBox(hInst,MAKEINTRESOURCE(IDD_REG),hWnd,Reg);//Dialog key if any of the above
// Ask for key and write file here
}else{
MessageBox(hWnd,L"Problem with Registration.\n",L"Registration",0);
}
CloseHandle(keyFile);
| |
Now when l have the code above, the if() statements executes but lands at else if statement
which l think is wrong.
but to my surprise, when l change it to the code below, no part of the if statements runs and lasterror gives me 2.
keyFile=CreateFile(L"C:\key.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
When OK is clicked, the app manipulates user entered Name, character by character and store it in a static TCHAR szKeyBuffer[12];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
if(lstrcmp(szKeyBuffer,szKey)==0){ //key is user entered strings.
keyFile=CreateFile(L"C:\key.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);//Here whenever l use \\, file does not create unless l use \. returns ALLREADY EXISTS if file exists. New if not
if(GetLastError() == ERROR_ALREADY_EXISTS){
MessageBox(hwnd,L"Sorry though!",L"Error",0);
SetFilePointer(keyFile,0,0,FILE_BEGIN);
WriteFile(keyFile,szKey,11,&dwBytes,NULL);
EndDialog(hwnd,TRUE);
}else
SetFilePointer(keyFile,0,0,FILE_BEGIN);
WriteFile(keyFile,szKey,11,&dwBytes,NULL);
EndDialog(hwnd,TRUE);
}else{
PostQuitMessage(0);
}
| |
So where is the fault from. Please l need a help/code skeleton on how to achieve my goal, which is when the app is started,it should check if keyfile(.txt file) exists and when exists, it should read the file content and check if the key is equal with the keystring the user entered, if equal app continues else it should ask for key.
Thanks.