Visual C + + is written for VB. NET calls to the DLL.

Source: Internet
Author: User
Tags function prototype

Like me, many beginners are really hard to write out title DLLs, even if written out often in the process of invoking a variety of problems. I wrote a DLL based on groping, compiling and calling the code to get through.

The following is an example of a brief explanation, and I will continue to study the problem.

1. Create a new WIN32 console DLL project with Visual C + + (remember to tick the empty items) such as: MyDll1

2. Add Mydll1.h in header file

#pragma<iostream>// function prototype int __stdcall Add (intint b); void __stdcall  shuchu ();

3. Add Mydll1.cpp in Source file

#include"MyDLL1.h"using namespacestd;int__stdcall Add (intAintb) {    returnA +b;}void__stdcall Shuchu () {unsignedLongi =1; unsignedLongUlnum =50000;//Cycle 50,000 Times         while(Ulnum! =0) {cout<<"The Ulnum is:"<< I <<Endl; Ulnum--; I++; }}

4. Under the Project menu, modify the configuration of the Mydll1 property as follows (note boldface bold section):

5. First compile the project once and get a Mydll1.map file that includes the name decoration of the DLL file member function in this file

Because as a beginner or even a veteran expert, it is difficult to figure out the rules for member function name modification, so you have to rely on the compiler itself to figure out the decorated name.

6. Edit the Mydll1.def file as follows:

Libraryexports   Add = [email protected]@[email protected]    Shuchu = [email protected] @YGXXZ    

7. Compile the project once again.

This completes a Mydll1.DLL file that can be called by the vb.net, and copies the file into the \Bin\Debug folder under the VB.net project you want to invoke.

8. VB. NET console project is as follows (the specific function is no longer detailed):

 Private Declare FunctionAddLib "MyDll1.dll"(ByValA as Integer,ByValB as Integer) as Integer Private Declare SubShuchuLib "MyDll1.dll" ()    SubMain ()DimA1 as Integer        DimS1 asDateTime, S2 asDateTimeDimT1 asTimeSpan, T2 asTimeSpanDimI as Long=1        DimUlnum as Long=50000        '==========================================================S1 =Now Shuchu () S2=Now T1=S2. Subtract (S1). Duration'===========================================================S1 = Now while(Ulnum <>0) Console.WriteLine ("The Ulnum is:"&Str(i)) Ulnum-=1I+=1        End  whileS2=Now T2=S2. Subtract (S1). Duration Console.WriteLine (T1. totalseconds.tostring) Console.WriteLine (T2. totalseconds.tostring)'================================================================A1 = Add ( +, the) Console.WriteLine (A1. ToString) Console.ReadLine ()End Sub


9. Modify VB.net project Properties/compile/target CPU to x86

10. Compile and run successfully!

Through the running results of this vb.net program, we will find that: invoking the C + + function is not as efficient as the code that vb.net the same function, which is why?

Visual C + + is written for VB. NET calls to the DLL.

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.