Solved

Problems importing functions from a DLL in a C++ program

Posted on 1998-10-29
6
500 Views
Last Modified: 2012-06-27
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\handkey.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
0
Comment
Question by:demarcy
  • 4
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 60 total points
Comment Utility
I think you need to add 'export'  to the function declarations.
0
 

Author Comment

by:demarcy
Comment Utility
rwilson... I have done so and it compiles well.... but in that case, I cannot use the functions in the DLL from an EXE program.

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\prueba.exe
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
0
 

Author Comment

by:demarcy
Comment Utility
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$qqsuc  @3   ; HandKey_RequestDatalog(unsigned char)
__stdcall
    @HandKey_RequestTemplate$qqsuc @2   ; HandKey_RequestTemplate(unsigned
char) __stdcall
    @Setup$qqsv                    @1   ; Setup() __stdcall
    __DebuggerHookData             @4

Why is this?
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:demarcy
Comment Utility
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
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
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.
0
 

Author Comment

by:demarcy
Comment Utility
Thanks.. I have solved the problem.. it was obvious  :)

Bye

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now