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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:02:10+00:00 2026-06-06T15:02:10+00:00

My C declarations are as follows: int myData(uint myHandle, tchar *dataName, long *Time, uint

  • 0

My C declarations are as follows:

int myData(uint myHandle, tchar *dataName, long *Time, uint *maxData, DATASTRUCT **data);

typedef struct {
  byte Rel;
  __int64 Time;
  char Validated;
  unsigned char Data[1];
} DATASTRUCT;

My C# declarations are as follows:

[DllImport("myData.dll", EntryPoint = "myData")]
public static extern int myData(uint myHandle, [MarshalAs(UnmanagedType.LPTStr)] string dataName, out long Time, out uint maxData, ref DATASTRUCT[] data);

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct DATASTRUCT
{
    public sbyte Rel;
    public long Time;
    public byte Validated;
    public double Data;
}

I then call the managed function as follows:

string dataToShow = "description";
long Time;
uint maxData; // How many structs will be returned, i.e., how much data is available?
uint myHandle = 1;

DATASTRUCT[] dataInformation = new DATASTRUCT[3]; // Doesn't it matter what I specify as the array size?

myData(myHandle, dataToShow, out Time, out maxData, ref dataInformation);

Upon execution the above function will return successfully with only one structure even though there are 3 to return. Why is this so?

Additional information; I have tried passing the pointer to a pointer of an array of structs the following ways:

  • ref DATASTRUCT[] data; // It works, but it only returns one struct
  • [Out, MarshalAs(UnmanagedType.LPArray)] DATASTRUCT[] data; // returns the number of defined structs with garbage

As I understand it I might need to do some manual marshalling using IntPtr, I do not know how to implement this however, so any advice would be appreciated.

  • 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-06T15:02:11+00:00Added an answer on June 6, 2026 at 3:02 pm

    Okay, it seems as though your native library does the allocation, so really all you need to do is provide a pointer through which you can access the allocated data.

    Change your API definition to (note, I changed the maxData param to uint, long is 64 bits in .NET and 32 bits in native.

    [DllImportAttribute("myData.dll", EntryPoint = "myData")]
    public static extern int myData(uint myHandle, [MarshalAs(UnmanagedType.LPTStr)] string dataName, out uint Time, out uint maxData, out IntPtr pData);
    

    Off the top of my head I can’t quite remember if you need the out keyword for the final parameter, but I think so.

    Then, call myData:

    uint nAllocs = 0, time = 0;
    IntPtr pAllocs = IntPtr.Zero;
    myData(1, "description", out time, out nAllocs, out pAllocs);
    

    Now, pAllocs should point to unmanaged memory, to marshal these into managed memory isn’t too difficult:

    [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct DATASTRUCT
    {
        public byte Rel;
        public long Time;
        public byte Validated;
        public IntPtr Data; //pointer to unmanaged string.
    }
    
    
    int szStruct = Marshal.SizeOf(typeof(DATASTRUCT));
    DATASTRUCT[] localStructs = new DATASTRUCT[nAllocs];
    for(uint i = 0; i < nallocs; i++)
        localStructs[i] = (DATASTRUCT)Marshal.PtrToStructure(new IntPtr(pAllocs.ToInt32() + (szStruct * i)), typeof(DATASTRUCT));
    

    And now you should have an array of local structs.

    A point to note
    You may need to set your project to compile as x86, to standardize the size of an IntPtr to 4 bytes (DWORD) instead of AnyCPU’s default 8.

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

Sidebar

Related Questions

I have the following data types declaration. typedef struct{ int aa; }A; typedef struct{
The two declarations are as follows: int (*p)[8]; int *p[8];
I've created a structure in a header file as follows: typedef struct { GLfloat
My case is as follows : I have a bunch of functions and declarations
I have a code as follows: int n; int get_the_number(); void some_computations(); int main()
If I have the following pointer variable declarations: int *a; int **c; Regarding to
Non-generic delegate declaration is done as follows: delegate void Print(int arg); where void is
Code is as follows: /* set.h */ struct setElement{ char *element; setElement *next; };
My code is as follows: #include <curl/curl.h> struct callback_data { FILE *output; char *path;
I have my declaration as follows int[] EmpIDs = new int[] { }; ArrayList

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.