Marek
Marek

Reputation: 11

How to convert C++ header file to delphi

I'm converting C++ header to delphi pas file. First, I converted C++ header using HeadConv 4.0 from Dr.Bob module. But it failed compiling. Can anyone help for this converting?

****** C++ header ******

#if !defined(____KKK_Module__)

#define ____KKK_Module__

#ifdef KKK_MODULE_EXPORTS

#define KKK_MODULE_API extern "C" __declspec(dllexport)

#else

#define KKK_MODULE_API extern "C" __declspec(dllimport)

#endif

KKK_MODULE_API int _stdcall KKK_Creat();

......

Above code was converted Delphi code which leads error as below;

****** Delphi code ******

{$IFDEF KKK_MODULE_EXPORTS}

const 
 KKK_MODULE_API = extern 'C' __declspec(dllexport);  **//error**

{$ELSE}

const
  KKK_MODULE_API = extern 'C' __declspec(dllimport);  **//error**

{$ENDIF}

var
  KKK_Creat: function: KKK_MODULE_API INT cdecl  {$IFDEF WIN32} stdcall 
{$ENDIF};

.........

Cause of error is that 'extern' is not the reserved syntex of Delphi. I don't know how to replace it with Delphi code and need any help.

Upvotes: 0

Views: 2412

Answers (1)

David Heffernan
David Heffernan

Reputation: 613461

That is not C#, it is C++.

The conditional section is a very common pattern to allow the header file to be used both when building the library and when importing it. You are only importing so don't need to translate that part.

You are then left with just a single function:

extern "C" __declspec(dllimport) int _stdcall KKK_Creat();

The use of extern "C" suppresses C++ name mangling. You don't translate that.

Loosely, you translate __declspec(dllimport) with the external directive.

Translate it all to:

function KKK_Creat: Integer; stdcall; external libname;

where libname is a string containing the name of the DLL.

You need to check what name was used to export the function. If the library was built without a .def file the function names will be decorated. That function would be named KKK_Creat@0. Use a tool like dumpbin or Dependency Viewer to check the exported function names in the DLL.

FWIW, I've never found an effective general purpose header translator and have always found it best to do this manually, or to write a bespoke header specific translator.

Upvotes: 2

Related Questions