Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi Unicode and Console
    primarykey
    data
    text
    <p>I am writing a C# application that interfaces with a pair of hardware sensors. Unfortunately the only interface that is exposed on the devices requires a provided dll written in Delphi.</p> <p>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.</p> <p>If I directly pass the data to WriteLn, I get '?' for each character. If I look through the array of characters and attempt to print them one at a time with an Ansi Conversion, only a few of the characters print (they do confirm the data though) and they will often print out of order. (printing with the index exposed simply jumps around.) I also tried converting the PWideChar's to integer straight: 'I' corresponds to 21321. I could potentially figure out all the conversions, but some of the data has a multitude of values.</p> <p>I am unsure of what version of Delphi the dll uses, but I believe it is 4. Definately prior to 7.</p> <p>Any help is appreciated!</p> <p>TLDR: Need to convert UTF-16 PWideChar to AnsiString for printing.</p> <p><strong>Example application:</strong></p> <pre><code>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.` </code></pre> <p>The issue was PAnsiChar needed to be the return type of the function sourced from the DLL.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload