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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:45:28+00:00 2026-05-27T04:45:28+00:00

I’m building a DLL in Delphi, and it needs to work similar to how

  • 0

I’m building a DLL in Delphi, and it needs to work similar to how the Windows API works. This DLL only has one exported function…

function DoSomething(var MyRecord: TMyRecord): Integer; stdcall;

…where TMyRecord = my record I will need to re-create in C#. If I’m not mistaken, this is exactly how the standard Windows API works. This record also contains a reference to another record type…

TMyOtherRecord = record
  SomeDC: HDC;
  SomeOtherInt: Integer;
end;

TMyRecord = record
  SomeInteger: Integer;
  SomeColor: TColor;
  SomeText: PChar;
  SomeTextSize: Integer;
  MyOtherRecord: TMyOtherRecord;
end;

Question part 1:

I’d like to see if I can avoid using PChar, if at all possible. I don’t expect anything over 255 characters to be passed through. Is there another type I can use instead which won’t require me to use a size of string?


Question part 2:

I need to double check that I am declaring this C# struct class correctly, because it needs to perfectly match the Record declared in Delphi…

public struct MyOtherRecord
{
  public IntPtr SomeDC;
  public int SomeOtherInt;
}

public struct MyRecord
{
  public int SomeInteger;
  public Color SomeColor;
  public string SomeText;
  public int SomeTextSize;
  public MyOtherRecord OtherReord = new MyOtherRecord();
}

Question part 3:

Is it safe in this case to have a record inside of a record (or struct inside of a struct)? Pretty sure it is, but I need to make sure.

  • 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-05-27T04:45:29+00:00Added an answer on May 27, 2026 at 4:45 am

    I’m going to assume that the information is flowing from C# to Delphi and not the other way, largely because that makes life a lot easier when writing the answer, and you didn’t state otherwise!

    In that case the Delphi function declaration should be:

    function DoSomething(const MyRecord: TMyRecord): Integer; stdcall;
    

    The first point is that you can’t expect System.Drawing.Color to be handled by the P/invoke marshaller. Declare the color as int and use ColorTranslator.ToWin32 and ColorTranslator.FromWin32 to handle the conversion.


    There’s nothing to be afraid of with PChar. You don’t need a field with the string length since the length is implicit in a PChar due to the null-terminator. Just declare the field as string in the C# struct, PChar in the Delphi record and let the P/invoke marshaller do its magic. Don’t try to write to the PChar content from Delphi. That will end in tears. If you want to pass a string back to the C# code then there are ways, but I won’t address them here.


    It’s perfectly fine to have inline structs. Nothing to worry about there. Don’t allocate them with new. Just treat them as value types (which they are) like int, double etc.


    In due course you will need to add StructLayout attributes and so on, declare your DLL function with DllImport and so on.


    To summarise, I would declare your structs like this:

    Delphi

    TMyOtherRecord = record
      SomeDC: HDC;
      SomeOtherInt: Integer;
    end;
    
    TMyRecord = record
      SomeInteger: Integer;
      SomeColor: TColor;
      SomeText: PChar;
      MyOtherRecord: TMyOtherRecord;
    end;
    
    function DoSomething(const MyRecord: TMyRecord): Integer; stdcall;
    

    C#

    [StructLayout(LayoutKind.Sequential)]
    public struct MyOtherRecord
    {
      public IntPtr SomeDC;
      public int SomeOtherInt;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public struct MyRecord
    {
      public int SomeInteger;
      public int SomeColor;
      public string SomeText;
      public MyOtherRecord MyOtherRecord;
    }
    
    [DllImport("mydll.dll")]
    static extern int DoSomething([In] ref MyRecord MyRecord);
    

    I’ve not marked the string with a MarshalAs since the default is to marshal it as a LPSTR which is the same as a Delphi 7 PChar.

    I’ve only compiled this in my head so there may be a few wrinkles.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.