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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking problem with dll internal functions 1

Status
Not open for further replies.

Mthales

Technical User
Feb 27, 2004
1,181
0
0
GB
Hi,
I have made myself a dll and then I'm trying to use it in an aplication. It all seems fine untill I like it because I get errors that are about functions that are internal to the dll and are not used in the aplication. The actual error message is:
Code:
StyleGenAPI.obj : error LNK2001: unresolved external symbol "int __cdecl AnaliseConstantsCSV(char *,int)" (?AnaliseConstantsCSV@@YAHPADH@Z)
In this StyleGenAPI is the name of the dll and the function is not exposed or used outside of the dll. I get this sort of error from several but not all the functions that are used inside the dll. I am really confused by this.

Does anyone have any advice or help for me? Thanks very much
 
I'm really not sure why there should be a __cdecl on that function. I have it very simply defined like this:

int AnaliseConstantsCSV(char * , int);

Thanks for thinking about this for me.
 
I'm not quite sure what happened but I seem to have changed the problem.

I read up on the MSDN about (the seemingly unrelated way of) linking against the .lib that is produced when making a dll so I changed the project settings of the application to link against this and it seems to have fixed the linkage problem.

However now when I run the application it gets to the first call into the dll and I get an error box that says "The procedure entry point _clearHeldData@0 could not be located in the dynamic link library StyleGenAPI.dll"

The declaration for this function is:
void __declspec( dllexport ) __stdcall clearHeldData(void);

I'm guessing that I'm getting the names and decoration wrong in the way that I am exporting and importing the functions from the dll but I can't see how to solve it :-(

Any help please? Thanks for your thoughts on this.

M
 
a) You can save some typing work and just declare exported functions as WINAPI as in
Code:
void WINAPI clearHeldData(void);

b) Make sure you have a definition file (<projectname>.def) with the following structure
Code:
; Declares the module parameters for the DLL.

; dll name, optional
LIBRARY      "xxx"
; optional description
DESCRIPTION  "My DLL"

EXPORTS
    ; Explicit exports can go here
    clearHeldData
    <other exported symbols>

Volker/
 
VolkerStamme, Thanks for your help but I have a few questions.

I thought since I was using the __declspec(dllexport) keyword to export the DLL’s functions, then the DLL did not require a .DEF file. But I've make one as you recomend. I've added it to the dll project but it appears to make no difference. Does it need to be put in any particular place?

Also what does the WINAPI bit do? Does this just need to go in the declairation or also where the function is actually defined?

M
 
You're right, __declspec(dllexport) does not require a .DEF file, WINAPI does (WINAPI is defined as __stdcall in windef.h).
Are you maybe mixing .c and .cpp files? You might have a name decoration problem. Try to declare (and define) your functions with extern "C". The following mini dll compiles, links and runs fine:
Code:
#include "windows.h"

extern "C" void __declspec(dllexport) Foo( void );

extern "C" void __declspec(dllexport) Foo( void )
{
	::MessageBeep( -1 );
}

Volker/
 
Thanks for that but I feel like I'm still missing something very fundimental here.

I've put your code in a new dll project and then make a new very simple application which calls the Foo function and which links to the dll's lib file. But I can't quite make it work - I get the error that Foo is an undeclared identifier. Any ideas about what I'm missing?

M


 
application which calls the Foo function and which links to the dll's lib file
Sorry, were you testing the .lib or the .dll? The following test program calls the Foo() function in the DLL, I don't link anything ;-)
Code:
#include "stdafx.h"
#include "windows.h"

int main(int argc, char* argv[])
{
	typedef void (APIENTRY *LPFoo)(void);

	LPFoo pFoo;

	int result = -1;
	HINSTANCE HL = LoadLibrary( <dll path> );

	if ( HL != NULL )
	{
		pFoo = (LPFoo) GetProcAddress( HL, "Foo" );
		if ( pFoo != NULL )
		{
			pFoo();
			result = 0;
		}
	}
	
	return result;
}
 
Ahhhhhhhhh that seems to be the step I was missing - I'm now going to look into the LoadLibrary and GetProcAddress functions.

Thank you very much for your help with this - have a star.
M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top