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; 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);
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 {
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.
c c # windows dll service
vnammour
source share