Solved

Calling C++ Builder dll from MSVC

Posted on 1998-01-21
8
1,686 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
 

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

9 Experts available now in Live!

Get 1:1 Help Now