Visual C + + is written for VB. Dll__c++ of the net call

Source: Internet
Author: User
Tags function prototype
Visual C + + is written for VB. NET called DLL
Recently in the use of VB for C + + DLL module to write an interface, see this article is very good, reprinted over
Point I entered the original

Like me, many beginners are really very difficult to write the title of the DLL, even if the writing is often in the process of invoking a variety of problems. I wrote a DLL based on my groping, compiling and invoking the code to live through it.

Here are some examples to illustrate, there are a lot of problems I will continue to study.

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

2. Add Mydll1.h to header file

#pragma once 
#include <iostream>
//function prototype
int __stdcall Add (int a, int b);
void __stdcall  Shuchu ();

3. Add Mydll1.cpp to source file

#include "MyDLL1.h"
using namespace std;
int __stdcall Add (int a, int b)
{return
  a + b;
}
void __stdcall  Shuchu ()
{
  unsigned long i = 1;
  unsigned long ulnum = 50000;  Loop 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 the boldface bold character section):

5. Compile the project first and get a Mydll1.map file containing the name modification of the DLL member function

Because as a beginner or even as a senior expert, it's difficult to figure out the rules of the 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:

LIBRARY
exports
   Add =? add@ @YGHHH @z 
   Shuchu =? shuchu@ @YGXXZ    

7. Compile the project once again.

This completes a Mydll1.DLL file that can be invoked by 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 (specific function no longer detailed):

Private Declare Function Add Lib "MyDll1.dll" (ByVal A As Integer, ByVal b As Integer) As Integer private Declare Sub Shuc Hu Lib "MyDll1.dll" () Sub Main () Dim A1 As Integer Dim S1 as datetime, S2 as DateTime Dim T1 as TimeSpan, T2 as TimeSpan Dim i As Long = 1 Dim ulnum As Long = 50000 ' ========================================================== S 1 = Now Shuchu () S2 = Now T1 = S2. Subtract (S1). Duration ' =========================================================== S1 = Now while (ulnum <> 0) Consol E.writeline ("The Ulnum is:" & Str (i)) ulnum-= 1 i + = 1 End While S2 = Now T2 = S2. Subtract (S1). Duration Console.WriteLine (T1. totalseconds.tostring) Console.WriteLine (T2). totalseconds.tostring) ' ================================================================ a1 = Add (a) Console.Wr Iteline (A1. ToString) Console.ReadLine () End Sub

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

10. Compilation run successfully.

By running the results of this vb.net program, we find that the efficiency of invoking C + + functions is not as efficient as the code that vb.net the same functionality.

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.