Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DLL: GetProcAddress() always returns NULL??

Status
Not open for further replies.

registering

Programmer
Dec 10, 2004
3
US
Hi all,

I'm using VC++ 6.0 to try and explicitly load a DLL. However no
matter what I've tried GetProcessAddress() always returns NULL.
LoadLibrary works fine (not NULL anyway). I've checked the exported
name from the DLL via 'depends.exe' and it's not mangled: it shows
the same name ("fnTestDLL").

I've googled for a couple of hours with no luck, so I'm
out of ideas. Maybe it's my project settings? I've tried
including the testdll.lib in "Link", the testdll.h in testp.app
and the DLL in "Debug" all with no luck.

I've dumbed-down my code to the basics and still can't get it
to work. :(

Any help would be greatly appreciated!!


------------------------------------------------------------
This is my App's "testapp.cpp" that (tries to) call my DLL:
------------------------------------------------------------
Code:
#include "stdafx.h"
#include "testapp.h"

#ifdef _DEBUG_
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CWinApp   theApp;
using namespace std;

typedef int (CALLBACK* LPFNDLLFUNC1)();

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
   if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
   { .............. }
   
   LPFNDLLFUNC1  LibMainEntryPoint;
   HINSTANCE   LoadMe = LoadLibrary("c:\\code\\begin\\testdll\\debug\\testdll.dll");
   if (LoadMe == NULL)
   {  ............. }
   
   LibMainEntryPoint = (LPFNDLLFUNC1) GetProcAddress(LoadMe, "fnTestDLL");
   if (LibMainEntryPoint == NULL)
[b]   { ....ALWAYS FAILS HERE!!!...........} [/b]
   
   LibMainEntryPoint();
   FreeLibrary(LoadMe);
   return (0);
}
----------------------------------------
This is my DLL's "testdll.cpp":
----------------------------------------
Code:
#include "stdafx.h"
#include "testdll.h"

using namespace std;
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason, LPVOID lpreserved)
{
   switch (ul_reason):
   {
      default: break;
   }
   return (TRUE);
}

TESTDLL_API  int  nTestDLL = 0;
TESTDLL_API  int  fnTestDLL(void)
{
   return (42);
}

CTestDLL::CTestDLL()
{
   cerr << "constructed";
}
CTestDLL::~CTestDLL()
{
   cerr << "destructed";
}
-----------------------------------------------------
This is the DLL's "testdll.h":
-----------------------------------------------------
Code:
#ifdef TESTDLL_EXPORTS
#define TESTDLL_API  __declspec(dllexport)
#else
#define TESTDLL_API  __declspec(dllimport)
#endif

class TESTDLL_API CTestDLL
{
   public:  CTestDLL(void);
            ~CTestDLL(void);
};

extern TESTDLL_API int nTestDLL;
TESTDLL_API int fnTestDLL(void);
 
Howdy all,
I found using "Dependency Walker" the exported function name wasn't "fnTestDLL" it was "?fnTestDLL@@YAHXZ", and using that name, it works fine. I assume there must be SOME way to export nicer sounding names using C++, but I'll read-up on that. One thing I also noticed, is I can point to CTestDLL's constructor in GetProcAddress(), and can create an instance of type CTestDLL, but then the destructor never gets called! Weird. However at least I can call my DLL now. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top