Solved

NT Services & MapViewOfFile handles

Posted on 1997-09-15
4
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:kfe
ID: 1406814
Edited text of question
0
 

Author Comment

by:kfe
ID: 1406815
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
ID: 1406816
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
ID: 1406817
Suggestion: Communicate using named pipes.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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