(Reporter) How to Use C # To use Win32 DLL? (. NET) (C #) (Windows form)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.