Click here to Skip to main content
15,887,676 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Waitting on hEvent of Mailslot Pin
Richard Andrew x645-Apr-16 13:27
professionalRichard Andrew x645-Apr-16 13:27 
GeneralRe: Waitting on hEvent of Mailslot Pin
ForNow5-Apr-16 13:32
ForNow5-Apr-16 13:32 
GeneralRe: Waitting on hEvent of Mailslot Pin
leon de boer5-Apr-16 16:33
leon de boer5-Apr-16 16:33 
QuestionHow to custmize the ribbon category at run time using MFC Pin
Ashish Ranjan Mishra4-Apr-16 6:36
Ashish Ranjan Mishra4-Apr-16 6:36 
SuggestionRe: How to custmize the ribbon category at run time using MFC Pin
David Crow5-Apr-16 15:50
David Crow5-Apr-16 15:50 
Questioninput to be parsed to produce a node tree Pin
Member 124358024-Apr-16 5:45
Member 124358024-Apr-16 5:45 
SuggestionRe: input to be parsed to produce a node tree Pin
Richard MacCutchan4-Apr-16 7:03
mveRichard MacCutchan4-Apr-16 7:03 
QuestionC, Win32 API: l need a help with file/data storage Pin
Member 121394424-Apr-16 4:32
Member 121394424-Apr-16 4:32 
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:
.....
C++
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

<pre>
                }
            }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);
                        }</pre>

Quote:
Now when l have the code above, the if() statements executes but lands at else if statement
which l think is wrong.

Quote:
but to my surprise, when l change it to the code below, no part of the if statements runs and lasterror gives me 2.

C++
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];
C++
<blockquote class="quote"><div class="op">Quote:</div>

<pre>
        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);
                }</blockquote></pre>

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.

modified 5-Apr-16 17:07pm.

AnswerRe: C, Win32 API: l need a help with file/data storage Pin
jeron14-Apr-16 4:49
jeron14-Apr-16 4:49 
QuestionRe: C, Win32 API: l need a help with file/data storage Pin
David Crow4-Apr-16 5:22
David Crow4-Apr-16 5:22 
AnswerRe: C, Win32 API: l need a help with file/data storage Pin
Jochen Arndt4-Apr-16 5:25
professionalJochen Arndt4-Apr-16 5:25 
QuestionAn example of _tcslwr with BYTE type Pin
Member 123535314-Apr-16 0:18
Member 123535314-Apr-16 0:18 
AnswerRe: An example of _tcslwr with BYTE type Pin
Jochen Arndt4-Apr-16 1:08
professionalJochen Arndt4-Apr-16 1:08 
Questionhow to access member functions and variables in another project without .lib Pin
kamal19773-Apr-16 7:53
kamal19773-Apr-16 7:53 
AnswerRe: how to access member functions and variables in another project without .lib Pin
Daniel Pfeffer3-Apr-16 20:16
professionalDaniel Pfeffer3-Apr-16 20:16 
GeneralRe: how to access member functions and variables in another project without .lib Pin
kamal19774-Apr-16 12:10
kamal19774-Apr-16 12:10 
AnswerRe: how to access member functions and variables in another project without .lib Pin
Richard MacCutchan3-Apr-16 21:43
mveRichard MacCutchan3-Apr-16 21:43 
GeneralRe: how to access member functions and variables in another project without .lib Pin
kamal19774-Apr-16 12:07
kamal19774-Apr-16 12:07 
GeneralRe: how to access member functions and variables in another project without .lib Pin
Richard MacCutchan4-Apr-16 21:45
mveRichard MacCutchan4-Apr-16 21:45 
GeneralRe: how to access member functions and variables in another project without .lib Pin
leon de boer5-Apr-16 2:40
leon de boer5-Apr-16 2:40 
AnswerRe: how to access member functions and variables in another project without .lib Pin
leon de boer5-Apr-16 2:50
leon de boer5-Apr-16 2:50 
QuestionC++ file stream read/write[SOLVED] Pin
Biruk Abebe3-Apr-16 6:22
Biruk Abebe3-Apr-16 6:22 
AnswerRe: C++ file stream read/write Pin
leon de boer3-Apr-16 7:12
leon de boer3-Apr-16 7:12 
GeneralRe: C++ file stream read/write Pin
Biruk Abebe3-Apr-16 7:40
Biruk Abebe3-Apr-16 7:40 
GeneralRe: C++ file stream read/write Pin
leon de boer3-Apr-16 9:22
leon de boer3-Apr-16 9:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.