I am trying to execute the below code but unable to compile due to the error OnStart function.
When I try to compile this whole code it shows up error in Service class function along with On start function . Currently I am trying to compile this code on visual studio 2015 .
I have tried changing OnStart() to OnAppearing() but no change same error comes up every time .
Please visit the link to see the screenshot
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:
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
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.
To test this, try removing the partial keyword and see if your code compiles then. System.ServiceProcess.ServiceBase has a virtual protected OnStarted method, so it should be overridable the way you set it up.
User contributions licensed under CC BY-SA 3.0