• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

File-Opening using Createfile

Started by Patrice Terrier, January 03, 2015, 10:22:41 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Patrice Terrier

You have to play with both the AccessMode and the ShareMode attributes.

Here is the all-languages common SDK code.

// 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;
}
Patrice Terrier
GDImage (advanced graphic addon)
http://www.zapsolution.com

Theo Gottwald

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.