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

  • Home
  • SEARCH
  • 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 203421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:22:38+00:00 2026-05-11T17:22:38+00:00

I have a C++ structure, and I have to interop it from my C#

  • 0

I have a C++ structure, and I have to interop it from my C# code.

I compile my C# code as a platform agnostic dll, which means that I can use the same C# dll on different platforms ( 32bit or 64bit– doesn’t matter), whereas I distribute the C++ differently for different platforms.

The problem is that the C++ structure that I must interop seems to work for different C# definition. In 64 bit OS, the following structure works:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct MayDay_CONTEXT
    {
        public System.UInt32  dwIndex;      //previously was int
        public System.UInt32 dwVersion;     //previously was int        
        public System.UInt64 hLock;         //previously was int
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        public byte[] reserve;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 56)]
        public byte[] bAtr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] bID;
        public uint dwAtrLen;
    } 

but in 32 bit OS, the following structure works:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct MayDay_CONTEXT
    {
        public int  dwIndex;        //previously was int
        public int dwVersion;       //previously was int        
        public int hLock;           //previously was int
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        public byte[] reserve;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 56)]
        public byte[] bAtr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] bID;
        public uint dwAtrLen;
    }

Is there anyway to set the structure member’s type at runtime? I know I need a kind of duck typing to do it, but maybe there is a better solution… any ideas?

  • 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-11T17:22:39+00:00Added an answer on May 11, 2026 at 5:22 pm

    Would IntPtr (for hLock) work? Otherwise, you’ll probably have to use #if definitions in your source… – i.e.

    #if X86
         ..
    #else
         ..
    #endif
    

    and define the X86 symbol in your 32-bit builds (there isn’t anything built in)

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

Sidebar

Related Questions

I have a structure which I need to populate and write to disk (several
I have a structure that contains an arrays of another structure, it looks something
I have a structure which I create a custom constructor to initialize the members
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
I am trying to use the GPS_DEVICE structure from within .NET CF 3.5 and
I have a DLL, which is designed in C++, included in a C# project
I have been given a DLL (InfoLookup.dll) that internally allocates structures and returns pointers
I have this structure which I want to write to a file: typedef struct
I have an array which have this structure: $queues[n] Array ( [id] => integer
I'm working on a multi-project, and right now I have a structure that resembles

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.