![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Intermediate
Win32 API: C++ to .NETBy kbrryderThis is a conversion sheet to go from C++ API calls to .NET P/Invoke. It includes data type conversions and tips. |
C++/CLI, C, C#, VB, eVC, VC6, VC7, VC7.1, VC8.0, Win Mobile, .NET, Mobile, Win2003, Vista, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I am writing an article to give a reference to developers who need to use API imports in their .NET programs to call C++ functions. I haven't found a single site that looks good or is totally complete with all the data types or has them converted correctly. So, I decided to write one. I also recommend using any new .NET function that does the same thing as an API if it is available. At least 75% of the API has already been converted to .NET and is available in various classes. This is because they usually work better and look nicer. If you have any questions send me an email or post them to the message board.
Declare Function <Name> Lib <dll name> <Optional fields> (<params>) _
As <return type>
[DllImport("<dll name>", <optional fields>)]
static extern <return type> <Method name> (<Params>)
[DllImport("<Dll name>",<optional fields>)]
static <return type> <Function name>( <Params>);
Windows API reference for C#, VB.NET & VB6 - This site has every major API declaration for VB.NET, C#.NET and some VB6.
Here is a program I have made that uses these sites:
Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA"(_
ByVal dwFlags As Integer, ByRef lpSource As Object, _
ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, _
ByVal lpBuffer As String, ByVal nSize As Integer, _
ByRef Arguments As Integer) As Integer
[DllImport("kernel32.dll", SetLastError=true)]
static extern int FormatMessageA ( int dwFlags, ref object lpSource,
int dwMessageId, int dwLanguageId, string lpBuffer,
int nSize, ref int Arguments)
[DllImport("KERNEL32.DLL",EntryPoint="MoveFileW",
SetLastError=true,CharSet=CharSet::Unicode,ExactSpelling=true,
CallingConvention=CallingConvention::StdCall)]
static bool MoveFile( String^ src, String^ dst );
DWORD* = PDWORD.
UIntPtr are not CLS compliant so I use IntPtr instead, but you can use any of these.
string for WCHAR*, TCHAR*, etc. For outputs you can use string or StringBuilder, but sometimes you need to use an IntPtr and marshal the characters out of it using Marshal.PtrToStructure() and increment the pointer till you get null characters. To increment a pointer turn it into an int and increase the size each time by the size of the type you get from Marshal.PtrToStructure() each time. E.g. : pointer += Marshal.SizeOf(<last object you got back>);
int could be used for a uint.
IntPtr back to an int or some other class, use Marshal.PtrToStructure() or some other IntPtr method.
unsafe keyword and use the /unsafe compiler option.
IntPtr in an API call you can use a HandleRef type.
structs for API, make sure they have the StructLayout.Sequential attribute. Also sometimes you may need to change the packing on a struct to make it work right, but usually you don't.
IntPtr possibly.
IntPtr or the data type it points to, sometimes you can just say ref <datatype> or out <datatype>, but unless it's an input char * you need to use IntPr for inputs and ref IntPtr for outputs.
Marshal and MarshalAs classes only when you have to, as they take up more processing power in certain situations. |
Term |
Description |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
|
|
|
.NET =
|
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ =
|
|
|
.NET = C++ =
|
|
|
.NET = C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET =
C++ =
C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET = C++ = |
|
|
.NET =
|
|
|
.NET = C++ = |
|
|
.NET = standard is default, look at the
|
|
|
.NET = C++ = |
|
|
.NET = C++ = |
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Mar 2005 Editor: Rinish Biju |
Copyright 2005 by kbrryder Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |