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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
chensuCommented:
You can use a freeware ImpLib32 - ImpLib for Win32.

http://www.geocities.com/SiliconValley/5806/implib32.htm
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
galkinCommented:
Your problem is name mangling. Define all exported functions as extern "C"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

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.