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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:04:28+00:00 2026-05-15T01:04:28+00:00

In external (Delphi-created) DLL I’ve got the following function that I need to call

  • 0

In external (Delphi-created) DLL I’ve got the following function that I need to call from C# application.

function ReadMsg(handle: longword; var Msg: TRxMsg): longword; stdcall; external 'MyDll.dll' name 'ReadMsg';

The “TRxMsg” type is variant record, defined as follows:

TRxMsg = record
    case TypeMsg: byte of
        1: (accept, mask: longword);
        2: (SN: string[6]);
        3: (rx_rate, tx_rate: word);
        4: (rx_status, tx_status, ctl0, ctl1, rflg: byte);
end;

In order to call the function from C#, I declared auxiliary structure “my9Bytes” containing array of bytes and defined that it should be marshalled as 9 bytes long array (which is exactly the size of the Delphi record).

private struct my9Bytes {
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1, SizeConst = 9)]
    public byte[] data;
}

Then I declared the imported “ReadMsg” function, using the “my9bytes” struct.

[DllImport("MyDll.dll")]
private static extern uint ReadMsg(uint handle, ref my9Bytes myMsg);

I can call the function with no problem… Then I need to create structure corresponding to the original “TRxMsg” variant record and convert my auxiliary “myMsg” array into this structure.

I don’t know any C# equivalent of Delphi variant array, so I used inheritance and created the following classes.

public abstract class TRxMsg {
    public byte typeMsg;
}
public class TRxMsgAcceptMask:TRxMsg {
    public uint accept, mask;
    //...
}
public class TRxMsgSN:TRxMsg {
    public string SN;
    //...
}
public class TRxMsgMRate:TRxMsg {
    public ushort rx_rate, tx_rate;
    //...
}
public class TRxMsgStatus:TRxMsg {
    public byte rx_status, tx_status, ctl0, ctl1, rflg;
    //...
}

Finally I create the appropriate object and initialize it with values manually converted from “myMsg” array (I used BitConverter for this).

This does work fine, this solution seems to me a little too complicated, and that it should be possible to do this somehow more directly, without the auxiliary “my9bytes” structures or the inheritance and manual converting of individual values. So I’d like to ask you for a suggestions for the best way to do this.

Thanks a lot!

  • 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-15T01:04:29+00:00Added an answer on May 15, 2026 at 1:04 am

    You’d normally handle this with the [StructLayout(LayoutKind.Explicit)] attribute on a structure declaration. With separate struct declarations for the union members, in this case. Which would almost work if it wasn’t for the SN member. The CLR won’t allow you to overlay a string or array member, it would allow unrestricted access to the garbage collected heap.

    It’s a mutt of a function, manual marshaling is appropriate here.

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

Sidebar

Related Questions

I'm trying to call an external Delphi function from C# which takes a Delphi
I need to call an external program from Delphi 2006 code with a long
I have a Delphi DLL that I did not write, but need to call
I have a Delphi 6 application that receives and processes an image stream from
I need a function in Delphi to verify the digital signature of an external
I need to get my external (public) IP address from Delphi. The same IP
I have a Delphi 6 program that receives audio from an external program via
I've inherited a Delphi application which I need to convert to a C# application
I am getting the following error from C++ Builder 2009's linker Unresolved external '__fastcall
I have a Delphi 5 application in the application code calls a function in

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.