English 中文(简体)
Delphi Unicode and Console
原标题:Delphi Unicode and Console
  • 时间:2012-04-06 16:54:18
  •  标签:
  • delphi

我正在撰写一份C#应用程序,与一台硬件传感器连接起来。 不幸的是,在装置上暴露的唯一接口需要在Delphi提供一张纸张。

I am writing a Delphi executable wrapper that takes calls the necessary functions for the DLL and returns the sensor data over stout. However, the return type of this data is a PWideChar (or PChar) and I have been unable to convert it to ansi for printing on command line.

如果我直接将数据传递给书写,我就每个特点获得数据。 如果我看一看各种特性,并试图在阿尼西转换时加以打印,那么只有几种文字(这些字的确证实了数据,但往往按顺序打印。 (用指数印出的只是跳跃。) 我也试图将PWideChar改成简单分类:我相当于21321。 我可能列举所有转换,但有些数据具有多种价值。

I am unsure of what version of Delphi the dll uses, but I believe it is 4. Definately prior to 7.

任何帮助都值得赞赏。

TLDR: 需要转换UTF-16 PWide Char to AnsiString forprint.

<>Example application:

program SensorReadout;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  Windows,
  SysUtils,
  dllFuncUnit in  dllFuncUnit.pas ; //This is my dll interface.

var  state: integer;
     answer: PChar;
     I: integer;
     J: integer;
     output: string;
     ch: char;

begin
  try
    for I := 0 to 9 do
    begin
      answer:= GetDeviceChannelInfo_HSI(1, Ord( a ), I, state); //DLL Function, returns a PChar with the results.  See example in comments.

      if state = HSI_NO_ERRORCODE then
      begin
        output:=   ;
        for J := 0 to length(answer) do
        begin
          ch:= answer[J]; //This copies the char. Originally had an AnsiChar convert here.
          output:= output + ch;
        end;
        WriteLn(output);
      end;

  except
    on E: Exception do
      Writeln(E.ClassName,  :  , E.Message);
  end;
  ReadLn(I);
end.`

问题是,PransiChar需要成为从DLLL公司获得的职能的返回类型。

最佳回答

To convert PWideChar to AnsiString:

function WideCharToAnsiString(P: PWideChar): AnsiString;
begin
  Result := P;
end;

该代码将UTF-16、无名PWideChar改为AnsiString/code>。 如果你在产出中看到问题标记,那么你的投入要么不是UTF-16,要么包含在你ANSI代码上无法编码的特性。

我的猜测是,实际发生的情况是,你的Delphi DLL是用一部大学前编码Delphi创立的,因此使用了ANSI的文本。 但是,现在,你试图从Delphi号后编码中将其连接起来。 我确信,Rob在你的另一个问题上向大家解释了这一点。 因此,你可以简单地将其固定下来,宣布进口到<条码>。 与此类似:

function GetDeviceChannelInfo_HSI(PortNumber, Address, ChNumber: Integer;
  var State: Integer): PAnsiChar; stdcall; external DLL_FILENAME;

当你这样做时,你可以按照我上文所述的同样精神,分配一个巨大的变量。

你们需要吸收的是,在Delphi的旧版本中,PChar<>/code>是<代码>的一个附件。 PAnsiChar。 在现代德尔菲中,它是<代码>的一个要点。 PWideChar 。 这种不匹配将解释你报告的一切。


确实,我写了一部DLLL的Delphi包,并通过与你沟通。 C# app是一个非常四舍五入的办法。 I d only p/invoke the DL direct from the C# Code. 你似乎认为这是不可能的,但很简单。

[DllImport(@"mydll.dll")]
static extern IntPtr GetDeviceChannelInfo_HSI(
    int PortNumber, 
    int Address, 
    int ChNumber,
    ref int State
);

要求发挥这样的职能:

IntPtr ptr = GetDeviceChannelInfo_HSI(Port, Addr, Channel, ref State);

如果该功能恢复UTF-16 string(似乎有疑问),那么你就可以将<条码>IntPtr<>/code”改为:

string str = Marshal.PtrToStringUni(ptr);

或者,如果它实际上是一种可能给我带来很大可能性的ANSI扼杀,那么你就是这样:

string str = Marshal.PtrToStringAnsi(ptr);

当然,你想向你们的利比里亚人民解放组织发出呼吁,要求他把被送回的座右铭放在你身上,假定这个座右铭被分配到他处。

问题回答

改变我对评论的思维——我要回答:

根据该法典,如果“国家”是代典;“国家”则不例外,那么它将写给青少年的未经初步使用的“产出”。 可能包括意外显示“S”和“4”以及一系列1个或1个以上问题标志的内容

type of answer(variable) is PChar. use length function good for string variable. use strlen instead of length.

 for J := 0 to StrLen(answer)-1 do

PChar(char *)的无障碍范围为0.n-1。

转换UTF-16 PWide Char to you you you

var
  WStr: WideString;
  pWStr: PWideString;
  AStr: AnsiString;
begin
  WStr :=  test ;
  pWStr := PWideString(WStr);
  AStr := AnsiString(WideString(pWStr));
end;




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签