Solved

Import Borland C++ DLL in VC++

Posted on 2002-06-26
8
673 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is it possible to combine several text files into one big one? (redhat v5 enterprise) 4 81
string initialization in java 11 110
post4 challenge 28 93
Making an alias 7 81
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

12 Experts available now in Live!

Get 1:1 Help Now