Error using CreateFileMapping - C

后端 未结 6 1968
梦谈多话
梦谈多话 2021-01-24 20:38

I am using the tutorial on this MSDN link to implement a way of transferring data from one process to another. Although I was advised in an earlier question to use the Pipe meth

相关标签:
6条回答
  • 2021-01-24 21:12

    Ensure the Global name is unique; this can be done with a tool named Process Explorer.

    If not unique, this would typically fail with Error code 6 (The handle is invalid) upon calling CreateFileMappinng

    1. Download Process Explorer from SysInternals
    2. Run Process Explorer
    3. Run your application up to a break point just prior to the CreateFileMapping() failure
    4. Search for MyFileMappingObject using Find (Ctrl-F)
    5. If anything comes up such as another FileMap, Mutex, etc.. consider a more unique name, and ensure your application is not the one creating it.

    Note: Consider naming your FileMapping using a GUID (File -> Tools > Create GUID) within Visual Studio

    0 讨论(0)
  • 2021-01-24 21:14

    Try changing "Global\MyFileMappingObject" to "MyFileMappingObject"

    0 讨论(0)
  • 2021-01-24 21:19

    Under Windows 7 I found:

    OpenFileMapping(FILE_MAP_ALL_ACCESS, ...);
    

    Causes issues with:

    CreateFileMapping(
                     (HANDLE)0xFFFFFFFF,    // use paging file
    

    Try:

    OpenFileMappingA(SECTION_MAP_WRITE | SECTION_MAP_READ,...);
    
    0 讨论(0)
  • 2021-01-24 21:22

    In the code for producer, is _getch() intended for non-console application as well?

    0 讨论(0)
  • 2021-01-24 21:26

    Your code for the producer works for me. What version of Windows are you using? In newer versions (like Vista and 7) there are additional security restrictions placed on accessing shared memory. There is a note about this in the MSDN article you referenced above, saying that you must be an Administrator to create global shared memory objects in Windows Vista/7.

    You should also make a call to GetLastError() to see which error code is actually returned from CreateFileMapping(), that may be helpful in determining the root cause of the problem.

    0 讨论(0)
  • 2021-01-24 21:37

    Maybe we learned from the same material/examples in the past. I had the same problem after migrating from XP to Windows 7:

    NULL Handle return value and GetLastError() = 5.

    ERROR_ACCESS_DENIED
        5 (0x5)
        Access is denied.
    

    System Error Codes (0-499): http://msdn.microsoft.com/en-us/library/ms681382.aspx

    I used a lpName with backslashes like in the Microsoft example from http://msdn.microsoft.com/en-us/library/windows/desktop/aa366537.aspx that you posted above. After changing the name of the file mapping object (lpName) from "Global\MyFileMappingObject" to "GlobalMyFileMappingObject", the CreateFileMapping function works again under Windows 7 with no other changes.

    "The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session namespace. The remainder of the name can contain any character except the backslash character ('\'). Creating a file mapping object in the global namespace from a session other than session zero requires the SeCreateGlobalPrivilege privilege. For more information, see Kernel Object Namespaces".

    This is not just a name-change! If you need access to the global namespace, then you have to go the SeCreateGlobalPrivilege way.

    0 讨论(0)
提交回复
热议问题