C, Win32 API: l need a help with file/data storage
-
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 @Jeron1, @DavidCrow, @Jochen Arndt 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: .....
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); }
Quote:
Now when l have the code above, the if() statements executes but lands at else if statement whi
-
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 @Jeron1, @DavidCrow, @Jochen Arndt 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: .....
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); }
Quote:
Now when l have the code above, the if() statements executes but lands at else if statement whi
Member 12139442 wrote:
My code is a little big and not straight forward for me to put in here.
Then create a small project that has a minimal amount of code that exhibits the problem, then try posting it here.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
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 @Jeron1, @DavidCrow, @Jochen Arndt 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: .....
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); }
Quote:
Now when l have the code above, the if() statements executes but lands at else if statement whi
Member 12139442 wrote:
But it always ask for key every time the app is run.
Sounds like you are not (properly) checking for the file's existance.
Member 12139442 wrote:
My code is a little big and not straight forward for me to put in here.
So then what are you asking of us?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
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 @Jeron1, @DavidCrow, @Jochen Arndt 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: .....
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); }
Quote:
Now when l have the code above, the if() statements executes but lands at else if statement whi
Open the file with
OPEN_ALWAYS
and check the error code to know if it has been created or an existing file has been opened:// Clear error
SetLastError(0);
HANDLE hFile = CreateFile(fileName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
// Read file here
}
else
{
// Ask for key and write file here
}
CloseHandle(hFile);
}Alternatively check if the file exists and open it for reading or writing afterwards. This can be optimised by trying to open the file for reading and if that fails to open it for writing:
HANDLE hFile = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// Read file here
}
else
{
hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
// Ask for key and write file here
}
}
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);