Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function from a DLL which is developed in C++ from C

Tags:

c++

c

dll

The function in dll has the prototype below

void Foo(int arg1, int& arg2);

The question is, how to declare the function prototype in C?

Is the declaration legal?

void Foo(int, int*);
like image 681
Sy Yuan Li Avatar asked Aug 30 '17 06:08

Sy Yuan Li


People also ask

What is DLL in C programming?

A DLL is a library that contains code and data that can be used by more than one program at the same time. For example, in Windows operating systems, the Comdlg32 DLL performs common dialog box related functions. Each program can use the functionality that is contained in this DLL to implement an Open dialog box.

Can I use a C++ DLL in C #?

Yes, it will work. Just make sure you reference the library in your c++ library and link them together. In some cases, this is not possible, e.g. if there is a considerable existing codebase written in C, that needs to be extended with new functionality (which is to be written in C++).


1 Answers

Is the declaration legal?

It is, but it doesn't declare the same function. If you need a C API, you cannot use a reference. Stick to a pointer, and make sure the function has C linkage:

extern "C" void Foo(int, int*) {
   // Function body
}

If you cannot modify the DLL code, you need to write a C++ wrapper for it that exposes a proper C API.

like image 116
StoryTeller - Unslander Monica Avatar answered Oct 15 '22 10:10

StoryTeller - Unslander Monica