Thread: Explicit dynamic linking

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    Unhappy Explicit dynamic linking

    when i click leftbutton, i got this message"Unable to locate the function", why i can't get the function address?
    i already put the mydll.dll file in the same folder and some PATH environment variable folder, but still can't working....
    please....

    /***********mydll.cpp*****************/

    #include<windows.h>
    #include"mydll.h"
    void ShowMousePos(HDC hDC, LPARAM lParam)
    {
    char str[80];
    wsprintf(str,"Button position is %d, %d", LOWORD(lParam),HIWORD(lParam));
    TextOut(hDC,LOWORD(lParam),HIWORD(lParam),str,strl en(str));
    }

    /******mydll.h************/

    #define DllExport __declspec(dllexport)
    DllExport void ShowMousePos(HDC hDC,LPARAM lParam);


    /*******ExplicitDll.cpp( Win32 implement dll file)************/

    long FAR PASCAL WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    HDC hDC;
    HMODULE hLibrary;

    typedef void (*func)(HDC,LPARAM);
    func f;
    hLibrary = LoadLibrary("mydll");

    if(!hLibrary)
    {
    MessageBox(hwnd,"Cannot Load Library","Error",MB_OK);
    return 0;
    }
    switch(msg)
    {
    case WM_LBUTTONDOWN:
    hDC = GetDC(hwnd);
    f = (func) GetProcAddress(hLibrary, "ShowMousePos");
    if(!f)
    {
    MessageBox(hwnd,"Unable to locate the function","Error",MB_OK);
    return 0;
    }
    f(hDC,lParam);
    ReleaseDC(hwnd,hDC);
    case WM_DESTROY:
    FreeLibrary(hLibrary);
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try enclose your function in an extern "C"{} block...it look like your compiler is mangling the name of the func and stopping the function being located....

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    Thanks

    Thanks,finally solve the problem

    #include<windows.h>
    #include"mydll.h"
    extern "C" __declspec(dllexport) void ShowMousePos(HDC hDC, LPARAM lParam)
    ^^^^^^^^^^^
    {
    char str[80];
    wsprintf(str,"Button position is %d, %d", LOWORD(lParam),HIWORD(lParam));
    TextOut(hDC,LOWORD(lParam),HIWORD(lParam),str,strl en(str));
    }

    /******mydll.h************/

    //#define DllExport __declspec(dllexport)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    extern "C" __declspec(dllexport) void ShowMousePos(HDC hDC,LPARAM lParam);
    ^^^^^^^^^^

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    why?

    um....extern "C" is the process of writing a program using modules from different or other
    languages,right? or i misunderstand?

    but in my program, the dll and the c++ souce code are same written in c++, so why we need the extern "C"? Even if this is working, but i just want to know why and how it is working......

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    When your compiler sees a function in C++ it "mangles" the name to provide extra information about the function (IE param size & type etc - this helps stuff like class membership and overloading)...

    Therefore when GetProcAddress looks for the function, it doesnt find that name...and as each compiler is allowed by the standard to produce its own form of namemangling (life would be so much easier if the mangling itself were part of the standard) - there's no uniform API to allow you to export a c++ function.....

    Therefore to allow your function to be found by GetProcAddress, it needs C linkage

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    fordy, when i use dynamic link libraries i dont need to use C linkage. why does mkyong have to? heres the source:

    Code:
    //library.cpp
    #include <windows.h>
    #include <stdio.h>
    #define DLL_EXPORT
    #include "library.h"
    
    int bitFlag[8]={1,2,4,8,16,32,64,128};	//Bit flags for binary operations
    
    void Notify(HWND hwnd,LPCTSTR str)
    {
    	MessageBox(hwnd,str,"Notification",MB_OK);
    	return;
    }
    
    void Notify(HWND hwnd,long int num)
    {
    	char *chTemp;
    
    	chTemp=(char *)malloc(11);
    	ltoa(num,chTemp,10);
    
    	Notify(hwnd,chTemp);
    
    	free(chTemp);
    	return;
    }
    
    int GetBit(BYTE byte,int bit)
    {
    	if ( (bit<0)||(bit>7) )
    	{
    		Notify(NULL,"bit request error; \nmust be between 0 and 7");
    		exit(ERR_GETBIT);
    	}
    
    	if (byte&bitFlag[bit])
    		return 1;
    	
    	return 0;
    }
    
    void SetBit(BYTE &byte,int bit,int state)
    {
    	if (GetBit(byte,bit))
    	{
    		if (state)
    			return;
    		else
    			byte-=bitFlag[bit];
    		return;
    	}
    	
    	if (state)
    		byte+=bitFlag[bit];
    	else
    		return;
    	return;
    
    }
    
    char *BinaryString(BYTE byte)
    {
    	char *tmpStr;
    
    	tmpStr=(char *)malloc(9);
    	for (int i=0;i<8;i++)
    		tmpStr[7-i]=GetBit(byte,i)+48;
    
    	tmpStr[8]=0;
    
    	return tmpStr;
    }
    Code:
    //library.h
    #ifndef libraryH
    #define libraryH
    
    #ifndef DLL_EXPORT
    #define DS __declspec(dllimport) 
    #else 
    #define DS __declspec(dllexport)
    #endif
    
    #define ERR_GETBIT 101
    
    void DS Notify(HWND,LPCTSTR);
    void DS Notify(HWND,long int);
    
    int DS GetBit(BYTE,int);
    void DS SetBit(BYTE,int,int);
    char DS *BinaryString(BYTE);
    
    #endif
    how is it different?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    fordy, when i use dynamic link libraries i dont need to use C linkage. why does mkyong have to? heres the source:

    how is it different?
    Because you will be implicitly linking via the created .lib file....all clients of your dll will need that lib file and more often than not it will require a specific compiler...add to that that and material changes to the dll (size as well as export list) will require a new library to be used...therefore all your clients must now re-link their programs.......

    The OP was using explicit linking via the GetProcAddress API function...and so for that, C Linkage is needed...

    The best of both worlds can be found with COM.....with that you can keep your dll code in object form...you dont need a lib (as your dll should be self registering)......and as long as the interfaces implemented dont change, you can add more interfaces and/or code without the clients recompiling....

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    i understood basically nothing of what you just said. do know of any tutorials/sites about this stuff? im a real beginner with libraries.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by bennyandthejets
    i understood basically nothing of what you just said. do know of any tutorials/sites about this stuff? im a real beginner with libraries.
    Well...I wasnt trying to be too complex......

    Try http://msdn.microsoft.com/library/de...it_linking.asp for a good start...also there are loads of decent tuts on the web

    My favorite reference for dlls was a chapter in Advanced Windows by Jeff Richter

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    thanks fordy. dont worry, you weren't being too complex, im just a lazy moron.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Linking Library questions
    By Blackroot in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2007, 03:04 AM
  2. Dynamic Linking & Memory usage
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 06-02-2007, 09:57 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Overloaded functions and Dll(with Explicit Linking)
    By Nishant Ghai in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2003, 12:34 AM
  5. Dynamic Linking in DOS... important!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-26-2002, 11:50 PM