PInvoke Issues...

From: Richard (Richard_at_discussions.microsoft.com)
Date: 08/10/04


Date: Tue, 10 Aug 2004 11:39:02 -0700

Hi,

I want to programmatically open a service and tune it's configuration from
C#. I think I'm having PInvoke issues because I imported the service API
functions and I'm getting nonsense errors from GetLastError() {RPC server not
found/file volume label incorrect} when I try to open the SCM database. I'm
trying to open it on my local machine and I am an Administrator... Questions:

Here's the WIN32 API signature that I'm getting nonsense errors from:

SC_HANDLE OpenSCManager(
  LPCTSTR lpMachineName,
  LPCTSTR lpDatabaseName,
  DWORD dwDesiredAccess
);

Does the code below look like I imported it correctly? I am marshalling the
strings and the DWORD correctly - yes? Do I have to do anyhting special if I
want to marshal the strings as const? What's the PInvoke way to pass a NULL
string to this WIN32 API? I would like to pass nulls for the strings as the
API documentation says this function defaults to the correct settings for my
needs...

[DllImport("AdvApi32.dll", SetLastError = true)]
static extern IntPtr OpenSCManager([MarshalAs(UnmanagedType.LPTStr)] string
machineName, [MarshalAs(UnmanagedType.LPTStr)] string databaseName, uint
desiredAccess);

I'm calling the function like this:

managerHandle = OpenSCManager(null, SERVICES_ACTIVE_DATABASE, (uint)
ServiceAccess.SC_MANAGER_ENUMERATE_SERVICE);
uint error = GetLastError();

Depending on what I pass for my machine name {tried Environment.MachineName
with no luck} I get bizarre error return code back and the handle may or may
not be 0. If the handle is not 0 and I try to use it I always get an
"invalid handle" error...

Any help would be appreciated...

--Richard



Relevant Pages

  • Re: DLL Marshalling : How do I treat a char FAR * HUGE * FAR * in C# ?
    ... The P/Invoke layer doesn't know how to marsal arrays back to the managed ... are you supplying the memory where the strings are written to? ... > API call by doing a type conversion for each parameter. ... > public static extern int XcDatabaseCodes( ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Equivalent to GetKeyNameText for WM_APPCOMMAND messages?
    ... >> Is there an equivalent of the GetKeyNameText API for the WM_APPCOMMAND ... I need to create user-readable strings to use in my keyboard ... WM_APPCOMMAND has nothing directly to do with keyboard input. ...
    (microsoft.public.win32.programmer.ui)
  • Re: Suggested Alternative Unicode Implementation (for Rudy+ miscothers)
    ... Only if you were using strings other that UTF16 in your application. ... You want conversions? ... A versions of function prototypes as appropriate to an applications ... i.e. you have an ANSIString in your hand, you call the A API. ...
    (borland.public.delphi.non-technical)
  • Re: Compare words (not Strings)
    ... > In the core API, Strings can be checked either case sensitively are ... > use Soundex, Metaphone, or Levenshtein. ... > Soundex and Metaphone return Strings which are approximate pronunciations ... database might hold spices. ...
    (comp.lang.java.programmer)
  • Re: Pinvoke and Win32 level API permissions
    ... You should not call GetLastError when using PInvoke, ... | call to the HrESEBackupPreparefunction in the Exchange backup API. ...
    (microsoft.public.dotnet.framework)

Quantcast