locked
Access Violation Exception in winrt application

    Question

  • Hi,

    When i try to assign the handler retured from createfile2() to a void pointer which i passed from my application, my application is giving the following error "Unhandled exception at 0x000007F612102079 in AppToTestPlatform.exe: 0xC0000005: Access violation writing location 0x0000000000000000"

    Sample code:

    terror File_Open(void* fp, char *fName, eFileOpenMode aMode)
    {
     HANDLE hdl = NULL;
     tError lStatus;
     // convert the string file name to LPCWSTR

        UINT a = strlen(fName);
        BSTR unicodefName = SysAllocStringLen(NULL, a);
        ::MultiByteToWideChar(CP_ACP, 0, fName, a, unicodefName, a);
        
     hdl = CreateFile2(unicodefName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,0);
     lStatus = GetLastError();
     if(lStatus == ERROR_ALREADY_EXISTS || lStatus == NO_ERROR)
     {
      *fp = (tFileHandle)hdl;// Getting Error on this line   <<---
      return E_SUCCESS;
     }
    }

    void*handle=nullptr; 
    char name[]={"C:\\Users\\win8\\AppData\\Local\\Packages\\54ed7c5f-b7d8-4842-bbf1-b6dabe9918f6_913w5jmqyr9pt\\LocalState\\Sample.txt"};//Creating a file in application local folder
    int32 result=File_Open(handle, name, WRITE_MODE);

    what are the possible cause for the above ?

    Monday, October 22, 2012 11:52 AM

Answers

  • Is fp set to a valid pointer there? It looks likely that it is null.

    Also note that the error handling is incorrect here. GetLastError is only valid if CreateFile2 failed, so you need to check if hdl is INVALID_HANDLE_VALUE before checking GetLastError(). If CreateFile2 succeeded, then GetLastError may return anything, not necessarily NO_ERROR.

    --Rob

    • Marked as answer by AKEV Wednesday, October 24, 2012 5:17 AM
    Monday, October 22, 2012 7:24 PM
    Owner