c# - 为什么从 unity3d 插件 dll 调用时为 System.ServiceProcess.ServiceController.GetServices() 提供空引用
问题描述
我正在尝试实现ServiceController,它是 Windows API 的一部分,以从 Unity3d 中的 ac# dll 插件检查 Windows 服务的状态。当我从 Unity Editor 运行 dll 时,结果会根据天气是否存在服务正确给出。但是,当我进行构建并尝试作为 exe 运行时,它会出现以下错误:
NullReferenceException: Object reference not set to an instance of an object
at (wrapper unknown) System.ServiceProcess.Win32ServiceController/ENUM_SERVICE_STATUS_PROCESS:PtrToStructure (intptr,object)
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)
at System.ServiceProcess.Win32ServiceController.GetServices (System.String machineName, SERVICE_TYPE serviceType, System.String group) [0x00000] in <filename unknown>:0
at System.ServiceProcess.Win32ServiceController.GetServices () [0x00000] in <filename unknown>:0
at System.ServiceProcess.ServiceController.GetServices (System.String machineName) [0x00000] in <filename unknown>:0
at System.ServiceProcess.ServiceController.GetServices () [0x00000] in <filename unknown>:0
这是我从 Unity3d 代码调用的 dll 中的静态方法:
public static bool isServicePresent(string serviceName)
{
bool serviceExists = false;
foreach (ServiceController sc in ServiceController.GetServices())
{
if (sc.ServiceName == serviceName)
{
//service is found
serviceExists = true;
break;
}
}
return serviceExists;
}
虽然错误来自部分GetService()但如果我尝试使用下面提到的功能,则不会为独立和编辑器生成错误。我确实知道它可能与new关键字有关,但我完全没有得到证实。
public static int GetServiceControllerStatus(string serviceName)
{
try
{
if(serviceController == null)
serviceController = new ServiceController(serviceName);
}
catch (ArgumentException)
{
//return "Invalid service name."; // Note that just because a name is valid does not mean the service exists.
return 0;
}
using (serviceController)
{
try
{
serviceController.Refresh(); // calling sc.Refresh() is unnecessary on the first use of `Status` but if you keep the ServiceController in-memory then be sure to call this if you're using it periodically.
return (int)serviceController.Status;
}
catch (Win32Exception ex)
{
// A Win32Exception will be raised if the service-name does not exist or the running process has insufficient permissions to query service status.
// See Win32 QueryServiceStatus()'s documentation.
//return "Error: " + ex.Message;
return -1;
}
}
}
所以关于上述结果,我有几个问题:
- 为什么它在 Unity 编辑器中工作而不是在独立构建中工作?
- 如果在 UnityEditor 中工作意味着 unityeditor 是否具有它初始化的东西或我的独立设备没有的额外 dll 依赖项?
- 因此,如果我想在 Unity3d 中独立使用 GetServices(),我将如何使其工作?
- 我还使用了sc.DependentServices,其中 sc 是从new ServiceController() 创建的;并且根据描述它说“获取依赖于与此 ServiceController 实例关联的服务的服务集。” 但是当我测试它并没有检索到我想要的服务。那么使用它并不能保证它会返回 pc 上的所有服务集?
我没有任何使用 Windows API 的经验。任何有关这方面的帮助,我将不胜感激。
解决方案
推荐阅读
- ruby - Ruby 中将“John Di Bello”转换为“John DI BELLO”的最短方法
- javascript - 一键提交多个 HTML 表单 (JQuery/Ajax)
- angular - 如何在 Angular 6 中正确设置辅助路线?
- angularjs - 错误:$compile:tplrt:在 Windows 上使用 WebStorm 编译 AngularJS 后无法呈现页面
- html - Chrome 为站点导航添加了空格,但 Safari 没有
- lucene - Lucene 搜索大量名称
- c# - 使用 IN 关键字参数化 SQL 命令
- bash - “dir/*:没有这样的文件或目录”与 find -exec ...“{}/*”
- h2o - 用 H2O 流量预测概率
- javascript - oracle apex 通过 javascript 获取选择列表的选定值