I am still trying to resolve the problem I described in this thread where I basically have a DLL written in native C and want to develop a wrapper function for it and call the C functions from within a C++ application. The solution described in that thread works, but it requires me to modify my original C DLL's files (which I don't want to because I have a lot of them, and not all of them compile on Windows), so I am trying to find a way that allows me to make direct calls to the functions of the DLL without having to dllexport/import...etc.

I found this and I thought I could try it out. I did basically the same thing as before.

My DLL is coded as follows

#include "addition.h"
#include <stdio.h>

int add(int x, int y){
return x+y;
}

My wrapper project, hereafter I'm calling it MyProject consists of just MyProject.cpp:

#include <Windows.h>
#include <iostream>

using namespace std;

int CallMyDLL(void) 
{ 
    /* get handle to dll */ 
   HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\AdditionDLL.dll"); 

   /* get pointer to the function in the dll*/ 
   FARPROC lpfnGetProcessID = GetProcAddress(HMODULE (hGetProcIDDLL),"add"); 

   /* 
      Define the Function in the DLL for reuse. This is just prototyping the dll's function. 
      A mock of it. Use "stdcall" for maximum compatibility. 
   */ 
   typedef int (__stdcall * pICFUNC)(int, int); 

   pICFUNC add; 
   add = pICFUNC(lpfnGetProcessID); 

   /* The actual call to the function contained in the dll */ 
   int intMyReturnVal = add(4, 5); 

   /* Release the Dll */ 
   FreeLibrary(hGetProcIDDLL); 

   /* The return val from the dll */ 
    returnintMyReturnVal; 
} 

int main(){
int res=0;
res = CallMyDLL();
cout << "The result is ";
cout << res;
}

Both codes compile with no errors. I have copied and pasted the AdditionDLL.dll in the C:// directory. But when I run the program, I get Unhandled exception access violation. What's the problem?

GetProcAddress() probably returned a NULL pointer.

The spelling and case of a function name pointed to by lpProcName must be identical to that in the EXPORTS statement of the source DLL's module-definition (.def) file.

Notice the functions still must be exported.

You had to do these:
at dll Project:
1.1-

int __stdcall add(int x, int y);

1.2-add def file With:

LIBRARY	"addition"
EXPORTS
		add

Thank you very much, adding the def file works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.