调用本机DLL从.NET Windows服务失败本机、DLL、Windows、NET

2023-09-06 05:03:55 作者:赤贯妖星 i

我在,我从C#中使用调用的DllImport 本机DLL形式的第三方API。这个本地的dll依赖于第三方应用程序被打开。

I have a 3rd party API in the form of a native dll that I call from C# using DllImport. This native dll depends on the 3rd party application being open.

当我运行code常的API做什么预期和驱动程序。然而,当我运行相同的code作为Windows服务,甚至为自己的API返回相同的(无证)错误code,我已经看到,当应用程序被关闭;进程资源管理器确认本机的dll被正确地从应用程序目录加载。

When I run the code normally the API does what is expected and drives the application. However, when I run the same code as a Windows service, even as myself, the API returns the same (undocumented) error code that I have seen when the application is closed; process explorer confirms that the native dll is correctly loaded from the application directory.

什么引起这一点,我怎么可能能够解决问题?

What could be causing this and how might I be able to resolve the issue?

推荐答案

很难说,但有三种可能性,我能想到的:

Hard to tell, but there is three possibilities I can think of:

您的服务有需要与桌面交互UI组件选项选中 对窗口服务的工作目录%WINDIR%\ SYSTEM32(例如C:\ Windows \ System32下),你的DLL有使用相对路径引用其他资源无法找到code 您的服务使用与在两个不同的会话中运行的应用程序netpipe通信