C #. Net call the DLL code written in VB

Source: Internet
Author: User
Go to C #. Net to call the DLL compiled by VB. Code The example (I made some changes) is transferred from: how to call the DLL written in VB6 in http://www.taotechinfo.com/collect/2010222/n68565463.html. net.

Take C # as an example. Hope to help people who encounter problems.

This example is divided into two parts. First, generate the DLL of VB. Second, call in C.

Part 1:

1. Select the activx DLL project type in VB.

2. Modify the project name and class file name and add two functions, for example:

The code for convenient copy is as follows:

Public Function fnctestdllfordotnet (instr as string) as integer

Fnctestdllfordotnet = 9

End Function

Public Function fnctestdllfordotnet2 (instr as string) as integer

Fnctestdllfordotnet2 = 99

End Function

3. Select generate testdllproject1.dll from the File menu to generate the DLL.

The second part is as follows:

1. Open vs2005, select the Language C #, and set the project type to a window application.

Add a button to form. For example:

 

I added: Copy testdllproject1.dll to the System32 folder.

2. In Solution Explorer, right-click to reference and select Browse in the pop-up dialog box,Find testdllproject1.dll in system32, select it, and click OK.You can add it to the reference. For example:

After adding the DLL, you will see that the reference to this dll has been added to the reference,

3. Finally, write the code of the button to call the method in this vb dll.

Double-click the button on form and add the following code to the button's click event:

Testdllproject1.testclass1 Tc = new testdllproject1.testclass1 ();

String Ss = "KKKK ";

Int I = tc. fnctestdllfordotnet (ref SS );

Int I2 = tc. fnctestdllfordotnet2 (ref SS );

MessageBox. Show (I. tostring () + "" + i2.tostring ());

OK. Press Ctrl + Shift + B to compile the project, press F5 to run it, and click the button. For example, the result is displayed successfully.

 

To port testdllproject1.dll to another computer, copy it to the System32 folder of the machine, run the regsvr32 testdllproject1.dll command in "run" to register the component (some components do not need to be registered). In this way, the component can be successfully transplanted to other computers. Otherwise, an error "failed to retrieve components whose CLSID is {a string of characters} in the com class factory due to the following error: 80040154" will be reported"

 

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.