Click here to Skip to main content
15,886,056 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a structure in c++ with pointers as the member of that structure which is used to point to an array of another type of structures. I dont know the size of that array as that is dynamically allocated by a thirdparty dll and that to reading a file. Please help me with this

What I have tried:

Hi i am very new to c# and have encountered a problem.
i have to port an application from c++ to c# and in c++ there is a structure of the format as shown
struct Frame
{
int frameID;
dirData* arr_dirdata;
}

struct dirData
{
int nFiles;
filesData* arr_fileData;
}

These structures are used to read data from a file and that data is actually a number of dirData structure array and filesData structure array. The structure is actually allocated by a third party dll. i dont have the c# version of the dll so i am using the c++ version and wrapping around that.

when i am converting the same structure to c# i defined as following
[StructLayout(LayoutKind.Sequential)]
struct Frame
{
int frameID;
[MarshalAs(UnmanagedType.LPArray, SizeConst = 1)]
dirData[] arrdirData;
}
How to define an array in my case.


Thanks in Advance
AK
Posted
Updated 7-Apr-16 22:04pm
Comments
Sergey Alexandrovich Kryukov 7-Apr-16 23:08pm    
If you are porting, why would you use P/Invoke? It's not called "porting".
—SA
ajaiisnow 8-Apr-16 9:29am    
hi,
This is not porting i am rewriting the code actually.

Thanks
Sergey Alexandrovich Kryukov 8-Apr-16 9:35am    
All right, thank you for the clarification. So, I guess the problem is to keep using 3rd-party unit? Did you consider using C++/CLI, in case you have some valuable C++ code? This way, you could couple it with implicit P/Invoke via mixed (managed + unmanaged) code...
—SA
BillWoodruff 8-Apr-16 13:16pm    
Does the data here potentially have a "depth" greater than one level: in other words do you need to map to an Array of Arrays of Arrays ... and so on. Or, can you always rely on the data being expressed as an Array of Arrays ? I would suggest you map to C# generic lists, not Arrays.
ajaiisnow 8-Apr-16 14:11pm    
yes the data what ever comes from the file is an array that points to a list of directories and the pointer in each of the dirData structure in turn points to an array of files. and this memory is returned from inside the dll call. so that is why i am not sure of using a list

Thanks

1 solution

You already did it for static arrays
dirData[] arrdirData;

Now just set the initial size you want lets make it 50
arrdirData = new dirData[50];

Or if you want it in one line
dirData[] arrdirData = new dirdata[50];

However in C# there you can create dynamic arrays using the generic list class
C#
List<dirdata> arrdirData = new List <dirdata>();

You just the use aardirData.Add to add etc. You can look at the generic list
functions like add, delete, count, replace etc. You will need to include the
generic list header at the top of your file.
 
Share this answer
 
v2
Comments
ajaiisnow 8-Apr-16 9:20am    
hi,
Thanks for the suggestion. Actually in my case the structure has a pointer to the dirData structure and to that the third party dll allocates the required memory and returns the pointer. With the other member that gives me the number of files i have to access that. I want to actually mimic the same feature in c#. so i what i want to know is that in the structure should i keep a place holder for one structure and or a IntPtr.

Thanks in advance
AK
leon de boer 10-Apr-16 4:08am    
You have no choice but to then translate the returned byte structure, carry the structure in byte form or use the [StructLayout(LayoutKind.Explicit)] operation and define matching structure to the DLL explicitly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900