Solved

NT Services & MapViewOfFile handles

Posted on 1997-09-15
4
538 Views
Last Modified: 2013-12-03
Dear experts...

I have written an NTService that I need to 'talk to' from various other applications via a DLL, so my service creates a hidden window that anyone else can send messages to. This all works fine for simple commands that can return their data in 32 bits, but some of the functionality I need to offer requires the two way transfer of character strings.

So, I create and populate a file map in my DLL (using CreateFileMapping), and send a message to my service asking for its Process Id so that I can use DuplicateHandle() to pass the file map across. This works fine when I run the service in the debugger, and also if I run it just as an executable. But when I run it as a service, when the service tries to use MapViewOfFile() using the duplicated handle, it gets a null pointer and GetLastError returns 6  (ERROR_INVALID_HANDLE).

Is there anything strange about how Services run and in particular their process context that might cause this? Are there any other/better ways of achieving a similar result other than having a hidden window?

Any help or ideas much appreciated!

kfe
0
Comment
Question by:kfe
  • 2
4 Comments
 

Author Comment

by:kfe
Comment Utility
Edited text of question
0
 

Author Comment

by:kfe
Comment Utility
I've looked further into this, and have discovered that it has nothing to do with file map handles, but that the problem happens in my DLL!

When I try to use duplicate handle I use OpenProcess() to get the handle of the Service's process. but this returns a NULL handle! and GetLastError() gives me 5 (Access denied).

My guess is that the Service Control Manager is creating the Service process with security on it such that my user application cannot "open" it. Can any 'NT Service guru' out there confirm this?

kfe
0
 
LVL 3

Accepted Solution

by:
vinniew earned 100 total points
Comment Utility
You're right.  If you try to debug a service by right-clicking on the taskbar, you'll notice that the system may have put security on it.  This usually happens to me when the service is paused or not completely stopped.  The solution is to use the task manager to kill the child process, and try again after the service is stopped.

You can use Mutexes to communicate with a service, also.
0
 
LVL 5

Expert Comment

by:y96andha
Comment Utility
Suggestion: Communicate using named pipes.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

10 Experts available now in Live!

Get 1:1 Help Now