Hi I need to use QueryServiceConfig WinAPI Method, using C# Interop,
VS2003.NET.
The .NET ServiceController class can not do what I need.
I am unable to correctly call the QueryServiceConfig() method, can you help?
Here is my code:
// Begin Source Code
using System;
using System.Runtime.InteropServices;
class ServiceTest
{
[DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern IntPtr OpenSCManager(String lpMachineName, String
lpDatabaseName, UInt32 dwDesiredAccess);
[DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern IntPtr OpenService(IntPtr hSCManager, String
lpServiceName, UInt32 dwDesiredAccess);
[DllImport("advapi32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
public static extern Boolean QueryServiceConfig(IntPtr hService, IntPtr
intPtrQueryConfig, UInt32 cbBufSize, out UInt32 pcbBytesNeeded);
[StructLayout(LayoutKind.Sequential)]
public class QUERY_SERVICE_CONFIG
{
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
public UInt32 dwServiceType;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
public UInt32 dwStartType;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
public UInt32 dwErrorControl;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public String lpBinaryPathName;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public String lpLoadOrderGroup;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
public UInt32 dwTagID;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public String lpDependencies;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public String lpServiceStartName;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
public String lpDisplayName;
};
private const int SC_MANAGER_ALL_ACCESS = 0x000F003F;
private const int SERVICE_QUERY_CONFIG = 0x00000001;
[STAThread]
static void Main(string[] args)
{
// This seems to be working.
IntPtr databaseHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
if(databaseHandle == IntPtr.Zero)
Console.WriteLine("Error OpenSCManager\n");
// This seems to be working.
IntPtr serviceHandle = OpenService(databaseHandle, "Alerter",
SERVICE_QUERY_CONFIG);
if(databaseHandle == IntPtr.Zero)
Console.WriteLine("Error OpenService\n");
UInt32 dwBytesNeeded = 0;
QUERY_SERVICE_CONFIG qUERY_SERVICE_CONFIG = new QUERY_SERVICE_CONFIG();
// I am unable to call QueryServiceConfig()
// Don't know how to convert to Intptr Buffer.
//Boolean success = QueryServiceConfig(serviceHandle, /* HELP !!! */,
4096, out dwBytesNeeded);
}
}
// End Source Code
Mattias Sj?gren - 05 Dec 2004 12:02 GMT
Russell,
> // I am unable to call QueryServiceConfig()
> // Don't know how to convert to Intptr Buffer.
> //Boolean success = QueryServiceConfig(serviceHandle, /* HELP !!! */,
>4096, out dwBytesNeeded);
Allocate some unmanaged memory with one of the Marshal.Alloc* methods.
Pass in the pointer to QueryServiceConfig. Retrieve the struct after
the call with Marshal.PtrToStructure. Finally free the buffer with the
corresponding Marshal.Free* method.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.