Click here to Skip to main content
12,451,425 members (47,957 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VC7 C++ DLL
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 :

#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 13-Jan-12 0:25am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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);
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:

#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 );
}
  Permalink  
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);
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 3 Jun 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100