none
CreateFileMapping() returning null for local or standard User RRS feed

  • Question

  • hMapFile = Win32test.CreateFileMapping(
                Win32test.INVALID_HANDLE_VALUE,
                ref securityAttributes,
                Win32test.PAGE_READWRITE,
                0,
                BUF_SIZE,
                //"Global\\MyFileMappingObject"
                lpName1
                //null
                );

                if (hMapFile == IntPtr.Zero)
                {
                    throw new Exception("CreateFileMapping", new Win32Exception(Marshal.GetLastWin32Error()));

                }            

                // Map file
                pView = Win32test.MapViewOfFile(
                hMapFile,
                //Win32.FILE_MAP_WRITE,
                Win32test.File_Map_All_Access,
                0,
                0,
                BUF_SIZE
                );

                if (pView == IntPtr.Zero)
                {
                    throw new Exception("MapViewOfFile", new Win32Exception(Marshal.GetLastWin32Error()));
                }

                pData = Marshal.StringToHGlobalAnsi(ip);
                Win32test.MemCopy(pView, pData, (uint)ip.Length);

    Above is my code which is not working for Local/Standard User

    Please can any one help?                                               

    Tuesday, January 8, 2019 9:59 AM

All replies

  • Is there any reason why you're not using MemoryMappedFile which already maps files into memory to be shared by processes? Please clarify what the built in class doesn't handle that you need to P/Invoke for.

    Please post where the code is failing, what exception you're getting, any callstack information and any other details that may help clarify what you're doing to get this to work. The signatures for the P/Invoke functions you're calling are also important.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 8, 2019 3:04 PM
    Moderator
  • Hi Shiny Arline,

    Thank you for posting here.

    According to your description, it does not work with local and standard User.

    If so, please check if it works with other users. Then, you can check the parameters passed if it is correct. You could set a breakpoint for debugging.

    If the problem still exists, please share a simple code example to reproduce your problem via OneDrive.

    We are waiting for your update.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 10, 2019 1:15 AM
    Moderator
  • hMapFile = Win32test.CreateFileMapping(
                Win32test.INVALID_HANDLE_VALUE,
                ref securityAttributes,
                Win32test.PAGE_READWRITE,
                0,
                BUF_SIZE,
                //"Global\\MyFileMappingObject"
                lpName1
                //null
                );

                if (hMapFile == IntPtr.Zero)
                {
                    throw new Exception("CreateFileMapping", new Win32Exception(Marshal.GetLastWin32Error()));

                }            

    .....

    Above is my code which is not working for Local/Standard User

    Please can any one help?                                               

    Let's assume that there are no coding errors and that you are trying to create a named file mapping object in the Global namespace.  The reason that this would fail when attempted by a standard user is because standard user accounts do not have the required SeCreateGlobalPrivilege privilege that is necessary for this action to succeed.  See the documentation for this privilege at Privilege Constants
    Thursday, January 10, 2019 2:07 AM