如何p/调用c#中的change eserviceconfig2 ?

时间:2022-09-01 19:03:40

I want to p/invoke the windows api function ChangeServiceConfig2 for setting the description and/or "start delayed" flag for a service (and possibly more). Special thing about this is that it takes a pointer to different structures depending on what you want to change.

我希望p/调用windows api函数ChangeServiceConfig2来设置服务的描述和/或“开始延迟”标志(可能更多)。它的特别之处在于,它使用指针指向不同的结构,这取决于你想要改变什么。

For example, to change the description text of a service, you'll need to point it to a SERVICE_DESCRIPTION structure and to change the delayed flag you'll point to a SERVICE_DELAYED_AUTO_START_INFO structure.

例如,要更改服务的描述文本,您需要将其指向SERVICE_DESCRIPTION结构,并更改延迟标志,您将指向SERVICE_DELAYED_AUTO_START_INFO结构。

I have solved this by creating several overloads of the api function like this:

我通过创建api函数的多个重载解决了这个问题:

   private static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, [MarshalAs(UnmanagedType.Struct)] ref SERVICE_DESCRIPTION lpInfo);
   private static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, [MarshalAs(UnmanagedType.Struct)] ref SERVICE_DELAYED_AUTO_START_INFO lpInfo);

I want to ask if there's a better solution that wouldn't require another dozen or so overloads if I want to completely prototype it?

我想问,如果我想要完全原型化,是否有更好的解决方案,不需要再做十几次重载?

1 个解决方案

#1


0  

Just took a look inside the code of the ServiceInstaller class and found out that they're doing it the same way, so it cannot be all wrong...

只要看一下ServiceInstaller类的代码,就会发现它们的做法是一样的,所以不可能都是错的……

Still open for suggestions, though.

尽管如此,仍然可以接受建议。

#1


0  

Just took a look inside the code of the ServiceInstaller class and found out that they're doing it the same way, so it cannot be all wrong...

只要看一下ServiceInstaller类的代码,就会发现它们的做法是一样的,所以不可能都是错的……

Still open for suggestions, though.

尽管如此,仍然可以接受建议。