How to use Unicode from Delphi in C++ DLL

半腔热情 提交于 2019-12-11 13:55:47

问题


How can I use a Unicode PWideChar in Delphi to call a C++ function in a DLL? I want to send a string from Delphi to C++ and modify it.

function Test(a: PWideChar): Integer; cdecl; external 'c:\Win32Project1.dll' name 'Test';

extern "C" __declspec(dllexport) int __cdecl Test(char* a)
{
    a = "汉语";
    return 0;
}

回答1:


Typically the caller allocates a buffer which is passed to the callee, along with the buffer length. The callee then populates the buffer.

size_t Test(wchar_t* buff, const size_t len)
{
    const std::wstring str = ...;
    if (buff != nullptr)
        wcsncpy(buff, str.c_str(), len);
    return str.size()+1; // the length required to copy the string
}

On the Delphi side you would call it like this:

function Test(buff: PWideChar; len: size_t): size_t; cdecl; external "mydll.dll";
....
var
  buff: array [0..255] of WideChar;
  s: string;
....
Test(buff, Length(buff));
s := buff;

If you don't want to allocate a fixed length buffer, then you call the function to find out how large a buffer is needed:

var 
  s: string;
  len: size_t;
....
len := Test(nil, 0);
SetLength(s, len-1);
Test(PWideChar(s), len);

If you wish to pass a value to the function, I suggest that you do that through a different parameter. That makes it more convenient to call, and does not force you to make sure that the input string has a buffer large enough to admit the output string. Maybe like this:

size_t Test(const wchar_t* input, wchar_t* output, const size_t outlen)
{
    const std::wstring inputStr = input;
    const std::wstring outputStr = foo(inputStr);
    if (buff != nullptr)
        wcsncpy(buff, outputStr.c_str(), len);
    return outputStr.size()+1; // the length required to copy the string
}

On the other side it would be;

function Test(input, output: PWideChar; outlen: size_t): size_t; cdecl; 
  external "mydll.dll";

And the calling code should be obvious.



来源:https://stackoverflow.com/questions/21867113/how-to-use-unicode-from-delphi-in-c-dll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!