Home All Groups Group Topic Archive Search About

AccessViolationException .NET 2.0 ServiceBase class sample code

Author
11 Nov 2005 8:16 AM
pes plano
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 ?

Author
11 Nov 2005 1:35 PM
pes plano
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.

Show quote
"pes plano" wrote:

> 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 ?

AddThis Social Bookmark Button