Calling C++ Builder dll from MSVC

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.

prbAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
galkinConnect With a Mentor Commented:
Your problem is name mangling. Define all exported functions as extern "C"
0
 
galkinCommented:
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
 
prbAuthor Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
chensuCommented:
You can use a freeware ImpLib32 - ImpLib for Win32.

http://www.geocities.com/SiliconValley/5806/implib32.htm
0
 
prbAuthor Commented:
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
 
prbAuthor Commented:
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
 
galkinCommented:
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
 
prbAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.