需要简单的演示调用Delphi DLL在C ++ [英] Need simple demo call Delphi DLL in C++

查看:99
本文介绍了需要简单的演示调用Delphi DLL在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能很好地与C ++,但现在我需要建立一个函数,调用Delphi DLL和传递一个字符串到DLL,并得到返回新的字符串。

i don't work well with C++ but now i need build a function that call Delphi DLL and pass a string to DLL and get return new string.

这里是我的Delphi DLL代码:

here is my Delphi DLL code:

library testdll;
uses
  System.Classes,Winapi.Windows,System.SysUtils;
{$R *.res}

function hello(name : PWideChar):PWideChar;
var
rs:PWideChar;
begin
  rs:=PWideChar('Hello '+rs);
  Result:=rs;
end;

exports
hello;
begin
end.

任何人都可以帮助我在C ++中创建简单的代码来调用和获得结果形式hello函数, 。

Anyone can help me create simple code in C++ to call and get result form hello function, thank for help.

推荐答案

您正在尝试将一个PWideChar连接到一个字符串文字,并将其返回为另一个PWideChar。这不会按原样工作。你不应该返回一个PWideChar反正。这导致内存管理梦。。更好的设计是让调用者将一个缓冲区传递给DLL来填充,例如:

You are trying to concat a PWideChar to a String literal and return it as another PWideChar. That will not work as-is. You should not be returning a PWideChar anyway. That leads to memory management nightmares. A better design is to let the caller pass a buffer into the DLL to fill in instead, eg:

library testdll;

uses
  System.Classes,
  Winapi.Windows,
  System.SysUtils;

{$R *.res}

function hello(name, buffer : PWideChar; buflen: Integer): Integer; stdcall;
var
  rs: UnicodeString;
begin
  rs := 'Hello '+UnicodeString(name);
  if buffer = nil then
  begin
    Result := Length(rs) + 1;
  end else
  begin
    Result := Min(buflen, Length(rs));
    Move(rs[1], buffer^, Result * SizeOf(WideChar));
  end;
end;

exports
  hello;

begin
end.

然后,给定这个C ++声明::

Then, given this C++ declaration::

int __stdcall hello(wchar_t* name, wchar_t* buffer, int buflen);

根据您的需要,您可以使用各种不同的方式:

You can call it all kinds of different ways, depending on your needs:

wchar_t str[256];
int len = hello(L"joe", str, 255);
str[len] = 0;
...

int len = hello(L"joe", NULL, 0);
wchar_t *str = new wchar_t[len];
len = hello(L"joe", str, len);
str[len] = 0;
...
delete[] str;

int len = hello(L"joe", NULL, 0);
std::wstring str(len-1);
str.resize(hello(L"joe", &str[0], len));
...

int len = hello(L"joe", NULL, 0);
UnicodeString str;
str.SetLength(len-1);
str.SetLength(hello(L"joe", str.c_str(), len));
...

同样的代码可以很容易地转换为Pascal需要在Delphi中使用相同的DLL:

The same kind of code can be translated to Pascal very easily if you ever need to use the same DLL in Delphi:

function hello(name, buffer: PWideChar, buflen: Integer): Integer; stdcall; extern 'testdll.dll';


var
  str: array[0..255] of WideChar;
  len: Integer;
begin
  len := hello('joe', str, 255);
  str[len] := #0;
  ...
end;


var
  str; PWideChar
  len; Integer;
begin
  len := hello('joe', nil, 0);
  GetMem(str, len];
  len := hello('joe', str, len);
  str[len] := #0;
  ...
  FreeMem(str);
end;


var
  str; UnicodeString;
  len; Integer;
begin
  len := hello('joe', nil, 0);
  SetLength(str, len-1);
  SetLength(str, hello('joe', PWideChar(str), len));
  ...
end;

这篇关于需要简单的演示调用Delphi DLL在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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