Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all,

I have an application written in VC++ 2005 and i create a DLL (exporting a single function)

The Code in my DLL is as below :

C++
#include <stdio.h>
#include <atlstr.h>


extern "C"
{
	__declspec(dllexport) void DisplayHelloFromDLL()
	{
		CString str;
		printf ("Hello from DLL !\n");
	}
}


The prob is i wanna import the Function from my DLL withour Importing the generated LIB and i dont know how to do that in VC2005.

Plz somebody help me or guive me a clue. Thx
Posted
Updated 2-Aug-19 0:39am

This is done by loading the library, getting the adresses of the required functions, and call them (see also GetProcAddress [^] in the MSDN):

C++
typedef VOID (CALLBACK* LPFN_DHFDLL)();
HINSTANCE hInst = ::LoadLibrary(_T("MyLib.dll"));
if (NULL != hInst)
{
    LPFN_DHFDLL pDisplayHello = 
        (LPFN_DHFDLL)::GetProcAddress(hInst, "DisplayHelloFromDLL");
    if (pDisplayHello)
        pDisplayHello();
    ::FreeLibrary(hInst);
}
 
Share this answer
 
Hi,
Check this.

When you say that you are not using import lib, I assume that you are not generating it. So there is not need to use __declspec(dllexport) to export a function, rather you can use a def file:

DefFile.def
================
LIBRARY "MyDLL"
EXPORTS
DisplayHelloFromDLL
================

Now coming to accessing this. All we have to do is to map that "MyDLL" into our exe address space. For this we can use using 'LoadLibrary'. Check this:

C++
#include <windows.h>

// Declaring Function pointer for DisplayHelloFromDLL
typedef void ( WINAPIV* LPFN_DISPLAYHELLO ) ( ); 

void main()
{
    // Loading DLL
    HINSTANCE hMyDLL = LoadLibraryA("Dll_New.dll");

    // Getting address of the DLL exported function
    LPFN_DISPLAYHELLO fnDisplayName = (LPFN_DISPLAYHELLO)GetProcAddress(
		                                   hMyDLL,
					      "DisplayHelloFromDLL" );
                                                          

    // Actual function call
    fnDisplayName();

   // Releasing DLL 
   FreeLibrary( hMyDLL );
}
 
Share this answer
 
v2
Comments
Member 11738586 3-Jun-15 7:42am    
How do I call a function which takes arguments?

cmsOpenProfileFromFile() is a function that takes two arguments.
When I build the below mentioned code, I get the following error:
error C2197:'LPFN_OPFDLL' : too many arguments for the call

typedef void (WINAPIV* LPFN_OPFDLL) ();
HINSTANCE hMyDll = ::LoadLibraryA("libcms2.dll");
LPFN_OPFDLL funOne =(LPFN_OPFDLL)GetProcAddress(hMyDll, "cmsOpenProfileFromFile");
funOne("AppleRGB.icc", "r");
FreeLibrary(hMyDll);
Jochen Arndt 3-Jun-15 9:02am    
You should have raised your own question rather than asking this at a three year old post.

However, you must specify the parameter types in the typedef:
typedef void (WINAPIV* LPFN_OPFDLL) (LPCSTR, LPCSTR);
Thank you both. that was exactly what i was looking for. I found many tutorials but most of them use the generated Lib and i found not not suitable coz everytime i change the DLL i have to recompile the projects using that DLL.

thank you again. You saved me a lot of time.
 
Share this answer
 
#include <windows.h>

// Declaring Function pointer for DisplayHelloFromDLL
typedef void ( WINAPIV* LPFN_DISPLAYHELLO ) ( ); 

void main()
{
    // Loading DLL
    HINSTANCE hMyDLL = LoadLibraryA("Dll_New.dll");

    // Getting address of the DLL exported function
    LPFN_DISPLAYHELLO fnDisplayName = (LPFN_DISPLAYHELLO)GetProcAddress(
		                                   hMyDLL,
					      "DisplayHelloFromDLL" );
                                                          

    // Actual function call
    fnDisplayName();

   // Releasing DLL 
   FreeLibrary( hMyDLL );
}
 
Share this answer
 
Comments
CHill60 3-Aug-19 10:44am    
An unexplained code dump is not a solution to this 7 year old question! Moreover, just copying the code from solution 2 is plagiarism and is not tolerated on this site. Please do not do this again

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