Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 706
  • Last Modified:

Import Borland C++ DLL in VC++

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
l_angelo
Asked:
l_angelo
  • 4
  • 4
1 Solution
 
jhanceCommented:
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
 
l_angeloAuthor Commented:
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
 
jhanceCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
l_angeloAuthor Commented:
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
 
jhanceCommented:
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
 
l_angeloAuthor Commented:
Well... the header file was prepared by me.

0
 
jhanceCommented:
But HOW did you determine this information?  

That's the KEY to this entire situtaion.  
0
 
l_angeloAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now