locked
Loading a Delphi DLL into a VC++ windows forms application RRS feed

  • Question

  • Hello,

    I need to use a DLL which was written in Delphi in my VC++ windows forms application. However, Delphi doesn't generate a .LIB file when creating DLL's so I have to create a .LIB file manually. I have already written a .h file for the functions that I need to use. The .h code is shown below.

    1//File: CITRemote.h  
    2extern "C" {  
    3  
    4__declspec(dllexport) void* __stdcall CITRemote_create(  
    5   void *parameters );  
    6  
    7__declspec(dllexport) int __stdcall CITRemote_open(  
    8   void* handle, void* parameters);  
    9  
    10__declspec(dllexport) int __stdcall CITRemote_close(  
    11   void *handle );  
    12}  


    From what I understand I need to create a .DEF file to generate a .LIB file with the LIB command: lib /def:CITRemote.def. The .DEF file I wrote is shown below.

    1;File: CITRemote.DEF  
    2LIBRARY "CITREMOTE"  
    3  
    4EXPORTS  
    5    CITRemote_open  
    6    CITRemote_close  
    7    CITRemote_create  


    The name of the DLL is CITRemote.dll and uses the STD calling convention. After I execute the LIB command, CITRemote.lib and object CITRemote.exp are created. I added the lib and exp file into my project in the 'Resource Files' filter. When i try to call for example the CITRemote_create function, I get the following error:

    Linking... 
    1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/LTCG' specification 
    1>NTP checker.obj : error LNK2001: unresolved external symbol "extern "C" void * __stdcall CITRemote_create(void *)" (?CITRemote_create@@$$J14YGPAXPAX@Z) 
    1>C:\Documents and Settings\Aartun\My Documents\Visual Studio 2008\Projects\NTP checker\Debug\NTP checker.exe : fatal error LNK1120: 1 unresolved externals 

    So i guess the implementation of the function isn't found, thus meaning it doesn't link the DLL. But what am I doing wrong, did I miss something?

    Thanks in advance.

    Note: I'm using Visual Studio 2008 Proffesional and all the files(dll, lib, h, exp and def) are in the same directory.
    Wednesday, December 17, 2008 3:33 PM

Answers

  • Declaring the functions extern "C", as you should, and still seeing the linker looking for the mangled name (CITRemote_create@@$$J14YGPAXPAX@Z) is very hard to explain.

    Hans Passant.
    • Proposed as answer by Cha0sBG Friday, December 19, 2008 9:52 PM
    • Marked as answer by Rong-Chun Zhang Tuesday, December 23, 2008 9:07 AM
    Friday, December 19, 2008 7:16 PM

All replies

  • Crono:

    Well, for starters you need to use dllimport in the version of the header that you use in your .exe project. Usually this is done with a macro, so you can use the same header in the DLL and the .exe. But since your DLL is already compiled, I can you can just hard-code dllimport.



    David Wilkinson | Visual C++ MVP
    Wednesday, December 17, 2008 3:39 PM
  • Could you please elaborate and give me some example code. And what is this dllimport you speak of? Do you mean i have to replace the

    __declspec(dllexport) void* __stdcall CITRemote_create(
       void *parameters );

    with

    __declspec(dllimport) void* __stdcall CITRemote_create(
       void *parameters );

    ?

    Thanks in advance.


    Thursday, December 18, 2008 8:36 AM
  • Did you try looking up dllimport in the Help?

    But yes, in the version of the header that you use in your client .exe application, you should have dllimport, not dllexport.

    Did you try it?

     

    David Wilkinson | Visual C++ MVP
    Thursday, December 18, 2008 12:20 PM
  • I tried it but I got the same error in my output window.

    I succeeded in calling the DLL by using the LoadLibrary and GetProcAddress functions. However, it just feels cheap and dirty.

    The following code below is how I did it.

    1//CITRemote.h 
    2 
    3typedef void* __stdcall CITRemote_create(void *parameters ); 
    4 
    5//main.cpp 
    6HMODULE hLoadLibrary("CITRemote.dll"); 
    7CITRemote_create* fp; 
    8void * test; 
    9fp = (CITRemote_create*)GetProcAddress(h,"CITRemote_create"); 
    10fp(test); 

    Thursday, December 18, 2008 2:13 PM
  • Yes, I was afraid of that.

    Perhaps your static import library was not created correctly, or you are not linking to it. I usually put static libraries in the Linker->Input section of the project properties.
    David Wilkinson | Visual C++ MVP
    Thursday, December 18, 2008 3:41 PM
  • Declaring the functions extern "C", as you should, and still seeing the linker looking for the mangled name (CITRemote_create@@$$J14YGPAXPAX@Z) is very hard to explain.

    Hans Passant.
    • Proposed as answer by Cha0sBG Friday, December 19, 2008 9:52 PM
    • Marked as answer by Rong-Chun Zhang Tuesday, December 23, 2008 9:07 AM
    Friday, December 19, 2008 7:16 PM