One call format
C # has a special invocation process when calling the WindowsAPI function interface
First we must refer to the namespace when calling the API function interopservices
Using System.Runtime.InteropServices;
For example, we want to invoke the AllocConsole () Console function interface in an interface function in the Kernel32.dll dynamic Library of Windows
The following is the simplest form of invocation to declare the invocation of this function interface (empty method, that is, the method body is empty)
[DllImport ("kernel32.dll")]
public static extern bool AllocConsole ();
Below we can add a field with the DllImportAttribute feature to further illustrate
1 callingconvention
Indicates the callingconvention value used when passing method parameters to an unmanaged implementation
callingconvention.cded: The caller cleans up the stack. He uses you to call a function with varargs.
Callingconvention.stdcall: The callee cleans up the stack. He is the default convention for calling unmanaged functions from managed code
2 CharSet
Controls the name version of the calling function and indicates how to marshal the string parameter to the method
CharSet.Ansi: All strings are converted to Ansi strings, and the letter "A" is appended to the name of the DLL Enterpoint
CharSet.Unicode: All string arguments are converted to Unicode characters before being passed to the unmanaged implementation, and the letter "W" is appended to the name of the DLL Enterpoint
CharSet.Auto: This conversion is platform-related (for example, in Windows NT Upper Unicode, and ANSI on Windows 98).
The default value for CharSet is ANSI
3 Enterpoint
Indicates the name or ordinal of the DLL entry point to invoke
If your method name does not want to have the same name as the API function, be sure to specify this parameter
For example, I want to call the MessageBox function in User32.dll, but I want to name it with my name MsgBox.
[DllImport ("User32.dll", charset= "CharSet.Auto",enterpoint= "MessageBox")]
public static extern int MsgBox (IntPtr hwnd,string txt,string caption,int type);
4 exactspelling
Indicates whether the name of the entry point in the unmanaged DLL should be modified, corresponding to the charset value specified in the CharSet field.
If true, the letter "A" is appended to the method name when the DllImportAttribute.CharSet field is set to the ANSI value of CharSet. When the DllImportAttribute.CharSet field is set to the Unicode value of charset, the letter "W" is appended to the method name, and the default value for this field is False
5 PreserveSig
Indicates that the managed method signature should not be converted to a return HRESULT, and there may be an unmanaged signature for the additional [out,retval] parameter that corresponds to the return value
6 Serlasterror
Instructs the called method to call the Win32 API SetLastError before returning the value from the attributed method.
True indicates that the caller will call SetLastError, which defaults to false. The runtime marshaler calls GetLastError and caches the returned values in case other API calls are overridden.
Two-parameter type conversions
| C++ |
C# |
| DWORD |
Int |
| Word |
Int16 |
| String pointer type |
String |
| Handle (handle, HWND) |
IntPtr |
| Structure or class |
To first qualify the declaration structure or class with the StructLayout attribute |
1 Explicit
Used to control the exact location of each data member. With explicit, each member must use FieldOffsetAttribute to indicate the position of this field in the type:
[StructLayout (layoutkind.explicit, size=16, Charset=charset.ansi)]
public class MySystemTime
{
[FieldOffset (0)]public ushort wyear;
[FieldOffset (2)]public ushort Wmonth;
[FieldOffset (4)]public ushort Wdayofweek;
[FieldOffset (6)]public ushort Wday;
[FieldOffset (8)]public ushort Whour;
[FieldOffset (]public) ushort Wminute;
[FieldOffset (]public) ushort Wsecond;
[FieldOffset]]public ushort wmilliseconds;
}
2 Sequential
Used to force the layout of members sequentially in the order in which they appear
For example, for the OSVERSIONINFO structure in the API, examples of defining classes or structs in. NET are as follows
API prototypes
* The original structure declaration is defined in the API
* Osversioninfoa STRUCT
* dwOSVersionInfoSize DWORD?
* dwMajorVersion DWORD?
* dwMinorVersion DWORD?
* dwBuildNumber DWORD?
* dwPlatformId DWORD?
* szCSDVersion BYTE to DUP (?)
* Osversioninfoa ENDS
*
* osVersionInfo equ <OSVERSIONINFOA>
The following is declared in. net
[StructLayout (LayoutKind.Sequential)]
public class osVersionInfo
{
public int osversioninfosize;
public int majorversion;
public int minorversion;
public int BuildNumber;
public int platformid;
[MarshalAs (UnmanagedType.ByValTStr, sizeconst=128)]
Public String versionstring;
}
Note: When a struct is a parameter, it is generally preceded by a ref modifier, otherwise an error occurs: The object's reference does not have an instance of the specified object
In the original, please visit: http://www.jb51.net/article/46041.htm
C # calls the WINDOWSAPI function