Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Import Borland C++ DLL in VC++

Posted on 2002-06-26
8
Medium Priority
?
699 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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
 
LVL 32

Accepted Solution

by:
jhance earned 75 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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