PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

AccessViolationException .NET 2.0 ServiceBase class sample code

 
 
=?Utf-8?B?cGVzIHBsYW5v?=
Guest
Posts: n/a
 
      11th Nov 2005
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 ?
 
Reply With Quote
 
 
 
 
=?Utf-8?B?cGVzIHBsYW5v?=
Guest
Posts: n/a
 
      11th Nov 2005

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.

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

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is class Sample<T> where T : Stream, class ILLEGAL puzzlecracker Microsoft C# .NET 29 7th Nov 2008 03:33 PM
Can't specify a location for new class code file on class diagram>new class dialog? Andy B Microsoft C# .NET 3 14th Feb 2008 01:57 AM
Sample code to make your code reload data that changed in config file delphiconsultingguy@yahoo.com Microsoft Dot NET Framework 0 13th Jun 2006 06:55 PM
Code to simulate mouse click not working - code sample attached John Michl Microsoft Powerpoint 3 19th Oct 2005 03:00 PM
Needed: sample code for Mime objects in "lesnikowski.Mail" Pop3 class Mark Stokes Microsoft ASP .NET 0 15th Mar 2004 09:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:54 PM.