working with system apis

  • Thread starter Thread starter José Achig
  • Start date Start date
J

José Achig

Hi

I have to use functions inside of system apis so I want the correct
dllimports declarations in c# for the following functions or information
about where I can get these statements.

+ OpenProcess
+ CloseHandle
+ EnumProcesses
+EnumProcessModules
+GetModuleFileNameExA
+GetProcessMemoryInfo
+VirtualQueryEx
+GetSystemInfo
+CreateToolhelpSnapshot
+ProcessFirst
+ProcessNext
+ImpersonateLoggedOnUser
+OpenProcessToken
+RevertToSelf
+GetUserName
+GetUserNameEx

Thanks in advance

José
 
José Achig said:
Hi

I have to use functions inside of system apis so I want the correct
dllimports declarations in c# for the following functions or information
about where I can get these statements.

+ OpenProcess
+ CloseHandle
+ EnumProcesses
+EnumProcessModules
+GetModuleFileNameExA
+GetProcessMemoryInfo
+VirtualQueryEx
+GetSystemInfo
+CreateToolhelpSnapshot
+ProcessFirst
+ProcessNext
+ImpersonateLoggedOnUser
+OpenProcessToken
+RevertToSelf
+GetUserName
+GetUserNameEx

Thanks in advance

José

This site doesn't explicitly give DLLImport declarations, but they shouldn't
be too difficult to derive:

http://www.mentalis.org/apilist/apilist.php

Erik
 
Here is an example of how you can create PInvoke signatures for these
native methods. I found which DLL had the exported function by searching
MSDN.

Hope this example helps.

using System;
using System.Runtime.InteropServices;

internal class NativeMethods {
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern IntPtr OpenProcess(int dwDesirdedAccess, bool
bInheritHandle, int dwProcessId);

[DllImport("psapi.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool EnumProcesses(int[] lpidProcess, int cb, out
int cbNeeded);
}

public class PClass {
public static int Main() {
int[] pids;
int bytesNeeded;

pids = new int[1024];

if (NativeMethods.EnumProcesses(pids, 1024, out bytesNeeded)) {
// returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLine("PID{0}: {1}",i, pids);
}
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed");
}

return 100;
}
}
 
It would have been nice (and not asking too much) if MS had created an
additional namespace that contained all the Win32 APIs.

The current way makes porting a real pita.
 
You're right that it's not easy but there is a good reason for that IMHO.
With .NET, Microsoft is actively discouraging people from writing unmanaged
code and instead using managed APIs for obvious reasons. While there are
some valid reasons for going to the Win32 API, they are in fact few in
comparison to going out from VB 6 for instance (multimedia is an obvious
exception). I would guess this was not done so that people stay mostly with
writing "pure" managed code and program one way to the .NET Framework SDK.
This results in many benefits. Of course, I have no insight into their
decision but this would be my take.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
..NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------
Julie J. said:
It would have been nice (and not asking too much) if MS had created an
additional namespace that contained all the Win32 APIs.

The current way makes porting a real pita.

Jeff Schwartz said:
Here is an example of how you can create PInvoke signatures for these
native methods. I found which DLL had the exported function by searching
MSDN.

Hope this example helps.

using System;
using System.Runtime.InteropServices;

internal class NativeMethods {
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern IntPtr OpenProcess(int dwDesirdedAccess, bool
bInheritHandle, int dwProcessId);

[DllImport("psapi.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool EnumProcesses(int[] lpidProcess, int cb, out
int cbNeeded);
}

public class PClass {
public static int Main() {
int[] pids;
int bytesNeeded;

pids = new int[1024];

if (NativeMethods.EnumProcesses(pids, 1024, out bytesNeeded)) {
// returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLine("PID{0}: {1}",i, pids);
}
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed");
}

return 100;
}
}
 
I don't have a problem w/ writing (only) to the .NET api, the problem is that
there is a lot of functionality that just isn't there.

For example, how do you do MessageBeep in .NET? You can't.

When and if MS gets around to creating a complete framework, the P/Invoke will
largely be unneeded for Win32 functionality. Unfortunately, by that time,
there will be enough 3rd party or other related libs that provide the missing
functionality that existing code managability will be seriously compromised.

Microsoft should have completed the framework *before* releasing it. Big
mistake, but MS support for the developer has been on a downhill slope since
shortly after Win 95 was released.

Sam Gentile said:
You're right that it's not easy but there is a good reason for that IMHO.
With .NET, Microsoft is actively discouraging people from writing unmanaged
code and instead using managed APIs for obvious reasons. While there are
some valid reasons for going to the Win32 API, they are in fact few in
comparison to going out from VB 6 for instance (multimedia is an obvious
exception). I would guess this was not done so that people stay mostly with
writing "pure" managed code and program one way to the .NET Framework SDK.
This results in many benefits. Of course, I have no insight into their
decision but this would be my take.

--
--------------------------------------------------------------
Sam Gentile [C#/.NET MVP]
.NET Blog http://samgentile.com/blog/
MSDN Column:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/bridge.asp
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
---------------------------------------------------------------
Julie J. said:
It would have been nice (and not asking too much) if MS had created an
additional namespace that contained all the Win32 APIs.

The current way makes porting a real pita.

Jeff Schwartz said:
Here is an example of how you can create PInvoke signatures for these
native methods. I found which DLL had the exported function by searching
MSDN.

Hope this example helps.

using System;
using System.Runtime.InteropServices;

internal class NativeMethods {
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern IntPtr OpenProcess(int dwDesirdedAccess, bool
bInheritHandle, int dwProcessId);

[DllImport("psapi.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool EnumProcesses(int[] lpidProcess, int cb, out
int cbNeeded);
}

public class PClass {
public static int Main() {
int[] pids;
int bytesNeeded;

pids = new int[1024];

if (NativeMethods.EnumProcesses(pids, 1024, out bytesNeeded)) {
// returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLine("PID{0}: {1}",i, pids);
}
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed");
}

return 100;
}
}
 
Thanks to all for the help

All controversies are welcome. In my case, I did a windows service and I
need to use many functions inside of these system's api (of course it
depends of what are you going to do)
Really is a headache the fact of declaring these statements <dllimport>
correctly. I hope do this well.

Thank you again and good luck!!!

José Achig
 
Not sure why you need to call these API's, or what you want to achieve ,but
I'm sure there is a Managed way to achieve what you need, just take some to
look what's included in the FCL, more specifically the System.Management
classes.
These classes are wrapping WMI, and the the WMI class "Win32_Process" and
associated classes can be used to enumerate processes and get a lot of
process properties.

WIlly.
 
Back
Top