Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7761593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:10:32+00:00 2026-06-01T14:10:32+00:00

I am writing a C# application that interfaces with a pair of hardware sensors.

  • 0

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.

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.

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.

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

Any help is appreciated!

TLDR: Need to convert UTF-16 PWideChar to AnsiString for printing.

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.`

The issue was PAnsiChar needed to be the return type of the function sourced from the DLL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:10:34+00:00Added an answer on June 1, 2026 at 2:10 pm

    To convert PWideChar to AnsiString:

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

    The code converts from UTF-16, null-terminated PWideChar to AnsiString. If you are getting question marks in the output then either your input is not UTF-16, or it contains characters that cannot be encoded in your ANSI codepage.

    My guess is that what is actually happening is that your Delphi DLL was created with a pre-Unicode Delphi and so uses ANSI text. But now you are trying to link to it from a post-Unicode Delphi where PChar has a different meaning. I’m sure Rob explained this to you in your other question. So you can simply fix it by declaring your DLL import to return PAnsiChar rather than PChar. Like this:

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

    And when you have done this you can assign to a string variable in a similar vein as I describe above.

    What you need to absorb is that in older versions of Delphi, PChar is an alias for PAnsiChar. In modern Delphi it is an alias for PWideChar. That mismatch would explain everything that you report.


    It does occur to me that writing a Delphi wrapper to the DLL and communicating via stdout with your C# app is a very roundabout approach. I’d just p/invoke the DLL directly from the C# code. You seem to think that this is not possible, but it is quite simple.

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

    Call the function like this:

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

    If the function returns a UTF-16 string (which seems doubtful) then you can convert the IntPtr like this:

    string str = Marshal.PtrToStringUni(ptr);
    

    Or if it is actually an ANSI string which seems quite likely to me then you do it like this:

    string str = Marshal.PtrToStringAnsi(ptr);
    

    And then of course you’ll want to call into your DLL to deallocate the string pointer that was returned to you, assuming it was allocated on the heap.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an application that needs to interfaces with different backend systems. I
I am writing a Web application that has a user interface for editing data.
I am writing an email application that interfaces with a MySQL database. I have
I'm writing a web application that will have plugins. The plugins will be .DLL
I am writing an ASP.NET class that interfaces with an external application. The flow
I am writing an Android application that interfaces with a RESTful service. This web
Good day, I am writing a simple CLI (command-line interface) application that provides a
I'm writing a J2SE desktop application that requires one of its components to be
I'm writing an application that interacts with other processes on a x64 Vista machine.
Im writing an application that supposed to send coordinates in an SMS, but I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.