1

我想将服务安装到 Service Manager 并运行它。我的代码如下:

 using System;
 using System.Runtime.InteropServices;
 class Ana
 {
    static void Main()
    {
        IntPtr sc_handle=OpenSCManager(null,null,2);
        IntPtr sv_handle = CreateService(sc_handle, "deneme", "deneme", 16, 16, 2, 0, @"D:\ServisDeneme2.exe", null, null, null, null, null);
        int i=StartService(sv_handle,0,null);
        CloseServiceHandle(sc_handle);
    }

    [DllImport("advapi32.dll")]
    public static extern IntPtr OpenSCManager(string machine, string db, int parameter);

    [DllImport("advapi32.dll")]
    public static extern IntPtr CreateService(IntPtr SC_HANDLE, string lpSvcName, string lpDisplayName, int dwDesiredAccess, int dwServiceType, int dwStartType, int dwErrorControl, string lpPathName, string lpLoadOrderGroup, object lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword);

    [DllImport("advapi32.dll")]
    public static extern void CloseServiceHandle(IntPtr SCHANDLE);

    [DllImport("advapi32.dll")]
    public static extern int StartService(IntPtr SVHANDLE, int dwNumServiceArgs, string[] lpServiceArgVectors); 
}

此代码在我的 32 位计算机上完美运行,但在 64 位计算机上不起作用。我怎样才能为 64 位做同样的工作?

4

2 回答 2

0

我假设您必须在 x86 中编译该应用程序才能使其在 64 位机器上正常运行。

或者你可以像这个人那样做:

在 C# DllImport 中使用 32 位或 64 位 dll

于 2016-04-01T20:43:23.033 回答
0

我已经做了!问题是没有管理员权限。它与 32 位/64 位的区别无关。要创建、启动、停止服务,服务控制程序必须具有管理员权限。我已经以管理员权限启动了命令行并且程序正常工作。

于 2016-04-03T06:22:44.260 回答