我有一个非托管的dll,我将其导入到c#控制台应用程序中:
[DllImport("mylib.dll", EntryPoint = "myfunction", CallingConvention = CallingConvention.Cdecl)]
public static extern void MyFunction();
该应用程序运行正常。
然后,我使用Topshelf将控制台应用程序托管为服务:
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");
});
我像这样安装并启动服务:
myapp.exe install
myapp.exe start
服务已启动,但日志中显示错误,提示它找不到我的dll:
System.DllNotFoundException: Unable to load DLL 'mylib.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
更新: 我尝试传递完整路径,甚至将其放入System32。仍然不起作用。
谢谢!