Thread overview
Can't link explicitly with a DLL
Mar 26, 2004
Michael
Mar 26, 2004
Walter
Mar 27, 2004
Michael
March 26, 2004
I'm trying to build a DLL with DMC++ to explicitly link with at runtime. My intent is to make use of a global factory function.

I get no error messages while building, but at runtime GetProcAddress() fails. I believe the symbol name may be mangled but I thought declaring the function with extern "C" and __stdcall would unmangle it for me.

First, here are the files (stripped to bare minimum to demonstrate the problem):

[Foo.h]====================================================================

#ifndef FOO
#define FOO

#ifdef BUILD_DLL
#define FOOAPI __declspec(dllexport)
#else
#define FOOAPI __declspec(dllimport)
#endif

struct FOOAPI Foo
{
Foo();
virtual ~Foo();
};

#endif

[Foo.cpp]==================================================================

#include "Foo.h"

Foo::Foo()
{ }

Foo::~Foo()
{ }

BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID)
{
return TRUE;
}

extern "C" __declspec(dllexport) Foo* __stdcall create()
{
return new Foo;
}

[TestDll.cpp]==============================================================

#include "Foo.h"
#include <windows.h>
#include <iostream>

using namespace std;

typedef Foo* (__stdcall *CreateFunc)();

int main()
{
HMODULE dll (LoadLibrary ("Foo.dll"));
if (!dll) {
cerr << "LoadLibrary: Failed!" << endl;
return 1;
}

CreateFunc create (
reinterpret_cast<CreateFunc>(GetProcAddress (dll, "create")));
if (!create) {
cerr << "GetProcAddress: Failed!" << endl;
return 1;
}

FreeLibrary (dll);

return 0;
}

I can't get past the call to GetProcAddress(). LoadLibrary() returns
successfully so I am finding the DLL at runtime and loading it.

Here is my build procedure from the command line:

dmc -ND -I\dm\stlport\stlport -DBUILD_DLL -c Foo.cpp
dmc -WD -oFoo.dll Foo.obj kernel32.lib
dmc -ND -I\dm\stlport\stlport -c TestDll.cpp
dmc -otestdll.exe TestDll.obj

I run the test and get:

GetProcAddress: Failed!

What am I doing wrong? I want to avoid putting aliases for symbols in a .def file. Note above that I'm not using a .def file but I'm explicitly importing and exporting the symbols.

Is there a way to see the symbols in a DLL with the tools from the DMC++ package?

Thanks! I'd appreciate any help with this. It's driving me nuts! :-(

/Michael


March 26, 2004
"Michael" <Michael_member@pathlink.com> wrote in message news:c40c4e$hmf$1@digitaldaemon.com...
> Is there a way to see the symbols in a DLL with the tools from the DMC++ package?

You can either use 'strings' or dumpexe.exe.


March 27, 2004
>I get no error messages while building, but at runtime GetProcAddress() fails. I believe the symbol name may be mangled but I thought declaring the function with extern "C" and __stdcall would unmangle it for me.

Well, I figured it out. I had to leave out the __stdcall. It was causing more harm than good. Apparently, it's not needed with DMC. I'm fairly certain that under MSVC++ and Borland the __stdcall is required. Maybe DMC doesn't mangle the names under this particular calling convention.

Weird.

Anyway, things are working nicely now.

P.S. If you are building DLLs to explicitly link, don't build them with the -fixed linker flag. The DLLs won't load. I found that out after a night of debugging and after many grey hairs. :-)

/Michael