将 WideString 转换为 PWideChar
Convert WideString to PWideChar
我使用 Nicomsoft OCR 库对 Delphi 中的图像进行 OCR。它对我的任务很有用,它有 Delphi 单元包装器,所以很容易在 Delphi 中使用它。但是,当我将空字符串作为参数值传递给某些 OCR 函数时,Delphi 调试器显示 "Range Error" 消息。我检查了包装器代码,发现 DLL 库函数接受 PWideChars 作为参数,但包装器接受 WideString。在 unit-wrapper 内部有以下转换:
function CallSomeOCRFunction(a: WideString);
var b: PWideChar;
begin
b := @a[1];
CallSomeDLLFunction(b); //passing "b" to DLL function that accepts PWideChar
//.....
我做了一些研究,发现很多常见问题解答都提供这种转换,例如:http://www.delphibasics.co.uk/RTL.asp?Name=PWideChar
如果 "a" 不是空字符串,它会起作用,但对于空字符串,它会导致 "Range" 错误。即使它是空字符串,我如何才能正确获取指向 WideString 变量的第一个字符的指针?据我了解,即使字符串为空,它也必须包含零字符并且 PWideChar 变量必须指向它。
按照 documentation 中的描述使用 PWideChar()
强制转换。在你的情况下它将是:
CallSomeDLLFunction(PWideChar(a));
我使用 Nicomsoft OCR 库对 Delphi 中的图像进行 OCR。它对我的任务很有用,它有 Delphi 单元包装器,所以很容易在 Delphi 中使用它。但是,当我将空字符串作为参数值传递给某些 OCR 函数时,Delphi 调试器显示 "Range Error" 消息。我检查了包装器代码,发现 DLL 库函数接受 PWideChars 作为参数,但包装器接受 WideString。在 unit-wrapper 内部有以下转换:
function CallSomeOCRFunction(a: WideString);
var b: PWideChar;
begin
b := @a[1];
CallSomeDLLFunction(b); //passing "b" to DLL function that accepts PWideChar
//.....
我做了一些研究,发现很多常见问题解答都提供这种转换,例如:http://www.delphibasics.co.uk/RTL.asp?Name=PWideChar
如果 "a" 不是空字符串,它会起作用,但对于空字符串,它会导致 "Range" 错误。即使它是空字符串,我如何才能正确获取指向 WideString 变量的第一个字符的指针?据我了解,即使字符串为空,它也必须包含零字符并且 PWideChar 变量必须指向它。
按照 documentation 中的描述使用 PWideChar()
强制转换。在你的情况下它将是:
CallSomeDLLFunction(PWideChar(a));