Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

C# Interop question: using QueryServiceConfig (advapi32.dll) : Can you help???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Russell Mangel - 05 Dec 2004 06:43 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.