Solved

Calling C++ Builder dll from MSVC

Posted on 1998-01-21
8
1,709 Views
Last Modified: 2013-12-03
I have written some code in CBuilder and compiled this as  cbase.dll . Currently my MSVC code dynamically loads the dll using LoadLibrary( dll_path ); and then GetProcAddress to get a function pointer to the function.

What I would like to do is generate one of those .lib files which hides from me the fact that I am loading a dll and allows me to call these directly. ( MSVC automatically generates one of these .lib files for you when you compile a dll). Is there some program that will take a non-MSVC dll and generate a .lib stub for it.

0
Comment
Question by:prb
[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
  • 3
8 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1410227
I suppose implib is old utility that generates import library from a DLL. You can use VC++ LINK linker program to generate import library. Run it with /IMPLIB switch
0
 

Author Comment

by:prb
ID: 1410228
If I use try :
implib cbase.lib cbase_dll.dll
and then link this into MSVC, I get

cbase.lib : fatal error LNK1136: invalid or corrupt file
Error executing link.exe.

In otherwords, MSVC sees the Borland lib and believes it is corrupt !?

If I try :
D:\projects\CBASE>link /IMPLIB:cbase_dll.dll
Microsoft (R) 32-Bit Incremental Linker
Copyright (C) Microsoft Corp 1992-1996. All rights reserved.

LINK : warning LNK4001: no object files specified; libraries used
LINK : warning LNK4068: /MACHINE not specified; defaulting to IX86
LINK : fatal error LNK1561: entry point must be defined

So I have to supply an entry point into the dll

Now C++ Builder has the following code :
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
  return 1;
}
Which I assume is the entry point into the dll
But when I try this, I get :

D:\projects\CBASE>link /IMPLIB:cbase_dll.dll /OUT:cbase.lib /ENTRY:DllEntryPoint

Microsoft (R) 32-Bit Incremental Linker Version 4.20.6164
Copyright (C) Microsoft Corp 1992-1996. All rights reserved.

LINK : warning LNK4001: no object files specified; libraries used
LINK : warning LNK4068: /MACHINE not specified; defaulting to IX86
LINK : error LNK2001: unresolved external symbol _DllEntryPoint
cbase.lib : fatal error LNK1120: 1 unresolved externals

Notice the underscore of DLLEntryPoint. Which still doesn't solve my problem.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1410229
You can use a freeware ImpLib32 - ImpLib for Win32.

http://www.geocities.com/SiliconValley/5806/implib32.htm
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:prb
ID: 1410230
I downloaded this utility. First I wrote the following code in MSVC and compiled it as a dll.

Inside new.h :
#include <stdio.h>
#include <windows.h>

#ifdef BUILDING_DLL
      #define      DLL_IMEX __declspec ( dllexport )
#else
      #define      DLL_IMEX __declspec ( dllimport )
#endif

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*);

DLL_IMEX int WINAPI Bump( int i );

--- End of file ----------------------------------


Inside new.cpp :

#include "new.h"

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* x)
{
  return 1;
}

DLL_IMEX int WINAPI Bump( int i )
{
   return( i + 1 );
}
--- End of file ----------------------------------

1) This MSVC generated newdll.dll compiles
2) I wrote an exe which includes the newdll.lib file and calls the dll's Bump function.
3) I run this exe with the dll in my path and it works.
4) I delete the newdll.lib file generated by MSVC and replace it with one generated by implib32.
5) I completly rebuild the exe and run
6) This works
7) I take the code listed above and create a dll in Cbuilder
8) I use implib32 to create a newdll.lib file
9) I modify the exe so that it now uses the newdll.lib from the cbuilder\projects directory
10) I recompile and get the following error :


Compiling...
simple_main.cpp
Linking...
simple_main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) int __stdcall Bump(int)" (__imp_?Bump@@YGHH@Z)
Debug/simple.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

simple.exe - 2 error(s), 0 warning(s)

When use implib32 with the -d option which generates a .def file, I get for the MSVC newdll.lib :

EXPORTS

      ?Bump@@YGHH@Z      @1

And for the Cbuilder dll, I get :
EXPORTS

      @Bump$qqsi      @1
      _DebugHook      @2
      _ExceptionClass      @3

You will notice that the signature of the the cbuilder Bump function is @Bump$qqsi and MSVC is looking for __imp_?Bump@@YGHH@Z which is clearly supplied by the MSVC version of the .lib as shown by the output .def file file of the MSVC version of the .lib .

I am starting to wonder if this is possible at all ?







0
 
LVL 7

Accepted Solution

by:
galkin earned 300 total points
ID: 1410231
Your problem is name mangling. Define all exported functions as extern "C"
0
 

Author Comment

by:prb
ID: 1410232
Still not the case. Changed both the MSVC and CBuilder version to  extern "C" and recompiled the dlls. Then used linklib32 to generate .def files

For MSVC :
EXPORTS

      _Bump@4      @1

For CBuilder:

EXPORTS

      Bump      @1
      _DebugHook      @2
      _ExceptionClass      @3

So now when I compile I get :

Linking...
simple_main.obj : error LNK2001: unresolved external symbol __imp__Bump@4
Debug/simple.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Notic the @4 which I am missing in the Cbuilder version



0
 
LVL 7

Expert Comment

by:galkin
ID: 1410233
Use dumbin utility with /EXPORTS switch to check what function are exported from a DLL and how they are exported. extern "C" declaration must result in exporting these function without any name mangline.
0
 

Author Comment

by:prb
ID: 1410234
Perhaps you dont understand the what I am saying. MSVC expects me to have a underscore in the front and @4 at the end. I can do what you surgest but I am doubtful that CBuilder is going to add a _ and @4 for me.  I give up.  You can have the points.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

626 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