473,491 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

working with system apis

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é


Nov 15 '05 #1
8 5311
"José Achig" <jo*******@hotmail.com> wrote in message
news:e$****************@TK2MSFTNGP12.phx.gbl...
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
Nov 15 '05 #2
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[i]);
}
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed");
}

return 100;
}
}

Nov 15 '05 #3
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 [MSFT]" wrote:

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[i]);
}
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed");
}

return 100;
}
}

Nov 15 '05 #4
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/de...tml/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." <unlisted@.> wrote in message news:403BAAF3.1CAC1BE4@....
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 [MSFT]" wrote:

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[i]); }
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed"); }

return 100;
}
}

Nov 15 '05 #5
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 [MVP - C#/.NET]" wrote:

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/de...tml/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." <unlisted@.> wrote in message news:403BAAF3.1CAC1BE4@....
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 [MSFT]" wrote:

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[i]); }
} else {
// got error
Console.WriteLine("NativeMethods.EnumProcesses() failed"); }

return 100;
}
}

Nov 15 '05 #6
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
Nov 15 '05 #7
Search the Win32 SDK *.h files.
Nov 15 '05 #8
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.

"José Achig" <jo*******@hotmail.com> wrote in message
news:e$****************@TK2MSFTNGP12.phx.gbl...
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é

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1501
by: Tanuki | last post by:
Hi All: I am doing some development in VC++ using some llibraries downloaded from the web. The library comes in 3 files <mylib.dll>, <mylib.lib>, <mylib.exp> and I also have <mylib.h>. As...
3
5080
by: Raquel | last post by:
A very basic question to all the esteemed listers. I am going through UDB manual. At the end of each chapter, "DB2 APIs" are listed. For example a multi-page description of the DB2 API "db2Restore"...
11
2224
by: Peter Arrenbrecht | last post by:
We have a setup with a v7 client accessing a v8 UDB server running on AIX. When I call SQLCancel from a second thread on a long running query, SQLCancel itself simply waits for statement completion...
5
3503
by: markus | last post by:
Hi, I have a question that deals with the standard c library VS (Unix) system calls. The question is: which header files (and functions) are part of the C library and which header files (and...
3
5003
by: Aaron Oxford | last post by:
hi all, hope you can help me with this question i've been scouring the net for a couple of days now :-(. is there any way to access the logical blocks on an ntfs volume from within ..NET? i'm...
3
13697
by: TC | last post by:
Hey Folks, I am using the following 4 Win32 APIs with a C# AddIn: FindWindow SetWindowLong SetForegroundWindow EnableWindow Within the AddIn, there are some winforms. I use these APIs to...
3
3826
by: Steve Marshall | last post by:
I'm looking at developing an application which would benefit from being able to work with 2 display monitors. But how do I work with multiple displays? I'd like to be able to control which...
1
2441
by: nagrik | last post by:
Hello Group, I am writing a http client and reading a web page from the server. The page can be compressed in any of the format namely; gzip or deflate or compress. My client reads the page...
0
7190
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.