Exporting variables from a dll issue

Started by
15 comments, last by SpreeTree 19 years, 11 months ago
can you post your code?
the .plg file would probably help too
Advertisement
Ok, heres the source from a little test app that does exactly the same thing

dllmain.h
#include <windows.h>#define MDLLEXPORTS#ifdef MDLLEXPORTS#define MEXPORT __declspec(dllexport)#else#define MEXPORT __declspec(dllimport)#endif// Create a class to exportclass MEXPORT CTestClass{public:	CTestClass(void) {}	~CTestClass(void) {}};	extern MEXPORT void randomFunction(int _int);extern MEXPORT int* randomPointer;


dllmain.cpp
#include "dllmain.h"BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved){    switch (ul_reason_for_call)	{		case DLL_PROCESS_ATTACH:			// Put any init code here		case DLL_THREAD_ATTACH:		case DLL_THREAD_DETACH:		case DLL_PROCESS_DETACH:			// Put any shutdown code here			break;    }    return TRUE;}MEXPORT void randomFunction(int _int){} MEXPORT int* randomPointer = NULL;


And my console app (no .h file here)

#include <windows.h>#include "dllmain.h"#pragma comment(lib, "test2.lib")void main(void){	CTestClass testClass;	randomPointer = NULL;	randomFunction(1);}


The dll and lib are created fine (the dll is called test2.dll). When building the console app, i get:

Linking...   Creating library Debug/consoleApp.lib and object Debug/consoleApp.expconsoleMain.obj : error LNK2001: unresolved external symbol "int * randomPointer" (?randomPointer@@3PAHA)Debug/consoleApp.exe : fatal error LNK1120: 1 unresolved externalsError executing link.exe.


The .plg file (for the dll) is:

 Build Log--------------------Configuration: Test2 - Win32 Debug--------------------Command LinesCreating temporary file "C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP3F.tmp" with contents[/nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "TEST2_EXPORTS" /Fp"Debug/Test2.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c "C:\Test2\dllmain.cpp"]Creating command line "cl.exe @C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP3F.tmp" Creating temporary file "C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP40.tmp" with contents[kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /incremental:yes /pdb:"Debug/Test2.pdb" /debug /machine:I386 /out:"Debug/Test2.dll" /implib:"Debug/Test2.lib" /pdbtype:sept .\Debug\dllmain.obj]Creating command line "link.exe @C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP40.tmp"Output WindowCompiling...dllmain.cppLinking...   Creating library Debug/Test2.lib and object Debug/Test2.expResultsTest2.dll - 0 error(s), 0 warning(s)



and the plg file for the console app is
Build Log--------------------Configuration: consoleApp - Win32 Debug--------------------Command LinesCreating temporary file "C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP47.tmp" with contents[/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/consoleApp.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c "C:\Test2\consoleMain.cpp"]Creating command line "cl.exe @C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP47.tmp" Creating temporary file "C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP48.tmp" with contents[test2.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/consoleApp.pdb" /debug /machine:I386 /out:"Debug/consoleApp.exe" /pdbtype:sept /libpath:"c:\test2\debug" .\Debug\consoleMain.obj]Creating command line "link.exe @C:\DOCUME~1\LEEWIN~1\LOCALS~1\Temp\RSP48.tmp"Output WindowCompiling...consoleMain.cppLinking...   Creating library Debug/consoleApp.lib and object Debug/consoleApp.expconsoleMain.obj : error LNK2001: unresolved external symbol "int * randomPointer" (?randomPointer@@3PAHA)Debug/consoleApp.exe : fatal error LNK1120: 1 unresolved externalsError executing link.exe.ResultsconsoleApp.exe - 2 error(s), 0 warning(s)


Hop you can see a problem in there

Thanks
Spree

[edited by - SpreeTree on May 19, 2004 4:35:24 AM]
As an aside. I created a brand new .net 2002 project, followed the exact same steps as i did when I created the vc6 dll and console project, and in this case, the variable was exported correctly, and no linking errors occured.

But, i also got .net to convert the vc6 project I am having trouble with, and when that was compiled in .net, I _did_ get the linking error on the variable.

The linking error only seems to occur when I try to access variable exported by a vc6 dll. Even when i use a vc6 console app with a .net2002 compiled dll, the variables have been exported fine.

I acn only assume that this means there is something wrong with my vc6 dll project settings somewhere?

Spree

[edited by - SpreeTree on May 19, 2004 5:14:54 AM]
First off when you create a DLL project in VC++ it defines PROJECTNAME_EXPORTS for you, so if I created a new DLL project named dll it would define DLL_EXPORTS for me.

If you look at the dll's .plg file VC++ has defined TEST2_EXPORTS for you.

In dllmain.h you have
#define MDLLEXPORTS
so when that header is included MEXPORT is always defined as __declspec(dllexport)

take #define MDLLEXPORTS out and change
#ifdef MDLLEXPORTS
to
#ifdef TEST2_EXPORTS

hope that fix's it

EDIT:
if you want you can just add something like this to the .cpp file with DllMain in it (this has to be before you include the .h file):

#ifndef DLL_EXPORTS
#define DLL_EXPORTS
#endif

and use something like this in the DLL's .h file:

#ifdef DLL_EXPORTS
#define DLLAPI __declspec(dllexport)
#else
#define DLLAPI __declspec(dllimport)
#endif

[edited by - Smacker_626 on May 19, 2004 11:02:01 PM]
BTW, if you have .NET 2K2 why don't you use it?

And if your going to use LoadLibrary() and GetProcAddress() to use the functions in your DLL you should use extern "C" so the names aren't changed

[edited by - Smacker_626 on May 19, 2004 11:07:36 PM]
Your right that did fix it. I errr... (slightly embarrassed), undefined the define to set up __declspec(dllimport), in another file earlier to test something and err.. never took it out :s

Everyone laugh at the daft programmer...

Hopefully wont be using LoadLibrary() and GetProcAddress(), but I will bear that in mind.

As for why I dont use 2002? I have yet to move onto forms, and have you ever tried to use 2002 and MFC? VC6 is a mucch better interface for MFC programming IMO

Thanks for all your help
Spree
Glad I could help

I only have MSVS 6.0, and I don't know much about the MFC library, if I ever need a GUI i'll just use FLTK :D

I would like to get VS .NET but I just spent ~$700 upgrading my PC again so i'm broke lol

[edited by - Smacker_626 on May 20, 2004 4:11:01 AM]

This topic is closed to new replies.

Advertisement