Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CreateFileMapping and OpenFileMapping accross different users.

CreateFileMapping and OpenFileMapping accross different users.

Scheduled Pinned Locked Moved C / C++ / MFC
helpsecurity
7 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Darrel Q Pham
    wrote on last edited by
    #1

    Hi guys, I'm a new programmer and I'm using CreateFileMapping in my service and trying to open it up with my Process with user A. OpenFileMapping with user A returns a file not found error. I know the file mapping works if I open two processes with the same user. I was able to get two services to share a file mapping correctly. So this must mean that the file mapping is out of scope to user A. Please help.

    //Service
    TCHAR SharedMemoryName[] = _T("Global\\Shared Memory1");

    	HANDLE handle\_SharedMemory = CreateFileMappingA( 
    		INVALID\_HANDLE\_VALUE,	// use paging file
    		NULL,					// default security
    		PAGE\_READWRITE,			// read/write access
    		0,						// max. object size
    		MAX\_SH\_MEM\_SIZE,		// buffer size
    		(LPCSTR) SharedMemoryName ); //name of mapping object
    

    //Process launched by user A
    TCHAR SharedMemoryName[] = _T("Global\\Shared Memory1");

                HANDLE handle\_SharedMemory = OpenFileMappingA( 
    		FILE\_MAP\_ALL\_ACCESS, //read/write access
    		FALSE,				 //do not inherit the name
    		(LPCSTR) SharedMemoryName ); //name of mapping object
    
    L 1 Reply Last reply
    0
    • D Darrel Q Pham

      Hi guys, I'm a new programmer and I'm using CreateFileMapping in my service and trying to open it up with my Process with user A. OpenFileMapping with user A returns a file not found error. I know the file mapping works if I open two processes with the same user. I was able to get two services to share a file mapping correctly. So this must mean that the file mapping is out of scope to user A. Please help.

      //Service
      TCHAR SharedMemoryName[] = _T("Global\\Shared Memory1");

      	HANDLE handle\_SharedMemory = CreateFileMappingA( 
      		INVALID\_HANDLE\_VALUE,	// use paging file
      		NULL,					// default security
      		PAGE\_READWRITE,			// read/write access
      		0,						// max. object size
      		MAX\_SH\_MEM\_SIZE,		// buffer size
      		(LPCSTR) SharedMemoryName ); //name of mapping object
      

      //Process launched by user A
      TCHAR SharedMemoryName[] = _T("Global\\Shared Memory1");

                  HANDLE handle\_SharedMemory = OpenFileMappingA( 
      		FILE\_MAP\_ALL\_ACCESS, //read/write access
      		FALSE,				 //do not inherit the name
      		(LPCSTR) SharedMemoryName ); //name of mapping object
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Have you enabled "Interact with desktop" for the service?

      D 1 Reply Last reply
      0
      • L Lost User

        Have you enabled "Interact with desktop" for the service?

        D Offline
        D Offline
        Darrel Q Pham
        wrote on last edited by
        #3

        Yes it is enabled. Any other suggestions?

        L 1 Reply Last reply
        0
        • D Darrel Q Pham

          Yes it is enabled. Any other suggestions?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          What operating system is the service running on? Try using AdjustTokenPrivileges to set SeCreateGlobalPrivilege in the service before creating the mapping.

          D 1 Reply Last reply
          0
          • L Lost User

            What operating system is the service running on? Try using AdjustTokenPrivileges to set SeCreateGlobalPrivilege in the service before creating the mapping.

            D Offline
            D Offline
            Darrel Q Pham
            wrote on last edited by
            #5

            It still doesn't work. This is what I did. Perhaps I did something wrong here?

            //Adjust Token and privileges to set global shared memory space
            HANDLE l_handle_Token; //To Do Close handle
            TOKEN_PRIVILEGES l_tokenprivileges_myPrivileges;

            if( !OpenProcessToken( GetCurrentProcess(), TOKEN\_ADJUST\_PRIVILEGES | TOKEN\_QUERY, &l\_handle\_Token ) )
            {
            	WriteLog( g\_cLogFile, \_T("OpenProcessToken failed \\n")		);
            	return (FALSE);
            }
            
            LUID l\_luid\_ID;	
            if( !::LookupPrivilegeValue( NULL, SE\_CREATE\_GLOBAL\_NAME, &l\_luid\_ID ) )
            {
            	WriteLog( g\_cLogFile, \_T("LookupPrivilageValue failed \\n")		);
            	CloseHandle( l\_handle\_Token );
            	return (FALSE);
            }
            else 
            {
            	WriteLog( g\_cLogFile, \_T("LookupPrivilageValue successful \\n")		);
            	l\_tokenprivileges\_myPrivileges.PrivilegeCount = 1;
            	l\_tokenprivileges\_myPrivileges.Privileges\[0\].Luid = l\_luid\_ID;
            	l\_tokenprivileges\_myPrivileges.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
            	AdjustTokenPrivileges( 
            		l\_handle\_Token, 
            		FALSE, 
            		&l\_tokenprivileges\_myPrivileges, 
            		sizeof(TOKEN\_PRIVILEGES), 
            		(PTOKEN\_PRIVILEGES)NULL, 
            		0 );
            	CloseHandle( l\_handle\_Token );
            }
            
            D 1 Reply Last reply
            0
            • D Darrel Q Pham

              It still doesn't work. This is what I did. Perhaps I did something wrong here?

              //Adjust Token and privileges to set global shared memory space
              HANDLE l_handle_Token; //To Do Close handle
              TOKEN_PRIVILEGES l_tokenprivileges_myPrivileges;

              if( !OpenProcessToken( GetCurrentProcess(), TOKEN\_ADJUST\_PRIVILEGES | TOKEN\_QUERY, &l\_handle\_Token ) )
              {
              	WriteLog( g\_cLogFile, \_T("OpenProcessToken failed \\n")		);
              	return (FALSE);
              }
              
              LUID l\_luid\_ID;	
              if( !::LookupPrivilegeValue( NULL, SE\_CREATE\_GLOBAL\_NAME, &l\_luid\_ID ) )
              {
              	WriteLog( g\_cLogFile, \_T("LookupPrivilageValue failed \\n")		);
              	CloseHandle( l\_handle\_Token );
              	return (FALSE);
              }
              else 
              {
              	WriteLog( g\_cLogFile, \_T("LookupPrivilageValue successful \\n")		);
              	l\_tokenprivileges\_myPrivileges.PrivilegeCount = 1;
              	l\_tokenprivileges\_myPrivileges.Privileges\[0\].Luid = l\_luid\_ID;
              	l\_tokenprivileges\_myPrivileges.Privileges\[0\].Attributes = SE\_PRIVILEGE\_ENABLED;
              	AdjustTokenPrivileges( 
              		l\_handle\_Token, 
              		FALSE, 
              		&l\_tokenprivileges\_myPrivileges, 
              		sizeof(TOKEN\_PRIVILEGES), 
              		(PTOKEN\_PRIVILEGES)NULL, 
              		0 );
              	CloseHandle( l\_handle\_Token );
              }
              
              D Offline
              D Offline
              Darrel Q Pham
              wrote on last edited by
              #6

              I checked the service information with process explorer and it does show that SE_CREATE_GLOBAL_NAME(SeCreateGlobalPrivilege ) is set. CreateEvent(...) was set up and called similarly, and that works fine. I initially had problems with access violations with OpenEvent(...) but atleast I know there is something there, and I fixed that with creating an empty security attribute and passing that in with OpenEvent(...). This is beyond me. More ideas would greatly be appreciated.

              D 1 Reply Last reply
              0
              • D Darrel Q Pham

                I checked the service information with process explorer and it does show that SE_CREATE_GLOBAL_NAME(SeCreateGlobalPrivilege ) is set. CreateEvent(...) was set up and called similarly, and that works fine. I initially had problems with access violations with OpenEvent(...) but atleast I know there is something there, and I fixed that with creating an empty security attribute and passing that in with OpenEvent(...). This is beyond me. More ideas would greatly be appreciated.

                D Offline
                D Offline
                Darrel Q Pham
                wrote on last edited by
                #7

                Solution: The casting is wrong. Now I just have to find a way to pass my TCHAR array/string correctly.

                g_handle_SharedMemory[i_counter] = CreateFileMapping(
                INVALID_HANDLE_VALUE, // use paging file
                &l_securityattributes_AllAccessAttr, // default security
                PAGE_READWRITE, // read/write access
                0, // max. object size
                MAX_SH_MEM_SIZE, // buffer size
                "Global\\Shared Memory" ); //name of mapping object

                instead of

                g_handle_SharedMemory[i_counter] = CreateFileMapping(
                INVALID_HANDLE_VALUE, // use paging file
                &l_securityattributes_AllAccessAttr, // default security
                PAGE_READWRITE, // read/write access
                0, // max. object size
                MAX_SH_MEM_SIZE, // buffer size
                (LPTSTR)Somestring ); //name of mapping object

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups