Solved

Import Borland C++ DLL in VC++

Posted on 2002-06-26
8
671 Views
Last Modified: 2013-11-20
Hi,

How to import a Borland C++ DLL in VC++ ?
I tried the following options:
a. Used LoadLibrary to load the BC++ DLL.
   Used GetProcAddress() for the individual functions.

But the call to the function throws an exception: Debug Error. Abnormal Termination of program under debug mode and goes to the catch block in release mode.
The return values of LoadLibrary and GetProcAddress() is successful.

b. Converted the .dll file into a .lib using:
   C:\> dumpbin /Exports law.dll > law.def
   C:\> lib /def:law.def
it generates a law.lib file.
I included the law.lib into the VC++ application thru' Project Settings.

But the function call in debug modes throws an exception (catch block) and in release mode crashes.

Sample Code :
<header file>
File name: DllFunctions.h
extern "C" int (WINAPI* OpenComm) (char* DllName, int ComPortNr, BYTE CanSpeedCode, BYTE BS, BYTE STmin);
extern "C" void (WINAPI* CloseComm) ();



<source file>
File name: DllFunctions.cpp

HINSTANCE HLevelWApi;

int (WINAPI* OpenComm) (char*, int, BYTE, BYTE, BYTE) = NULL;
void (WINAPI* CloseComm) () = NULL;


BOOL LoadLevelWApi (char* FileName)
{
     
     if ((HLevelWApi = LoadLibrary (FileName)) == NULL)
     {
          return FALSE;     // WARNING : Multiple returns          
     }

     if ((*(FARPROC*)&OpenComm = GetProcAddress (HLevelWApi, "_OpenComm")) == NULL)
     {
          return FALSE;     // WARNING : Multiple returns          
     }

if ((*(FARPROC*)&CloseComm = GetProcAddress (HLevelWApi, "_CloseComm")) == NULL)
     {
          return FALSE;     // WARNING : Multiple returns          
     }
}

File Name : Diag.cpp
#include "LevelW.h"

void CDiag::OnPbComm() // button click on a dialog
{
nResult = OpenComm("LXN4cat_", // LevelX dll
                    1, // COM Port
                    0, // Baud rate code
                    0, // Default
                    0); // default
/* the function crashes to this call */
}


The above code works in Borland C++, but fails in VC++.

Thanks and in anticipation,
Lawrence
   
0
Comment
Question by:l_angelo
  • 4
  • 4
8 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7113118
Assuming that this is a valid DLL ( and you need to validate that it is) the LoadLibrary/GetProcAddress method is the standard Windows method of using it.

I suspect that you failed to:

1) Properly account for the CALLING CONVENTION used by the DLL when you called its export functions.  This can be either "__stdcall" or "__cdecl" and you can only determine this by checking the documentation of the DLL.  This is MOST LIKELY the problem and a error here will cause a crash when you call any of the exports in the DLL.  The DLL and your application using it MUST AGREE on the calling convention.  Since the DLL is already written, its calling convention is correct by definition.

2) Pass the proper type and count of arguments to the DLL's export functions you called.  Check the DLL's documentation and be 100% you are calling its export functions with the proper type, quantity, and order of parameters.

3) Properly initialize the DLL.  Does it need to have one or more of its exports called before calling others?  Check its documentation.
0
 

Author Comment

by:l_angelo
ID: 7113169
Jhance, Thanks for the response. In addition to sample mentioned in the question, i tried various options of changing the calling convention, but i don't seem to be successful in that. I'm ain't sure where i'm goin wrong..
0
 
LVL 32

Expert Comment

by:jhance
ID: 7113389
There are generally WAY TOO MANY possibilities to determine the calling convention and parameter type/order/quantity by trial-and-error.

You really have three options:

1) Read the documentation for the DLL (assuming that there exists such).  Is there a .H file for this DLL giving the function declarations?  That would be a BIG help.

2) Contact the author of the DLL (Borland perhaps??) and see if the documentation is available.

3) Reverse engineer the DLL or an application that uses it.  This is certainly the "hard road" but may be your only option.

Assuming (again) that this is a VALID DLL and it CAN BE USED, if YOU use it properly from your application it will work.  But just saying here "it crashes" is not going to enable anyone to help much.
0
 

Author Comment

by:l_angelo
ID: 7113608
The header file is as below:


#ifndef     _DLLFUNCTIONS_H
#define     _DLLFUNCTIONS_H

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define     MAX_RESPONSE                         261

#define DIAG_RESP_INC                         0x40     // KWP2000 standards definition

// Return values:
#define     LW_SUCCESS                              0
#define     LW_REPEAT                              1
#define     LW_ERROR                              2
#define     LW_NOANSWER                              3


extern "C" int (WINAPI* OpenComm) (char* DllName, int ComPortNr, BYTE CanSpeedCode, BYTE BS, BYTE STmin);
extern "C" void (WINAPI* CloseComm) ();

// Parameters are passed after NrParam.
extern "C" int (WINAPI* GenDiagReq) (unsigned long TxCanId, BYTE Ta, BYTE Sa, BYTE Sid, int NrParam, ...);

// Requested SID
extern "C" int (WINAPI* GetResponse) (unsigned long RxCanId, BYTE* Response, BYTE Sid = 0xFF);

extern "C" int (WINAPI* Send34Download) (unsigned long TxCanId, BYTE Ta, BYTE Sa, BYTE Lr, char* SLine);

extern "C" int (WINAPI* J1939Request) (BYTE priority, BYTE dp,  BYTE pf, BYTE ps, BYTE sa, int NMessage, BYTE* RMessage);

extern "C" int (WINAPI* GetSerialNumber) (unsigned char* Response);
                   
#endif //#ifndef     _DLLFUNCTIONS_H


Thanks.
 
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Accepted Solution

by:
jhance earned 25 total points
ID: 7113887
Well, you're declaring these all as WINAPI.  That is a synonym for __stdcall.  But if these functions are __cdecl your calls will not work and will generally crash.

How did this H file come into your hands?  Did it come from Borland or did you prepare it?
0
 

Author Comment

by:l_angelo
ID: 7113985
Well... the header file was prepared by me.

0
 
LVL 32

Expert Comment

by:jhance
ID: 7114025
But HOW did you determine this information?  

That's the KEY to this entire situtaion.  
0
 

Author Comment

by:l_angelo
ID: 7131228
Found the problem:

In the Borland C++ code define the prototype to be WINAPI instead of _cdeclspec. Try using the DLL in VC++, it should work.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ADO Memory leak with DELPHI 2007 37 155
Question regarding Copy/Paste 16 86
Generic progress indicator 6 109
Problem to event 3 49
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

14 Experts available now in Live!

Get 1:1 Help Now