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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:42:34+00:00 2026-06-01T00:42:34+00:00

-In my c code I have a struct which contains many unknown sized arrays

  • 0

-In my c code I have a struct which contains many unknown sized arrays in an unmanaged dll (c code)

-I need the data of one instance of this struct marshaled over to c#, which I will later on send back to the unmanaged c code

-I do not need to manipulate this data once it gets to csharp, only hold onto it/store it for a while (so it can remain in a byte array).

-I do not want to use the keyword ‘unsafe’ as it is a big project and this is just one small piece and I don’t want to be compiling like that.

I tried marshaling it as a lpArray and everything looks fine but when i look at the contents after coming back to the csharp, it is always empty. This type of marshaling style worked for me for dynamic arrays of various types but not the struct.

Searching the web is drawing blanks and much more complicated scenarios than my own, but if anybody has seen such a link please post it here I would be very greatful!

Thanks.

–update here is more or less the structure of my code:

c#:

[DllImport("mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
private static extern int W_Thread_Connect_NET(
    [MarshalAs(UnmanagedType.LPStr, SizeConst = 100)] string IPAddress, 
    int DevicePort,
    [MarshalAs(UnmanagedType.LPArray)] byte[] connectionHandle
);

//and call it like this, with an empty struc to be populated by c (can this be done? it is comming back without the data):
byte[] myStrucParam= new byte[100];
int result = W_Thread_Connect_NET(myStrucParam, myParam1, myParam2, ...);

c:

 typedef struct myStructDef{
     char* myArray1, 
     char* myArray2,
     int myInt1,
     ...
 } mystrucObj, *pMystrucObj;

//method that i am wanting to marshal the struct as a paramter here..
 MYDLL_DLLIMPORT int APIENTRY W_Thread_Connect_NET(pMystrucObj strucReturn_handle, char * IPAddress, int DevicePort, ...)
    {
      //(omitted)
    }
  • 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-01T00:42:35+00:00Added an answer on June 1, 2026 at 12:42 am

    You say that the C# code does not need to manipulate the struct. That makes it a pretty simple problem to solve. You can treat the struct pointer as an opaque pointer, that is an IntPtr.

    First of all you add a new function to your native code:

    pMystrucObj CreateStruct(void)
    {
        pMystrucObj res = malloc(sizeof(*res));
        return res;
    }
    

    Then in your C# code you call it like this:

    [DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
    private static extern IntPtr CreateStruct();
    

    Now declare W_Thread_Connect_NET like this:

    [DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
    private static extern int W_Thread_Connect_NET(
        IntPtr theStructPtr,
        string IPAddress, 
        int DevicePort,
        ....
    );
    

    And call it all like this:

    IntPtr theStructPtr = CreateStruct();
    int res = W_Thread_Connect_NET(theStructPtr, IPAddress, DevicePort, ...);
    

    And of course you’ll want to add another function named DestroyStruct to deallocate the struct’s memory once you are done with it.

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

Sidebar

Related Questions

I have a legacy database which contains simple data structures (no CODE refs thank
I have an unmanaged dll that contains a function to read a data from
I have a code that looks something like: struct Data { int value; };
Newbie here, I have a struct for a word, which contains a char array
I want to have some data cache which contains some objects which I can
I have a DLL from which I need to P/Invoke the following C method:
Suppose I compile a source file file which contains this piece of code, struct
I am having a struct defined in C++ which contains int and std::string arrays
In C++, I have a class which contains an anonymous bitfield struct. I want
I have this code struct Student { char name[48]; float grade; int marks[10,5]; char

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.