demarcy
asked on
Problems importing functions from a DLL in a C++ program
Hi all... I need your help again:
I've programmed the following DLL (I only show the function definitions):
#include <windows.h>
#include "handkey.h"
#pragma hdrstop
class TSerial
{
public:
BOOL Error;
MSG_FMT MessageOut, MessageIn;
TSerial( ); // Constructor
~TSerial( ); // Destructor
void SendMessage( ); // Envía un mensaje
void ReceiveMessage( ); // Recibe el mensaje
private:
HANDLE hCom;
LPSTR Message;
};
BOOL APIENTRY DllEntryPoint( HINSTANCE hDLL, DWORD dwReason, LPVOID Reserved )
{
hInstance = hDLL;
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
}
return TRUE;
}
BOOL APIENTRY Setup( )
{
..................
}
TSerial::TSerial( )
{
.......................... ......
}
TSerial::~TSerial( )
{
.......................... ..
}
void TSerial::SendMessage( )
{
.......................... ......
}
void TSerial::ReceiveMessage( )
{
.......................... ...
}
BOOL APIENTRY HandKey_RequestTemplate( BYTE Reader )
{
.......................... .....
}
BOOL APIENTRY HandKey_RequestDatalog( BYTE Reader )
{
.......................... ......
}
The exported functions are Setup, HandKey_RequestTemplate and HandKey_RequestDatalog, so the .DEF file is:
LIBRARY HANDKEY
DESCRIPTION 'Rutinas que manejan el HandKey Reader'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE SINGLE
EXPORTS Setup
HandKey_RequestTemplate
HandKey_RequestDatalog
When I compile the program, and then link it, I got the following linking error;
Info :Linking E:\Trabajos\HandKey\DLL\ha ndkey.dll
Warn :Warning: Attempt to export non-public symbol 'Setup'
Warn :Warning: Attempt to export non-public symbol 'HandKey_RequestTemplate'
Warn :Warning: Attempt to export non-public symbol 'HandKey_RequestDatalog'
The million question, WHY??????? Why cannot I export those functions????? How can I do it????? I think this problem is related to the use of a class because I have a similar DLL but made in C, not in C++ and it compiles and links well.
Thanks a lot in advance :)
Jaime
I've programmed the following DLL (I only show the function definitions):
#include <windows.h>
#include "handkey.h"
#pragma hdrstop
class TSerial
{
public:
BOOL Error;
MSG_FMT MessageOut, MessageIn;
TSerial( ); // Constructor
~TSerial( ); // Destructor
void SendMessage( ); // Envía un mensaje
void ReceiveMessage( ); // Recibe el mensaje
private:
HANDLE hCom;
LPSTR Message;
};
BOOL APIENTRY DllEntryPoint( HINSTANCE hDLL, DWORD dwReason, LPVOID Reserved )
{
hInstance = hDLL;
switch( dwReason )
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
}
return TRUE;
}
BOOL APIENTRY Setup( )
{
..................
}
TSerial::TSerial( )
{
..........................
}
TSerial::~TSerial( )
{
..........................
}
void TSerial::SendMessage( )
{
..........................
}
void TSerial::ReceiveMessage( )
{
..........................
}
BOOL APIENTRY HandKey_RequestTemplate( BYTE Reader )
{
..........................
}
BOOL APIENTRY HandKey_RequestDatalog( BYTE Reader )
{
..........................
}
The exported functions are Setup, HandKey_RequestTemplate and HandKey_RequestDatalog, so the .DEF file is:
LIBRARY HANDKEY
DESCRIPTION 'Rutinas que manejan el HandKey Reader'
CODE PRELOAD MOVEABLE DISCARDABLE
DATA PRELOAD MOVEABLE SINGLE
EXPORTS Setup
HandKey_RequestTemplate
HandKey_RequestDatalog
When I compile the program, and then link it, I got the following linking error;
Info :Linking E:\Trabajos\HandKey\DLL\ha
Warn :Warning: Attempt to export non-public symbol 'Setup'
Warn :Warning: Attempt to export non-public symbol 'HandKey_RequestTemplate'
Warn :Warning: Attempt to export non-public symbol 'HandKey_RequestDatalog'
The million question, WHY??????? Why cannot I export those functions????? How can I do it????? I think this problem is related to the use of a class because I have a similar DLL but made in C, not in C++ and it compiles and links well.
Thanks a lot in advance :)
Jaime
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
One strange thing is that if I use IMPDEF to generate a DEF file of an example program I got:
LIBRARY DLLSKEL.DLL
EXPORTS
DLLFunction1 @1
DLLFunction2 @2
__DebuggerHookData @3
While if I do it with my program, I get:
LIBRARY HANDKEY.DLL
EXPORTS
@HandKey_RequestDatalog$qq suc @3 ; HandKey_RequestDatalog(uns igned char)
__stdcall
@HandKey_RequestTemplate$q qsuc @2 ; HandKey_RequestTemplate(un signed
char) __stdcall
@Setup$qqsv @1 ; Setup() __stdcall
__DebuggerHookData @4
Why is this?
LIBRARY DLLSKEL.DLL
EXPORTS
DLLFunction1 @1
DLLFunction2 @2
__DebuggerHookData @3
While if I do it with my program, I get:
LIBRARY HANDKEY.DLL
EXPORTS
@HandKey_RequestDatalog$qq
__stdcall
@HandKey_RequestTemplate$q
char) __stdcall
@Setup$qqsv @1 ; Setup() __stdcall
__DebuggerHookData @4
Why is this?
ASKER
I've proved that those letters, for example, $qqsv after Setup is placed only by the CPP compiler, how can I do it so that in the LIB file only appear Setup, instead of Setup$qqsv?
Thanks
Jaime
Thanks
Jaime
I think what you are seeing is the name mangling that the compiler does. You need to specify the pascal calling convention (either pascal (or _pascal) or stdcall (or _stdcall)) in the function declarations.
Cheers,
Raymond.
Cheers,
Raymond.
ASKER
Thanks.. I have solved the problem.. it was obvious :)
Bye
Bye
ASKER
I have in the EXE program the following declarations:
#define DLLIMPORT __declspec(dllimport)
DLLIMPORT BOOL APIENTRY HandKey_RequestTemplate( BYTE );
DLLIMPORT BOOL APIENTRY HandKey_RequestDatalog( BYTE );
When I compile the program, linker gives the errors:
Info :Linking E:\Trabajos\HandKey\DLL\pr
Error:Error: Unresolved external 'HandKey_RequestTemplate' referenced from module prueba.c
Error:Error: Unresolved external 'HandKey_RequestDatalog' referenced from module prueba.c
How can I import it properly? The PRUEBA program is in C.
Thanks
Jaime