473,796 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ 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
+EnumProcessMod ules
+GetModuleFileN ameExA
+GetProcessMemo ryInfo
+VirtualQueryEx
+GetSystemInfo
+CreateToolhelp Snapshot
+ProcessFirst
+ProcessNext
+ImpersonateLog gedOnUser
+OpenProcessTok en
+RevertToSelf
+GetUserName
+GetUserNameEx

Thanks in advance

José


Nov 15 '05 #1
8 5331
"José Achig" <jo*******@hotm ail.com> wrote in message
news:e$******** ********@TK2MSF TNGP12.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
+EnumProcessMod ules
+GetModuleFileN ameExA
+GetProcessMemo ryInfo
+VirtualQueryEx
+GetSystemInfo
+CreateToolhelp Snapshot
+ProcessFirst
+ProcessNext
+ImpersonateLog gedOnUser
+OpenProcessTok en
+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("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
internal static extern IntPtr OpenProcess(int dwDesirdedAcces s, bool
bInheritHandle, int dwProcessId);

[DllImport("psap i.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
internal static extern bool EnumProcesses(i nt[] lpidProcess, int cb, out
int cbNeeded);
}

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

pids = new int[1024];

if (NativeMethods. EnumProcesses(p ids, 1024, out bytesNeeded)) {
// returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLi ne("PID{0}: {1}",i, pids[i]);
}
} else {
// got error
Console.WriteLi ne("NativeMetho ds.EnumProcesse s() 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("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
internal static extern IntPtr OpenProcess(int dwDesirdedAcces s, bool
bInheritHandle, int dwProcessId);

[DllImport("psap i.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
internal static extern bool EnumProcesses(i nt[] lpidProcess, int cb, out
int cbNeeded);
}

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

pids = new int[1024];

if (NativeMethods. EnumProcesses(p ids, 1024, out bytesNeeded)) {
// returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLi ne("PID{0}: {1}",i, pids[i]);
}
} else {
// got error
Console.WriteLi ne("NativeMetho ds.EnumProcesse s() 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.1 CAC1BE4@....
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("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)] internal static extern IntPtr OpenProcess(int dwDesirdedAcces s, bool bInheritHandle, int dwProcessId);

[DllImport("psap i.dll", CharSet=CharSet .Auto, SetLastError=tr ue)] internal static extern bool EnumProcesses(i nt[] lpidProcess, int cb, out int cbNeeded);
}

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

pids = new int[1024];

if (NativeMethods. EnumProcesses(p ids, 1024, out bytesNeeded)) { // returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLi ne("PID{0}: {1}",i, pids[i]); }
} else {
// got error
Console.WriteLi ne("NativeMetho ds.EnumProcesse s() 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.1 CAC1BE4@....
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("kern el32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)] internal static extern IntPtr OpenProcess(int dwDesirdedAcces s, bool bInheritHandle, int dwProcessId);

[DllImport("psap i.dll", CharSet=CharSet .Auto, SetLastError=tr ue)] internal static extern bool EnumProcesses(i nt[] lpidProcess, int cb, out int cbNeeded);
}

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

pids = new int[1024];

if (NativeMethods. EnumProcesses(p ids, 1024, out bytesNeeded)) { // returned true;
for(int i=0; i<(bytesNeeded/4); i++) {
Console.WriteLi ne("PID{0}: {1}",i, pids[i]); }
} else {
// got error
Console.WriteLi ne("NativeMetho ds.EnumProcesse s() 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.Manageme nt
classes.
These classes are wrapping WMI, and the the WMI class "Win32_Proc ess" and
associated classes can be used to enumerate processes and get a lot of
process properties.

WIlly.

"José Achig" <jo*******@hotm ail.com> wrote in message
news:e$******** ********@TK2MSF TNGP12.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
+EnumProcessMod ules
+GetModuleFileN ameExA
+GetProcessMemo ryInfo
+VirtualQueryEx
+GetSystemInfo
+CreateToolhelp Snapshot
+ProcessFirst
+ProcessNext
+ImpersonateLog gedOnUser
+OpenProcessTok en
+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
1522
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 stated in the readme guide provided, to use the library, we need to call APIs defined inside the library. In the <mylib.h> file, there are defined APIs as follows:
3
5099
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" is provided at the end of the chapter on 'Database Restore'. Now, what are these APIs and how can I use them? TIA Raquel.
11
2253
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 too. That was not the idea, no? Does anyone know how to fix this? Or is it not supported (despite being described in the DB2 help)? Regards Peter Arrenbrecht Opus Software AG
5
3548
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 function calls) are part of the (Unix) system calls. The cause of my confusion is that for example stdio.h is considered
3
5029
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 thinking about starting a disk defragger project and want to know where to get started. of course i have a long way to go before i can move blocks around and end up with a drive that still has valid NTFS data on it :-) but i want to at least confirm...
3
13725
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 set them as
3
3862
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 display a form opens on, etc. Are there objects/APIs in .NET that support this? I've trolled through the object browser, but haven't been able to find anything. Either they aren't there, or I'm not looking for the right words. Any advice welcomed.
1
2479
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 in chunks from the socket and then passes on the decompressed data to the user. I am makeing use of zlib APIs to decompress the data read from the server.
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2925
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.