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 8132021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:14:35+00:00 2026-06-06T09:14:35+00:00

// delphi code (delphi version : Turbo Delphi Explorer (it’s Delphi 2006)) function GetLoginResult:PChar;

  • 0

// delphi code (delphi version : Turbo Delphi Explorer (it’s Delphi 2006))

function GetLoginResult:PChar;
   begin
    result:=PChar(LoginResult);
   end; 

//C# code to use above delphi function (I am using unity3d, within, C#)

[DllImport ("ServerTool")]
private static extern string GetLoginResult();  // this does not work (make crash unity editor)

[DllImport ("ServerTool")] 
[MarshalAs(UnmanagedType.LPStr)] private static extern string GetLoginResult(); // this also occur errors

What is right way to use that function in C#?

(for use in also in delphi, code is like,
if (event=1) and (tag=10) then writeln(‘Login result: ‘,GetLoginResult); )

  • 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-06T09:14:36+00:00Added an answer on June 6, 2026 at 9:14 am

    The memory for the string is owned by your Delphi code but your p/invoke code will result in the marshaller calling CoTaskMemFree on that memory.

    What you need to do is to tell the marshaller that it should not take responsibility for freeing the memory.

    [DllImport ("ServerTool")] 
    private static extern IntPtr GetLoginResult();
    

    Then use Marshal.PtrToStringAnsi() to convert the returned value to a C# string.

    IntPtr str = GetLoginResult();
    string loginResult = Marshal.PtrToStringAnsi(str);
    

    You should also make sure that the calling conventions match by declaring the Delphi function to be stdcall:

    function GetLoginResult: PChar; stdcall;
    

    Although it so happens that this calling convention mis-match doesn’t matter for a function that has no parameters and a pointer sized return value.

    In order for all this to work, the Delphi string variable LoginResult has to be a global variable so that its contents are valid after GetLoginResult returns.

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

Sidebar

Related Questions

We have some ancient Delphi code (might have even originated as Turbo Pascal code)
I tried to replace Delphi build-in function with my own version on-the-fly. function ShortCutToTextOverride(ShortCut:
Hi I use interop to call C# code in Delphi. C# code has a
I am working on Delphi XE2 version. We are planning to move our code
I have this horribly stripped delphi code that basically login to server, save cookie
I am assigned to convert delphi code to .Net, and was wondering if we
I'm using Enterprise Architect code generation feature to export my models to Delphi code.
For a test 'crash' I need a small piece of Delphi code to see
How do I get the motherboard ID or serial number from Delphi code? Is
In some Delphi 7 code I am maintaining, I've noticed a lot of instances

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.