www.digitalmars.com         C & C++   DMDScript  

c++.windows.32-bits - Can't link explicitly with a DLL

reply Michael <Michael_member pathlink.com> writes:
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
Mar 25 2004
next sibling parent "Walter" <walter digitalmars.com> writes:
"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.
Mar 26 2004
prev sibling parent Michael <Michael_member pathlink.com> writes:
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
Mar 26 2004