【问题标题】:Convert WideString to PWideChar将 WideString 转换为 PWideChar
【发布时间】:2015-04-09 08:58:35
【问题描述】:

我使用 Nicomsoft OCR 库在 Delphi 中对图像进行 OCR。它对我的任务很有用,它具有 Delphi 单元包装器,因此很容易在 Delphi 中使用它。但是,当我将空字符串作为参数值传递给某些 OCR 函数时,Delphi 调试器会显示“范围错误”消息。我检查了包装器代码,发现 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 变量必须指向它。

【问题讨论】:

  • CallSomeDLLFunction(PWideChar(a)); 传递它。

标签: delphi type-conversion ocr widestring


【解决方案1】:

使用PWideChar() 转换,如documentation 中所述。在您的情况下,它将是:

CallSomeDLLFunction(PWideChar(a));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 2020-02-05
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多