简体   繁体   English

从delphi调用c ++函数

[英]Calling c++ function from delphi

I wrote a DLL in C++ whose functions will be called from a Delphi application. 我用C ++编写了一个DLL,其功能将从Delphi应用程序中调用。

One of the functions in the DLL takes a Pointer to a buffer where an XML string should be written. DLL中的功能之一将Pointer应该写入XML字符串的缓冲区。 But, when I write a string into the buffer, after returning from the function the application crashes with an "Access violation at address 0048B... in module ....exe. Write of address 3030D..." error. 但是,当我向缓冲区中写入字符串时,从函数返回后,应用程序崩溃,并显示“在模块.... exe中地址0048B ...上的访问冲突。写入地址3030D ...”错误。

The calling convention of the function declarations are the same, both in the DLL and the application. 在DLL和应用程序中,函数声明的调用约定相同。

I've made a simple application in Delphi to simulate the behavior of the application and it works fine. 我在Delphi中制作了一个简单的应用程序来模拟应用程序的行为,并且运行良好。 The biggest problem is that I don't have any information about the application internals: no sources, no documents, not even logs. 最大的问题是我没有有关应用程序内部的任何信息:没有源,没有文档,甚至没有日志。 Just function declarations and parameter descriptions. 只是函数声明和参数描述。

Function declaration in delphi: 在delphi中的函数声明:

function functionName(var Buffer: Pointer; var BuffLen: Integer): Integer; stdcall;

Function Declaration in the DLL: DLL中的函数声明:

extern "C" int WINAPI functionName(char*, int*);

Does someone know how to solve this? 有人知道如何解决吗?

From my tests, I have a feeling that the problem is in the application, not in the DLL. 从我的测试中,我感觉到问题出在应用程序中,而不是DLL中。 However, I'm not completely sure about this. 但是,我对此并不完全确定。 Are there any possible tests I can do at the DLL site to either solve the problem or locate the issue? 我可以在DLL站点上进行任何可能的测试来解决问题或定位问题吗?

I'd really appreciate any help in this matter. 我真的很感激此事。

As a side note, the DLL is compiled with Visual Studio. 作为附带说明,DLL是使用Visual Studio编译的。 Can this cause the problem? 这会引起问题吗?

The DLL function you showed is declared wrong in your Delphi code. 您显示的DLL函数在您的Delphi代码中声明为错误。 var Buffer: Pointer is equivilent to void** in C, or void*& in C++, but certainly not to char* like the DLL function is expecting. var Buffer: Pointer等效于C中的void**或C ++中的void*& ,但肯定不像DLL函数所期望的那样等于char* Using a void** / void*& parameter would be useful if the DLL were allocating memory to return to the application, but from your description that is not the case. 如果DLL正在分配内存以返回到应用程序,则使用void** / void*&参数将很有用,但是从您的描述来看并非如此。

Use this Delphi declaration instead: 使用此Delphi声明代替:

function functionName(Buffer: PAnsiChar; var BuffLen: Integer): Integer; stdcall;

PAnsiChar in Delphi is equivalent to char* in C/C++. Delphi中的PAnsiChar等效于C / C ++中的char*

You should read the following blog article about the gotchas to watch out for when converting C/C++ declarations to Delphi: 您应该阅读以下有关陷阱的博客文章,当将C / C ++声明转换为Delphi时要当心:

Rudy's Delphi Corner: Pitfalls of converting 鲁迪(Rudy)的德尔菲角(Delphi Corner):转换的陷阱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM