AccessViolationException .NET 2.0 ServiceBase class sample code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I am trying to get the sample code at
http://msdn2.microsoft.com/en-us/library/5xh5cfw0(en-US,VS.80).aspx
up and running, but get an AccessViolationException. The exception occurs
where a call is made to
[DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")]
public static extern bool SetServiceStatus(
IntPtr hServiceStatus,
SERVICE_STATUS lpServiceStatus
);
If i comment out that call, the code works. What have I missed ?

The full message text is "
Service cannot be started. System.AccessViolationException: Attempted
to read or write protected memory. This is often an indication that
other memory is corrupt.
at ServiceSample.SimpleService.SetServiceStatus(IntPtr
hServiceStatus, SERVICE_STATUS lpServiceStatus)
at ServiceSample.SimpleService.OnStart(String[] args)
at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)"

What have I missed ?
 
The declaration in the sample code is incorrect. This works
[DllImport("advapi32.dll", SetLastError = true)]
public static extern int SetServiceStatus(IntPtr hServiceStatus, ref
SERVICE_STATUS lpServiceStatus);

Note the ref keyword.
 
Back
Top