从Delphi应用程序调用C ++编写的dll [英] Calling c++ written dll from delphi application

查看:186
本文介绍了从Delphi应用程序调用C ++编写的dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我被困了几天,不知道该怎么办.

我正在使用C ++编写的库(结果是dll),该库包含多个函数. DLL本身不是使用MFC的,而是单线程的.然后从delphi应用程序中调用这些函数.函数示例:
extern "C" __declspec(dllexport) int open(void)

所有函数都必须返回int,因为它用于检测,判断该函数是否失败.虽然,某些函数的结果也必须是字符串.该字符串作为函数参数传递.示例:
extern "C" __declspec(dllexport) int getName(int index, char* &name)

问题来了.执行完整个函数后,字符串变量包含正确的数据(很好),但是某个地方(我不知道在哪里)内存损坏,这也损坏了delphi应用程序中的某些变量.
我把所有的功能都删掉了:

Hello, I am stucked for few days and dont know what to do.

I am working on a library written in c++ (the result is dll), which contains several functions. The DLL itself isnt using MFC, and is singlethread. These functions are then called from delphi application. Example of a function:
extern "C" __declspec(dllexport) int open(void)

All the functions have to return int, because it is used to detect, wheather the function failed. Although, result of some functions has to be string also. This string is passed as a function parameter. Example:
extern "C" __declspec(dllexport) int getName(int index, char* &name)

And here comes the problems. After the whole function is executed, the string variable contains correct data (which is good), but somewhere (I dont know where) is being memory corrupted, which also corrupts some variables in delphi app.
I blanked all the functions like this:

extern "C" __declspec(dllexport) int getName(int index, char* &name)
{
   return 0;
}


内存仍在损坏. (这种字符串"类型的函数会不必要地改变delphi变量).
我将不胜感激,因为我真的不知道什么会导致此问题.
谢谢.


添加了一些delphi代码:


The memory is being corrupted still. (Unwanted change of delphi variables occures by this "string" type of functions).
I will appreciate any type of help, because I really dont know, what could cause this problem.
Thank you.


Added some delphi code:

n := getCount; //calling dll function
if n<0 then exit;
else
if n=0 then //throw exception
else
if n>1 then //throw exception
status := getName(0,tmp);//after executing this function is n modified, however there are no operations with n
if status<0 then exit;
status := getTown(0,tmp);
if status<0 then exit;


n 是神秘修改的变量.

Edit2 :刚尝试创建一个c ++控制台应用程序,我可以在其中测试从dll对方法的正确调用,并且一切正常,因此问题似乎出在delphi中,也许是在为方法或为方法调用或分配内存整个dll.

最终 问题已解决:您必须定义def文件并使用stdcall


n is the mystery modified variable.

Edit2: just tried to create a c++ console app, where could I test correct calling of methods from dll and everything works fine, so problem seems to be in delp maybe in calling and allocating memory for methods or whole dll.

FINAL The problem is solved: you have to define def files and use stdcall

推荐答案

我知道关于Delphi没什么,但是您确定它可以处理参考参数吗?我经常使用的其他语言(Java,Python)都无法处理C ++引用,因此对于Delphi来说可能是相同的,这就是导致问题的原因.
干杯,

I know nothing about Delphi but are you sure it can cope with a reference parameter? None of the other languages I use regularly (Java, Python) can handle C++ references so it might be same for Delphi and that''s what''s causing the problem.

Cheers,

Ash


1. C ++中的字符串= Deplhi中的PChar.
2. extern "C"表示C样式的函数调用.对于delphi,我记得它是cdecl.

干杯
1. String in C++ = PChar in Deplhi.
2. extern "C" means C-style function calling. For delphi it is cdecl as soon as I remember.

Cheers


检查了C ++和Delphi代码.什么都没看到:(但这看起来有点有趣:
Hi, checked both C++ and Delphi code. Don''t see anything :( However this looks a little bit interesting:
extern "C" __declspec(dllexport) int getName(int index, char* &name)



C ++中是否存在诸如* &说明符之类的东西?不应该是



Is there such thing as * & specifier in C++? Shouldn''t it be

extern "C" __declspec(dllexport) int getName(int index, char* name)



?



?


这篇关于从Delphi应用程序调用C ++编写的dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆