Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, May 6, 2009 8:43 AM
Hi,
I am converting a C application into C# application.
In the C code double pointers are used like,
item_t ** Allitems.
How can I define Allitems in C#?
Ram
All replies (7)
Wednesday, May 6, 2009 1:02 PM ✅Answered | 2 votes
If you're converting a C program to a C# program, you shouldn't be using IntPtr anywhere.
If you're using P/Invoke to call into a C DLL, you may need to use IntPtr, but how to use it very much depends on the C interface.
Which are you doing?
Wednesday, May 13, 2009 6:08 AM ✅Answered
Hi,
Please have a look at the article with shows a method called UsingMarshal :
http://msdn.microsoft.com/en-us/library/2k1k68kw%28VS.85%29.aspx
UsingMarshal calls TestOutArrayOfStructs witch is defined below:
extern "C" PINVOKELIB_API void TestOutArrayOfStructs( int* pSize, MYSTRSTRUCT2** ppStruct )
{
const int cArraySize = 5;
*pSize = cArraySize;
*ppStruct = (MYSTRSTRUCT2*)CoTaskMemAlloc( cArraySize * sizeof( MYSTRSTRUCT2 ));
MYSTRSTRUCT2* pCurStruct = *ppStruct;
char* buffer;
for( int i = 0; i < cArraySize; i++, pCurStruct++ )
{
pCurStruct->size = i;
buffer = (char*)CoTaskMemAlloc( 4 );
strcpy_s( buffer, 4, "***" );
pCurStruct->buffer = buffer;
}
}
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Wednesday, May 6, 2009 8:48 AM
Use List<item_t>
A double pointer like that is a pointer to an array of pointers to items. If item_t is implemented as a class in C#, then creating a single one of them will give you a reference to it (like a pointer) and creating a List<> of them will give you a reference to that list. This will give you the same effect that they are getting in C++, except about a million times easier to manage. :)
public class item_t
{
... whatever
}
List<item_t> Allitems = new List<item_t>();
Allitems.Add(new item_t());
Allitems.Add(new item_t());
etc.
Or you could use a plain array:
item_t[] Allitems = new item_t[10];
item_t[0] = new item_t();
...
etc
Wednesday, May 6, 2009 8:55 AM
Hi Matthew,
Thanks for the reply.
But what if I am using IntPtr for item_t. As the structure of item_t is complex. Can I define it as follows?**
List<IntPtr> AllItems;
**
Ram
Wednesday, May 6, 2009 9:03 AM
Khenat.Ram,
As Mathew shown, where item_t is a complex type and List<item_t> can hold references of a collection of item_t objects.
**List<item_t> AllItems = new List<item_t>();**Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
Thursday, May 7, 2009 5:45 AM
Actually I am making a call to C DLL using P/Invoke.
I am getting pointer from DLL functions and using Marshal.PtrToStructure to create a structure.
Ram
Wednesday, May 13, 2009 5:33 AM
Hi,
Could you please post the defination of the native code and managed code?
HarryPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.