Solved

CreateFileMapping in Vista service

Posted on 2007-04-10
7
2,419 Views
Last Modified: 2013-12-03
I have Windows service which calls CreateFileMapping with some name. User mode client opens file mapping with the same name. This works in Windows XP, but in Vista client reports "File mapping with this name doesn't exist". If the same server runs as Console application and not as service, client opens file mapping successfully.
lpAttributes of CreatefileMapping is NULL. Maybe I need to fill it by some way in client and/or server? Since I don't have previous experience in Windows services and security, I need some direction and maybe some code to solve this problem.
Thanks.
0
Comment
Question by:AlexFM
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
alexcohn earned 400 total points
ID: 18883124
That's a correct guess. Look at the examples here: http://forums.microsoft.com/MSDN/ShowPost.aspx?PageIndex=2&SiteID=1&PostID=1332473 
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
ID: 18886983
You need to set the security so as to allow non-service binaries access.
Here's some example code for the service side:
                  SECURITY_DESCRIPTOR         sd = {0};
                  InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
                  SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);

                  SECURITY_ATTRIBUTES sa = {0};
                  sa.nLength = sizeof(SECURITY_ATTRIBUTES);
                  sa.bInheritHandle = FALSE;
                  sa.lpSecurityDescriptor = &sd;

                  m_MapHandle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE, 0, m_SizeOfBuffer, m_NameOfMyMapView);

By the way, this is NOT unique to Vista.
I had the same problem on Windows 2003 when using a service to create the memory map file.
0
 
LVL 30

Expert Comment

by:Axter
ID: 18887006
FYI:
In my example, I'm mapping memory, instead of a particular file.
If you're mapping a file, change the first argument in above example to the file name.
Example:
m_MapHandle = CreateFileMapping(MyFileName, &sa, PAGE_READWRITE, 0, m_SizeOfBuffer, m_NameOfMyMapView);
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 48

Author Comment

by:AlexFM
ID: 18887681
Adding "Global\" prefix to memory mapped file name solved the problem - one of ways I found following by alexcohn's link. Thank you.
Axter, please take a look at my next Windows security question, maybe you can help there.
0
 
LVL 11

Expert Comment

by:alexcohn
ID: 18887771
Alex, I first decided to write only about "Global\", but while looking for a confirmation I came accross the comprehensive discussion in the MSDN forum, and actually erased my original comment!
0
 
LVL 48

Author Comment

by:AlexFM
ID: 18887962
For reference, after adding Global\ prefix my original problem was solved. But when I changed sevice account from User to LocalSystem, access to file mapping was denied on client side. After applying Axter's code to server side  CreateFileMapping, the problem was solved.
0
 
LVL 11

Expert Comment

by:alexcohn
ID: 18888001
Consider the remark of efratian (http://forums.microsoft.com/MSDN/ShowPost.aspx?PageIndex=2&SiteID=1&PostID=1332473#847374) in the MSDN forum thread. He provided exactly the same code using SetSecurityDescriptorDacl(). He explains further, that if you want the user mode client to have write access to the shared resource, you need, in Vista, to set the integrity level, specified via the SACL.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now