Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use DLL in C without lib

Tags:

c

dll

How can I use the functions in a DLL in C without a LIB file to go with it? I know all of the function prototypes and their names.

like image 774
Void Star Avatar asked Jul 28 '12 04:07

Void Star


2 Answers

Yes you can. You should use the GetProcAddress function, to call the function directly in the DLL, without involving the LIB

Processes explicitly linking to a DLL call GetProcAddress to obtain the address of an exported function in the DLL. You use the returned function pointer to call the DLL function.

To quote the Example from the above link:

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD,UINT);
...

HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;

hDLL = LoadLibrary("MyDLL");
if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL,
                                           "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);
      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}
like image 88
Anirudh Ramanathan Avatar answered Sep 19 '22 08:09

Anirudh Ramanathan


You can use LoadLibrary() and GetProcAddress() as described in the answer by DarkXphenomenon. Or, another alternative is to create your own import library for the DLL by creating a .def file then running that through the LIB command to generate an import library. Additional details here:

http://support.microsoft.com/kb/131313

like image 42
cbranch Avatar answered Sep 22 '22 08:09

cbranch