all groups > dotnet interop > february 2006 >
You're in the

dotnet interop

group:

Posible ServiceBase sample error


Posible ServiceBase sample error Yanko
2/28/2006 2:50:34 PM
dotnet interop:
Hi guys/gals

I'm not versed at all using p/invoke so I don't know how to fix
this:

Given this declarations:
/**************************************/
typedef struct _SERVICE_STATUS {
DWORD dwServiceType;
DWORD dwCurrentState;
DWORD dwControlsAccepted;
DWORD dwWin32ExitCode;
DWORD dwServiceSpecificExitCode;
DWORD dwCheckPoint;
DWORD dwWaitHint;
} SERVICE_STATUS, *LPSERVICE_STATUS;

BOOL SetServiceStatus(
SERVICE_STATUS_HANDLE hServiceStatus,
LPSERVICE_STATUS lpServiceStatus
);
/**************************************/

Is it correct to translate them (for p/invoke use) to this
declarations?
(as seen in
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.aspx)

/**************************************/
[StructLayout(LayoutKind.Sequential)]
public struct SERVICE_STATUS
{
public int serviceType;
public int currentState;
public int controlsAccepted;
public int win32ExitCode;
public int serviceSpecificExitCode;
public int checkPoint;
public int waitHint;
}

[DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")]
public static extern bool SetServiceStatus(
IntPtr hServiceStatus,
SERVICE_STATUS lpServiceStatus
);

private SERVICE_STATUS myServiceStatus;
..
..
..
myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
SetServiceStatus(handle, myServiceStatus);
/**************************************/

I programmed a test service based on this sample and it worked without
a hitch on my PC (w2k3), but on a colleague's PC (XPsp2), it crashed
giving us this exception:

Service cannot be started. System.AccessViolationException: Attempted
to read or write protected memory. This is often an indication that
other memory is corrupt.
at Test.MyService.SetServiceStatus (IntPtr hServiceStatus,
SERVICE_STATUS lpServiceStatus)
at Test.MyService.OnStart(String[] args) in
G:\Projs\Test\MyService.cs:line 70
at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

Why this happened on his PC and it didn't on mine? Wouldn't be the
correct way to do the definitions and the call the following one?

/**************************************/
[DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")]
public static extern bool SetServiceStatus(
IntPtr hServiceStatus,
ref SERVICE_STATUS lpServiceStatus // ref added
);
..
..
..
SetServiceStatus(handle, ref myServiceStatus); // ref added
/**************************************/

Can someone help us?

Yanko
Re: Posible ServiceBase sample error jpwell
3/17/2006 2:53:05 PM
The second parameter needs a ref keyword.

[DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")]
public static extern bool SetServiceStatus(
IntPtr hServiceStatus,
ref SERVICE_STATUS lpServiceStatus
);

---
AddThis Social Bookmark Button