Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
I have a problem.
There is a C++ dll which has some functions in it (suppose - Addition of 2 nos.).
and i need to call this addition function from a C application.

Note: I dont want to add/link/refer the source code of C++ dll.
I want that the C Application will directly call the C++ dll from the specified path and use the "Addition()".

Iam using Visual C++ 2008 for all this.
Whatever soluton iam getting over internet, everywhere they tell us to add the reference of the dll project(dll source code)

My requirement is that-
We will only give our C++ dll to the client(who has his C application) and will tell him how he should provide the inputs to the C++ dll and how we are going to give him his output.

Please someone suggest me what i need to do...
What changes i need to do in C++ dll and How we should write the C application such that it can successfully call & use the C++ dll.
Iam a newbie in C/C++ so please help me.

Thanx in advance.
Dinesh kumar dora
Posted
Comments
[no name] 27-Apr-11 9:15am    
I wrote the code of the app and placed the dll file near the exe file
this is the code snippet of my test application:


#include

int __cdecl sum(int a,int b); // a function from a DLL

int main(VOID)
{
int Ret = 0;
HANDLE hLib = LoadLibrary("TestDLL.dll");
PFARPROC x = GetProcAddress(hLib,"sum");
Ret=x(5,3);
printf("Result (5+3)=%d",Ret);
FreeLibrary(hLib);
return 0;
}
and i got some errors:
error C2065: 'PFARPROC' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'x'
error C2065: 'x' : undeclared identifier

this is my C++ dll code snippet:
#include

#ifdef __cplusplus // If used by C++ code,
extern "C" { // we need to export the C interface
#endif

__declspec(dllexport) int __cdecl sum(int a1,int b1)
{
int result = 0;
result = a1 + b1;
return result;
}

#ifdef __cplusplus
}
#endif
Successfully built.
NOTE: No header files are included in app as well as in dll.Totally empty project
Please tell me whats wrong in this?

Please read: "How to mix C and C++"[^] (expecially you need to use the extern "C" construct in your DLL).
If you don't want implicitely link the DLL than you have to use, in the C application, the LoadLibrary function, see "Using Run-Time Dynamic Linking"[^] at MSDN.
 
Share this answer
 
Comments
Joan M 27-Apr-11 7:08am    
Good answer, my 5.
CPallini 27-Apr-11 7:44am    
Thank you.
Olivier Levrey 27-Apr-11 7:39am    
Have my 5 too.
CPallini 27-Apr-11 7:44am    
Thanks.
[no name] 27-Apr-11 7:42am    
My Five++
An example to use DLL files.
HANDLE hLib = LoadLibrary("math.dll");
PFARPROC x = GetProcAddress(hLib,"Addnumbers");
x(23,34);
FreeLibrary(hLib);
 
Share this answer
 
Comments
[no name] 27-Apr-11 7:10am    
can we use LoadLibrary() in C code?
LaxmikantYadav 27-Apr-11 7:18am    
Yes, It is defined in CRT
Olivier Levrey 27-Apr-11 7:41am    
Have a 5 Debojyoti.
I just suggest to cast the returned value of GetProcAddress with the appropriate type.

You need to include windows.h to use LoadLibrary.
[no name] 27-Apr-11 7:50am    
ok
iam little bit confused abt all this..
can anyone just post me a sample dll and sample c app
so that i can refer it..plz
[no name] 27-Apr-11 8:33am    
I tried your suggestion and iam getting some errors

I wrote the code of the app and placed the dll file near the exe file

this is the code snippet of my test application:
#include "windows.h"

int __cdecl sum(int a,int b); // a function from a DLL
int main(VOID)
{

int Ret = 0;
HANDLE hLib = LoadLibrary("TestDLL.dll");

PFARPROC x = GetProcAddress(hLib,"sum");
Ret=x(5,3);

printf("Result (5+3)=%d",Ret);
FreeLibrary(hLib);
return 0;
}

and i got some errors:
error C2065: 'PFARPROC' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'x'
error C2065: 'x' : undeclared identifier

this is my C++ dll code snippet:

#include "windows.h"

#ifdef __cplusplus // If used by C++ code,

extern "C" { // we need to export the C interface
#endif

__declspec(dllexport) int __cdecl sum(int a1,int b1)
{ int result = 0;

result = a1 + b1;
return result;
}
#ifdef __cplusplus
}#endif
Successfully built.

NOTE: No header files are included in app as well as in dll.Totally empty project


Please tell me whats wrong in this?
.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900