Solved

Calling C++ Builder dll from MSVC

Posted on 1998-01-21
8
1,692 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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