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

I have done it! The problem was not having administrator rights. It is not related to 32 bit/64 bit distinction. To create, start, stop a service the service control program must have administrator rights. I've started the command line with administrator rights and the program worked.

于 2016-04-03T06:22:44.260 に答える