Solved

Import Borland C++ DLL in VC++

Posted on 2002-06-26
8
680 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add content to output file 4 63
wait notify demo infinite loop 3 110
noX challenge 17 117
Sed question 2 86
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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