Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C, Win32 API: l need a help with file/data storage

C, Win32 API: l need a help with file/data storage

Scheduled Pinned Locked Moved C / C++ / MFC
helpvisual-studiocsharpcomjson
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12105981
    wrote on last edited by
    #1

    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

    J D J 3 Replies Last reply
    0
    • U User 12105981

      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

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • U User 12105981

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • U User 12105981

          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

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #4

          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);

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups