Topshelf service can't find dll

0

I have an unmanaged dll that I import into my c# console application:

[DllImport("mylib.dll", EntryPoint = "myfunction", CallingConvention = CallingConvention.Cdecl)]
public static extern void MyFunction();

The application works just fine.

Then, I use Topshelf to host my console app as a service:

 var rc = HostFactory.Run(x =>
        {
            x.Service<StartupService>();
            x.RunAsLocalSystem()
             .StartAutomatically()
             .EnableServiceRecovery(r =>
             {
                 r.RestartService(0);
                 r.SetResetPeriod(1);
             });

            x.SetStartTimeout(TimeSpan.FromSeconds(120));
            x.SetStopTimeout(TimeSpan.FromSeconds(120));
            x.SetDescription("my service");
            x.SetDisplayName("service");
            x.SetServiceName("service");
        });

I install and start the service like so:

myapp.exe install
myapp.exe start

The service starts, but an error appear in logs saying it can't find my dll:

System.DllNotFoundException: Unable to load DLL 'mylib.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
  • The dll resides in the same directory as the application executable
  • I added mylib.dll's dependencies into PATH. (i also tried copying them into app directory with no luck).
  • The application works just fine when I run it .exe
  • There are a bunch of other dlls I added and the service has no problem finding those

Update: I tried passing a full path and even putting it into System32. Still does not work.

Thanks!

c#
c++
dll
dllimport
topshelf
asked on Stack Overflow Jun 6, 2019 by user4964351 • edited Jun 6, 2019 by user4964351

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0