Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want to use third party dll in my project. i have only one file *.dll. how can i access few method which is defined in that dll.

please somebody help me.


thanks,
Posted

The minimum you need in addition to the dll file is some documentation of the functions of that DLL, including the function names.

If you have that you can load the library with the LoadLibray function of Windows. Then use multiple calls to GetProcAddress to retrieve pointers to the functions you want to call. To actually call the function, you need to cast that pointer to a pointer to function with the correct signature, i.e. calling convention, return value, and arguments.

So your code will look something like this:

HINSTANCE hMyib = LoadLibrary (_T("MyLib.dll"));
if (hMyib == 0)
{
    // handle error
}

typedef int MyLibFunction (double arg1, int arg2);

MyLibFunction* pLibFunction =
    (MyLibFunction*) GetProcAddress (hMyib , "MyFuncName");
if (pLibFunction == 0)
{
    // handle error
}

// calling that function
int retValue = (*pLibFunction) (3.14, 42);


If your library contains C++ code, things get more complicated as function names will be mangled (contain some addition characters that encode the signature of the function).

Hope that get's you started in the right direction.
 
Share this answer
 
v2
Comments
rajKR12 6-Jun-12 5:28am    
Thanks for your reply.

I created a simple dll and tried to use it in my project but the problem is that dll class and method are not identified in the my project.

here is the code for dll project:

MathFuncsDll.h
<pre lang="cs">namespace MathFuncs
{
class MyMathFuncs
{
public:
// Returns a + b
static __declspec(dllexport) double Add(double a, double b)

};
}</pre>

MathFuncsDll.cpp
<pre lang="cs">#include "MathFuncsDll.h"

#include <stdexcept>

using namespace std;

namespace MathFuncs
{
double MyMathFuncs::Add(double a, double b)
{
return a + b;
}
}</pre>


MyProject.cpp
<pre lang="c++">
typedef void (*EntryPointfuncPtr)(int argc, const char * argv );

int main()
{
HINSTANCE LoadME;
LoadME = LoadLibrary(_T("MathFuncsDll.dll"));
if (LoadME != 0)
printf("LoadMe library loaded!\n");
else
printf("LoadMe library failed to load!\n");

EntryPointfuncPtr LibMainEntryPoint;
LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadME,"entryPoint");

double a = 7.4;
int b = 99;
std::cout << "a + b = " <<
MathFuncs::MyMathFuncs::Add(a, b);

std::cin.get();
std::cin.sync();
return 0;
}
</pre>


result:
<pre lang="vb">Error 1 error C2653: 'MathFuncs' : is not a class or namespace name c:\documents and settings\dmstest\my documents\visual studio 2005\projects\myexecrefsdll\myexecrefsdll\myexecrefsdll.cpp 29
Error 2 error C3861: 'Add': identifier not found c:\documents and settings\dmstest\my documents\visual studio 2005\projects\myexecrefsdll\myexecrefsdll\myexecrefsdll.cpp 29</pre>
nv3 6-Jun-12 5:51am    
As I don't have the source files I can't track these errors. Obviously your namespace definition of MathFuncs contains some problems. I's recomment to start with a plain C function to avoid all the trouble with name mangling.
Aescleal 6-Jun-12 10:37am    
Another option to loading the library and doing the whole "get proc address boogy" is to use the same information to write a header and DEF file for the DLL and then use lib /def (IIRC) to create an import library.

If there's only a few functions you might as well loads the thing manually, but when you're trying to load something like NTDLL with a couple of hundred functions it becomes a bit of a swine :-).
nv3 6-Jun-12 10:46am    
Yes, valuable remark! I didn't think of this.
stib_markc 12-Jun-12 2:23am    
LoadLibrary() doesn't export class, it only used only to export functions.
Just to add to the previous posts, download a simple DLL Export Viewer, which will give you the signature of all the functions available in a DLL.
 
Share this answer
 
  • Best solution: ask for the corrensponding header *.h file (and *.lib one) to the DLL developers.
  • Workaround solution, use the Dependency Walker tool[^] to discover the function (or method) signatures and use LoadLibrary, GetProcAddress, .. to dynamically bind with the DLL (see "Using Run-Time Dynamic Linking"[^]).
     
    Share this answer
     
    v2
    Comments
    nv3 6-Jun-12 5:01am    
    You were faster than I was :-) And with a good answer.
    Member 8576081 11-Jun-12 5:08am    
    5'ed
     
    Share this answer
     
    Comments
    CPallini 6-Jun-12 5:04am    
    Why do you assume it is a C# DLL?
    codeBegin 6-Jun-12 5:34am    
    Dll in c# was really an assumption, but call from c++ was an answer. Hope comment doesn't sounds you rude which results in a down vote.
    Thanks for your reply.

    I created a simple dll and tried to use it in my project but the problem is that dll class and method are not identified in the my project.

    here is the code for dll project:

    MathFuncsDll.h
    C#
    namespace MathFuncs
    {
        class MyMathFuncs
        {
        public:
            // Returns a + b
            static __declspec(dllexport) double Add(double a, double b)
            
        };
    }


    MathFuncsDll.cpp
    C#
    #include "MathFuncsDll.h"
    
    #include <stdexcept>
    
    using namespace std;
    
    namespace MathFuncs
    {
        double MyMathFuncs::Add(double a, double b)
        {
            return a + b;
        }
    }



    MyProject.cpp
    C++
    typedef void (*EntryPointfuncPtr)(int argc, const char * argv ); 
    
    int main()
    {
    	HINSTANCE   LoadME;
    	LoadME = LoadLibrary(_T("MathFuncsDll.dll"));
    	if (LoadME != 0)
        printf("LoadMe library loaded!\n");
    	else
        printf("LoadMe library failed to load!\n");
    
    	EntryPointfuncPtr LibMainEntryPoint;
    	LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadME,"entryPoint");
    
        double a = 7.4;
        int b = 99;
    	std::cout << "a + b = " <<
            MathFuncs::MyMathFuncs::Add(a, b);
    
    	std::cin.get();
    	std::cin.sync();
    	return 0;
    }



    result:
    VB
    Error   1   error C2653: 'MathFuncs' : is not a class or namespace name c:\documents and settings\dmstest\my documents\visual studio 2005\projects\myexecrefsdll\myexecrefsdll\myexecrefsdll.cpp    29
    Error   2   error C3861: 'Add': identifier not found    c:\documents and settings\dmstest\my documents\visual studio 2005\projects\myexecrefsdll\myexecrefsdll\myexecrefsdll.cpp    29
     
    Share this answer
     
    Comments
    rajKR12 6-Jun-12 5:43am    
    Shall i make any change in project properties ?????
    stib_markc 12-Jun-12 2:30am    
    LoadLibrary(Explicit linking of a dll) doesn't export class from a DLL, it only exports functions. You need a ".lib"(Implicit Linking) to link it to your project, then add corresponding header file.
    nv3 6-Jun-12 7:50am    
    While you are still experimenting, forget the namespace stuff and concentrate on simple C functions. As soon as you put the functions in a namespace or define them as C++ functions, the name will be mangled and you have to first figure out what the mangled name is, for example by the Dependency Walker.

    Also: You specified "entryPoint" in your GetProcAddress call. You will have to replace that by the function's name.

    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