Using C object files with Delphi XE2

After I wrote, a couple of day ago, the article on the theme of using Assembly Language with Delphi, one of the gurus at the Embarcadero’s Forums told me that I was wrong because Delphi XE2 does accept COFF formatted object files not only when compiling for 64-bit but also for 32-bit. I started Visual Studio 2010 to confirm and you know? He was damn right!

There are literally, millions of lines of ‘C’ code waiting to be compiled and linked with Delphi in order to receive a nice GUI. I know that Embarcadero has  C++ Builder, but for pure (no GUI) C or C++ development it stays behind Visual Studio in every respect.

The question now is: “Is it easy to link with Delphi XE2 code compiled with Visual Studio?”

The answer is: “It is not difficult, just pay attention to a few details. It is even easier for 64-bit code”

To show how it is done, let’s open VS 2010, select New Project in the File Menu and go to the Visual C++ section. You can select the Empty Project on the General node but is preferable to select  either Win32 Console Application or Win32 Project on the Win32 Project node because allows you to test the whole in VS. After doing this, add a C++ File and a Header File from the Project menu. These 2 files are the ones where we are going to put the code that will compile and link with Delphi. In Solution Explorer right-click the new C++ file you just added, select Properties and in the sub-node Advanced of the C/C++ node select Compile as C Code (/TC) and in the Precompiled Headers sub-node select Not Using Precompiled Headers. Now, in the Project Properties, General node select Use Unicode Character Set. Disable all settings of the project that deal with exception handling to avoid complications (i.e, Enable C++ Exceptions=”No” and Buffer Security Check = “No”). Finally, set Whole Program Optimization to “No” in the Optimization sub-node.

You are set and can start coding away your C functions. When done, right-click on the source file and select Compile. Do compilations for 32-bit and 64-bit then copy the .obj files to the folder where you have the Delphi XE2 project.

The Delphi linker has neither information about the parameters of the functions you defined in VS2010 nor information  about all the externals that need to be resolved at link time by the Delphi Linker. So, you need to declare all that in a Delphi unit.

To link with 32-bit Delphi programs, the golden rules are: 1) All the function declarations will be proceeded by an underscore and the calling convention should be cdecl (all right, this is the default). 2) All the externals should have the cdecl calling spec, if it is a call to a Windows API you must use in VS an intermediate function with cdecl calling spec (see our demo program to see how we solved the call to the MessageBoxW API) 3) Genuine externals with the cdecl calling spec are probably exports from msvcrt.dll and you can use this dll from your Delphi program without need to figure out replacement functions (see our demo program to see how).

To link VS object files with 64-bit Delphi XE2 programs it easier because: 1) There is only one calling convention, fastcall. 2) Function names are not underscored. 3) Externals to the Windows API are directly resolved without our action. 4) Using msvcrt.dll functions is just a question of declaring them in Delphi (see our demo program to see how).

All right, that’s all about it. If you are not experienced with ‘C’, with Delphi, or with both, things are not as easy as I put them and you will have to work hard.

I include complete source code (Delphi XE2 and VS 2010) for a demo of what I have been writing about. A final note, in VS stdafx.h file there is a #define DEBUGGING, You enable it to test the routines in VS, you disable it when you compile just the CtoDelphi.cpp file.

Download the VS2010 and Delphi XE2 32-bit/64-bit demo program source code Now!

Leave a Reply

Your email address will not be published. Required fields are marked *