Abstract
Due to the addition of C # and Visual Studio Tools, more and more people prefer to use C # To develop the GUI and then use MFC, however, in the past, too many programs have been developed using Visual C ++ 6.0 and MFC. At one time, it is impossible to change all C/C ++ code to C # later, therefore, organizing the business rule originally used in C/C ++ into a DLL for C # is not a good choice.
Introduction
Use environment: Visual Studio 2008
Step 1:
Create a project using Visual Studio 2008 and use the following Gui
Step 2:
Use(Reporter) How to Use Visual C ++ 6.0 to develop Win32 DLL? (C/C ++)Win32 DLL
Step 3:
Form1.cs/C #
1 Using System;
2 Using System. Collections. Generic;
3 Using System. componentmodel;
4 Using System. Data;
5 Using System. drawing;
6 Using System. LINQ;
7 Using System. text;
8 Using System. Windows. forms;
9 Using System. runtime. interopservices;
10
11 Namespace Pinvoke_win32_sum {
12 Public Partial Class Form1: FORM {
13 Public Form1 (){
14 Initializecomponent ();
15 }
16
17 [Dllimport ( " Win32_sum.dll " )]
18 Private Static Extern Int Sum ( Int X, Int Y );
19 Private Void Button#click ( Object Sender, eventargs e ){
20 Textbox3.text = Sum (convert. toint32 (textbox1.text), convert. toint32 (textbox2.text). tostring ();
21 }
22 }
23 }
9 rows
UsingSystem. runtime. interopservices;
When using the pinvoke (plateform invoke) method to use Win32 DLL, you must use the namespace system. runtime. interopservices.
17 rows
[Dllimport ("Win32_sum.dll")]
Private Static Extern IntSum (IntX,IntY );
How can I use VISUAL C ++ 6.0 to develop Win32 DLL? (C/C ++), we have placed the developed win32_sum.dll under the C: \ WINDOWS project. Win32 DLL does not need to be compiled like Com DLL, just put it in c: \ windows.
Declare the sum () function here, and use the dllimport attribute to inform C # To find the sum () function in win32_sum.dll. In addition, C # specifies that all functions of the primary ke must be static extern.
20 rows
Textbox3.text=Sum (convert. toint32 (textbox1.text), convert. toint32 (textbox2.text). tostring ();
Actually, this pinvoke function is used by the worker.
Results
Conclusion
When C # and C/C ++ are InterOP, the most important thing is the two alternative responses, which are as follows:
Unmanage type in wtypes. h |
Unmanaged C type |
Managed class name |
Managed C # type |
Description |
Handle |
Void * |
System. inptr |
N/ |
32 bits on 32-bit Windows operation systems, 64 bits on 64-bit Windows operation systems. |
Byte |
Unsigned char |
System. byte |
Byte |
8 bits |
Short |
Short |
System. int16 |
Short |
16 bits |
Word |
Unsigned short |
System. uint16 |
Ushort |
16 bits |
Int |
Int |
System. int32 |
Int |
32 bits |
Uint |
Unsigned int |
System. uint32 |
Uint |
32 bits |
Long |
Long |
System. int32 |
Int |
32 bits |
Bool |
Long |
System. int32 |
Int |
32 bits |
DWORD |
Unsigned long |
System. int32 |
Int |
32 bits |
Ulong |
Unsigned long |
System. int32 |
Int |
32 bits |
Char |
Char |
System. Char |
Char |
Decorate with ANSI |
Lpstr |
Char * |
System. string or system. Text. stringbuilder |
String |
Decorate with ANSI |
Lpcstr |
Const char * |
System. string or system. Text. stringbuilder |
String |
Decorate with ANSI |
Lpwstr |
Wchar_t * |
System. string or system. Text. stringbuilder |
String |
Decorate with ANSI |
Lpcwstr |
Const wchar_t * |
System. string or system. Text. stringbuilder |
String |
Decorate with ANSI |
Float |
Float |
System. Single |
Float |
32 bits |
Duoble |
Double |
System. Double |
Double |
64 bits |
Download the complete program
Pinvoke_win32_sum.7z
Reference
Msdn plateform invoke Data Types
Msdn built-in types table (C # reference)
See also
(Reporter) How to Use Visual C ++ 6.0 to develop Win32 DLL? (C/C ++)
The full text is complete.