C#/vb calling C-written standard DLL

Source: Internet
Author: User

Get ready:

First Open VC + + 6.0 new project, select Win32 Dynamic link-library, named Stdlibrary

Create a new Library.cpp file with the following content

#include <stdio.h><windows.h>BOOL apientry DllMain (hmodule hmodule,                       DWORD  ul_ Reason_for_call,                       lpvoid lpreserved) {    return  TRUE;} HRESULT __stdcall  testadd (intintreturn i + j;}

The new text file is then named Export.def, which reads as follows

LIBRARY "Stdlibrary"

Exports
Testadd @1

Compile project Get StdLibrary.dll

1 VB Call StdLibrary.dll

New Standard EXE project, drag interface,

Right-click on the code and write the following code

Private Declare FunctionTestaddLib "StdLibrary.dll"(ByValInX as Long,ByValIny as Long) as LongPrivate SubCmdcaclute_click ()DimX as LongDimY as LongDimResult as Longx=txtx.texty=Txty.textresult=testadd (x, y) txtresult.text=resultEnd Sub

Then use VB to generate EXE, drag the StdLibrary.dll to the project in the compiled directory, run can, call success.

Note: VB calls the third-party DLL, the debugging process Lib "StdLibrary.dll" if not the absolute path will be error, do not know whether VB Bug, as long as the generation of EXE after the run will not error

2 C # Call StdLibrary.dll

Write the following code and drag the StdLibrary.dll to the Debug/release directory

usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespacecsharpcallclibrary{classProgram {[DllImport ("StdLibrary.dll", CallingConvention =Callingconvention.stdcall)] Public Static extern intTestadd (intXinty); Static voidMain (string[] args) {            intx =1, y =2, result=0; Result=testadd (x, y); Console.WriteLine ("{0} + {1} = {2}", X,y,result);        Console.read (); }    }}

Run compile-generated EXE, this call succeeds

C#/vb calling C-written standard DLL

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.