Thread: GetProcAddres fails

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    GetProcAddres fails

    Hi all,

    I'm porting some project from mingw to MSVC. I managed to get everything to compile and link, but one problem remains: from the main application i have to load some DLL at run time and get the address of a function from it.
    This is my loader code (taken from MSDN):

    Code:
    #include <string>
    #include <iostream>
    #include <windows.h>
    
    int main()
    {
    
    	
    
    	typedef const std::pair<const int, const std::string> (*LPFNDLLFUNC1)();
    
    
    	HINSTANCE hDLL;               // Handle to DLL
    	LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
    	DWORD dwParam1;
    	UINT  uParam2, uReturnVal;
    
    	hDLL = LoadLibrary("FennekEntity");
    	if (hDLL != NULL)
    	{
    	   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "getUserType");
    	   if (!lpfnDllFunc1)
    	   {
    		  // handle the error
    		  FreeLibrary(hDLL);
    		  std::cout << "getadd error" << std::endl;
    	   }
    	   else
    	   {
    		  // call the function
    		  //uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
    	   }
    	}
    	else
    	{
    		std::cout << "loadlib error" << std::endl;
    	}
    
    
    	return 0;
    }
    And this is the code inside the DLL:




    Code:
    // FennekEntity.h
    
    
    
    #ifdef FENNEKENTITY_EXPORTS
    #define FENNEKENTITY_API __declspec(dllexport)
    #else
    #define FENNEKENTITY_API __declspec(dllimport)
    #endif
    
    
    #ifndef FENNEKENTITY_H_
    #define FENNEKENTITY_H_
    
    [...]
    
    FENNEKENTITY_API const std::pair<const int, const std::string> getUserType();
    
    
    #endif /*FENNEKENTITY_H_*/
    
    
    
    
    // FennekEntity.cpp
    
    
    
    #include "FennekEntity.h"
    
    [...]
    
    //extern "C"
    //{
    
    	
    	
    
    FENNEKENTITY_API const std::pair<const int, const string> getUserType()
    {
    	[...]	
    }
    	
    
    
    
    //} // extern "C"
    Does anybody has an idea, why getProcAddress always returns NULL? Using mingw it works (if I comment in the extern "C"). MSVC compains what an extern "C" declared function can't return non-pod data types, so I took it out.

    Thank you in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The problem here is that the function name of getUserType is "decorated" by the C++ compiler to indicate what types it returns and what parameters it takes. So the symbol of your function in the .DLL is not getUserType, but something like getUserType?ZVBA6 [that name is completely made up - but the concept applies].

    As to what the right solution is, I don't really know.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    Thank you matsp. The problem seems to be the extern "C" linkage. If I take that out, run time loading does also fail with mingw.

    So the correct question should probably be: How can I get MSVC to compile the DLL functions with extern "C" linkage, if that functions returns non-pod types/classes ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not sure what the BEST way to do this is, one way would be to pass in a pointer/reference to be filled in, instead of returning the data as a return value - from a performance aspect, it makes little difference, as returning "large" values is done by the calling function passing the address where the return type is supposed to be stored anyways.

    The other solution is of course to use MingW instead of MSVC - but I suspect that this is not a solution you want to have, as I would guess that the reason you want MSVC may be that it's your corporate standard or some such.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's what .def files are for. Look for them on the board. They allow you to rename functions for the purpose of DLL export.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    That's what .def files are for. Look for them on the board. They allow you to rename functions for the purpose of DLL export.
    Of course! Why didn't I think of that ... :-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    The other solution is of course to use MingW instead of MSVC - but I suspect that this is not a solution you want to have, as I would guess that the reason you want MSVC may be that it's your corporate standard or some such.
    In future I have to interface some extern code only available for MSVC. So I'm spending my nights learning about the littly but nasty compiler differences between mingw/gcc/msvc.

    Thank you, you guys here are just the best and saved me a lot of headaches already!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 06-24-2009, 09:49 AM
  2. Mac - File locking with fcntl() fails on shared volumes!?
    By idelovski in forum Linux Programming
    Replies: 3
    Last Post: 11-10-2008, 07:37 PM
  3. Why this fails - confusing
    By manav-II in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2008, 01:01 AM
  4. Replies: 0
    Last Post: 05-23-2005, 11:39 AM
  5. ReadFile function fails.
    By dpkeller in forum C++ Programming
    Replies: 2
    Last Post: 12-03-2004, 10:20 PM