Author Topic: File-Opening using Createfile  (Read 4136 times)

0 Members and 1 Guest are viewing this topic.

Offline Patrice Terrier

  • Hero Member
  • *****
  • Posts: 934
  • User-Rate: +62/-1
  • Gender: Male
    • www.zapsolution.com
File-Opening using Createfile
« on: January 03, 2015, 10:22:41 AM »
You have to play with both the AccessMode and the ShareMode attributes.

Here is the all-languages common SDK code.

Code: [Select]
// Generic Open file function, less ovehead than using the PB encapsulation of the same API
long zFOpen (IN wstring sFilName, IN long AccessMode, IN long ShareMode, OUT HANDLE &hFile) {

    long AccessIs, ShareIs, FlagAndAttribute, nRet;
    nRet = 0; hFile = 0;

    AccessMode = min(max(AccessMode, 0), 2);    // Coherce between 0-2
    if (AccessMode == 0) {                      // 0 Open for read only.
       AccessIs = GENERIC_READ; }
    else if (AccessMode == 1) {                 // 1 Open for write only.
       AccessIs = GENERIC_WRITE; }
    else {                                      // 2 Open for read and write.
       AccessIs = GENERIC_READ | GENERIC_WRITE;
    }

    ShareMode = min(max(ShareMode, 1), 4);      // Coherce between 1-4
    if (ShareMode == 1) {                       // 1 Deny read/write access.
       ShareIs = 0; }
    else if (ShareMode == 2) {                  // 2 Deny write access.
       ShareIs =  FILE_SHARE_READ; }
    else if (ShareMode == 3) {                  // 3 Deny read access.
       ShareIs =  FILE_SHARE_WRITE; }
    else {                                      // 4 Deny none (full share mode).
       ShareIs =  FILE_SHARE_READ | FILE_SHARE_WRITE;
    }

    if (hFile == INVALID_HANDLE_VALUE) {
       FlagAndAttribute = FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH; }
    else {
       FlagAndAttribute = FILE_ATTRIBUTE_NORMAL;
    }

    hFile = CreateFile(sFilName.c_str(), AccessIs, ShareIs, NULL, OPEN_ALWAYS, FlagAndAttribute, NULL);

    if (hFile == INVALID_HANDLE_VALUE) {        // -1 Fail to create the file
       nRet = GetLastError();                   // Set the error code
       hFile = 0;                               // Reset handle number
    }
    return nRet;
}
« Last Edit: January 03, 2015, 10:26:28 AM by Patrice Terrier »
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Offline Theo Gottwald

  • Administrator
  • Hero Member
  • *****
  • Posts: 1058
  • User-Rate: +30/-4
  • Gender: Male
    • it-berater
Re: File-Opening using Createfile
« Reply #1 on: January 03, 2015, 10:54:31 AM »
Thanks Patrice.
Jose was right i get the invalid handle.
I had a problem with the filename.
I have shortened the post and removed misleading parts.