First, we recommend a book, "proficient in. NET interoperability," which is the most exhaustive book in Chinese literature.
Do system integration project students should have dealings with equipment (such as video equipment: Hoi Hong, Dahua, etc.), in most cases these equipment manufacturers will give the system Integration Vendor Development Protocol (DLL class library, function definition header file, test program, call process), the DLLs of these protocols are mostly developed in C + +, So if we integrate with C #, we have to use P/invoke technology (one of the. NET interop)
A simple example of P/invoke
P/invoke plainly, you call the protocol DLL's function, passing in the correct parameters (note C + + and C # data type conversion)
Example:
1 usingSystem.Runtime.InteropServices;2 3 classApp4 {5[DllImport ("Msvcrt.dll")]6 Static extern intPutsstringmsg);7 8[DllImport ("Msvcrt.dll")]9 Static extern int_flushall ();Ten One Static voidMain () A { -Puts"Hello World"); - _flushall (); the Createmsgwindow (); - } - - +[DllImport ("user32.dll", entrypoint="MessageBox")] - Static extern intMessageBox (inthwndstringLptext,stringLpcaption,intwtype); + A Static voidCreatemsgwindow () at { -MessageBox (0,"Hello world!","Welcome",0); - } -}
View Code
Two, two DLL function view tool
1. Dll Export Viewer
2. Dll Function Viewer 2.0
Third, C # and C + + common types comparison
C + + Types |
C # types |
HANDLE (void *) is an equivalent type within parentheses |
IntPtr |
Byte (unsigned char) |
Byte |
Short (short) |
Short |
WORD (unsigned short) |
Short |
int (int) |
Int |
int* |
ref int |
UINT (unsigned int) |
Int |
Long (long) |
Int |
ULONG (unsigned long) |
UInt |
DWORD (unsigned long) |
UInt |
BOOL (Long) |
bool |
Pcahr (char *) |
String |
Char[] |
String |
Pbyte (BYTE *) |
Byte[] |
Structural body |
publlic struct struct {} |
char * * Variable name |
Ref string variable Name |
Structural Package
1 //struct in the protocol header file2typedefstruct 3 { 4 Charslibname[ the ]; 5 Charspathtolibrary[ the ]; 6 INT32 ientries; 7 INT32 iused; 8 UINT16 Isort; 9 UINT16 iversion; Ten BOOLEAN fcontainssubdirectories; One INT32 ireserved; A } Libheader; - - //the corresponding structure in C # protocol wrapping class the [StructLayout (layoutkind.sequential)] - Public structLibheader - { -[MarshalAs (UnmanagedType.ByValArray, SizeConst = the)] + Public Char[] slibname; -[MarshalAs (UnmanagedType.ByValArray, SizeConst = the)] + Public Char[] spathtolibrary; A Public intientries; at Public intiused; - Public ushortIsort; - Public ushortiversion; - Public BOOLfcontainssubdirectories; - Public intireserved; -}
View Code
Report:
1. Proficient in. NET interoperability PDF and ISO downloads
2. Dll Export Viewer Download
3. Dll Function Viewer 2.0 download
. NET Interop