Calling C ++ dll (unmanaged code) from a Windows C # service (written in managed code)

I am working on a C ++ project that has different components. We need to run the application as a Windows service. The project is unmanaged C ++ code. I wrote a Windows C # service and a C-style DLL that will have a function to start various components, and another to stop them. The dll has two files: the header and the .cpp file: RTSS.h:

namespace PFDS { extern "C" __declspec(dllexport) int runRTS(char*); } 

RTSS.cpp:

 using namespace PFDS; /* ... includes and declarations */ extern "C" __declspec(dllexport) int runRTS(char* service_name) { g_reserved_memory = (char*) malloc(sizeof(char) * RESERVED_MEMORY_SIZE); _set_new_handler(memory_depletion_handler); // this function is from a C++ .lib which is included in // the linker input for the RTSS dll project setting. // SetUnhandledExceptionHandler("RTS"); return 0; } 

In a subclass of a ServiceBase Windows service, I have the following:

 [DllImport("RTSSd.dll")] public static extern int runRTS(string serviceName); protected override void OnStart(string[] args) { try { // the bin directory has all dependencies (dlls needed) Environment.CurrentDirectory = "D:/work/projects/bin"; eventLog1.WriteEntry("RTSWinService: Starting " + this.ServiceName); int result = runRTS(this.ServiceName); eventLog1.WriteEntry("Result of invoking runRTS = " + result); } catch (Exception e) { eventLog1.WriteEntry("Exception caught: " + e.ToString()); } } 

In addition, I have a console test application with internal code similar to the code inside OnStart. Both the application and the Windows service start without problems when commenting on the SetUnhandledException function. But when I uncomment this function, the Windows console application is working fine, but the Windows service throws the following exception:

System.DllNotFoundException: Unable to load DLL 'RTSSd.dll': The specified module was not found. (Exception from HRESULT: 0x8007007E) in RTSWS.RTSWinService.runRTS (String serviceName) in RTSWS.RTSWinService.OnStart (String [] args) in D: \ work \ project \ ... \ RTSWinService.cs: line 39

I read in some threads on different forums that Windows services start in C: \ WINDOWS \ System32, and this is true, since initializing DirectoryInfo and printing its full name shows this. I tried to change the default start directory using Environment.CurrentDirectory = "the directory where the Windows executables and DLLs are executed," but this did not work. I also tried changing the Process directory, but that also failed. Other topics lead to this conclusion link text , but is it really? Maybe it was easier? I should note that the SetUnhandledException function is written in C ++, not C, and therefore there are many other functions that I need to call. All necessary DLLs are located next to the service executable. Your feedback is greatly appreciated.

Thanks.

+6
c c # windows dll service
source share
2 answers

When something works on the console, but not as a service, I suspect that it allows access. Make sure that the user is working as a service, has read / execute access to d: \ work \ projects \ bin.

Another suggestion is to call SetDllDirectory, which is a more direct way to tell where the DLLs are, than to use the current directory. See this SO thread on pinwoke dll search path

You can also use SysInternal ProcMon to see how the system tries to find your DLL. Sometimes this is not a DLL, but the DLL that your DLL depends on is a problem, and ProcMon is good for finding these problems.

+2
source share

I just want to update this thread. It turned out that the problem was exclusively for my car. The same set of codes worked like a charm on another machine, in the same version of Windows (32-bit version of Windows XP, SP2). On both machines, Visual Studio 2008 was used to create the service. Thanks so much for all the comments and response. I appreciate it.

+1
source share

All Articles