Need help in declaring a C# structure from C structure 
Author Message
 Need help in declaring a C# structure from C structure

Hi all,

I am trying to declare the following C structure into C#.

typedef struct {     /* list of 0 or more object names */
                            unsigned int numItems;
                            Name *nameList;
                      } NameList;

And Name is defind as

typedef char Name[128];

More over the 'callee' fills this structure with data and I should just pass
a placeholder for the list. How do I accomplish this in C# ? i.e. my
function GetNameList( NameList * nameList) in 'C' returns the list filled
with names.

Can I declare it like,

[DllImport("mylib.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern int GetNameList( ref NameList nameList);

Will it work?

or do i need to use the marshaler manually and use the 'unsafe' keyword? If
yes, can anybody show me how?

I ran out of options. Any help is appreicated.

Thanks
Suresh



Sun, 31 Oct 2004 02:29:57 GMT  
 Need help in declaring a C# structure from C structure
Don't cross-post, this IS interop !

Your description is ambiguous...
 - Who sets field NameList.numItems ?
 - Who does allocate NameList.nameList  ?
 - Who does free it?

It would help if you post the complete C(++) code
of an existing caller.

--
  NETMaster  (Thomas Scheidegger)
  http://www.cetus-links.org/oo_csharp.html

Quote:

> I am trying to declare the following C structure into C#.
> typedef struct {     /* list of 0 or more object names */
>                             unsigned int numItems;
>                             Name *nameList;
>                       } NameList;
> And Name is defind as
> typedef char Name[128];
> More over the 'callee' fills this structure with data and I should just pass
> a placeholder for the list. How do I accomplish this in C# ? i.e. my
> function GetNameList( NameList * nameList) in 'C' returns the list filled
> with names.
> Can I declare it like,
> [DllImport("mylib.dll", CharSet=CharSet.Auto, SetLastError=true)]
> static extern int GetNameList( ref NameList nameList);
> Will it work?
> or do i need to use the marshaler manually and use the 'unsafe' keyword? If
> yes, can anybody show me how?
> I ran out of options. Any help is appreicated.



Sun, 31 Oct 2004 14:22:29 GMT  
 Need help in declaring a C# structure from C structure
You can do like this, where the nameList array is allocated in the dll;:
Declare similar structs in C#

[StructLayout(LayoutKind.Sequential)]
struct NameList
{
     public int noItems;
     public IntPtr nameList; // the array pointer

Quote:
};

//struct to wrap the char name[128] if is a simple char array and not a null
terminated string

[StructLayout(LayoutKind.Sequential)]
struct Name
{
 [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
 char[] name;

Quote:
}

Dll import function

[DllImport("dllname", EntryPoint = "namefunction", CharSet=CharSet.Ansi)]
extern static void GetNameList(ref NameList nameList);

If the char name[128] is s simple char array you can do something like this

    NameList nameListStruct = new NameList(); //alloc struct
   GetNameList(ref nameListStruct); //pass the ref

   Name[] names = new Name[nameListStruct.noItems];

  IntPtr pStruct = nameListStruct.nameList;
   int sizeOfName = Marshal.SizeOf(typeof(Name)); // get the size of the
Name -> 128
   for(int i=0;i<nameListStruct.noItems;i++)
   {
        names[i] = (Name)Marshal.PtrToStructure(pStruct, typeof(Name)); //
from a pointer get a struct
        pStruct = (IntPtr)( (int)pStruct + sizeOfName ); // increment the
pointer to get the next char array
   }

If the char name[128] is in fact a null terminated string you can do
something like this

   NameList nameListStruct = new NameList();
   GetNameList(ref nameListStruct);

   string[] names = new String[nameListStruct.noItems];
   IntPtr pStruct = nameListStruct.nameList;
   int sizeOfName = 128; // the size of the char[] array
   for(int i=0;i<nameListStruct.noItems;i++)
   {
        names[i] = Marshal.PtrToStringAnsi(pStruct); // from pointer get a
string
        pStruct = (IntPtr)( (int)pStruct + sizeOfName ); // increment the
pointer with the size of the array to get the next string
   }

Dumitru Sbenghe


Quote:
> Hi all,

> I am trying to declare the following C structure into C#.

> typedef struct {     /* list of 0 or more object names */
>                             unsigned int numItems;
>                             Name *nameList;
>                       } NameList;

> And Name is defind as

> typedef char Name[128];

> More over the 'callee' fills this structure with data and I should just
pass
> a placeholder for the list. How do I accomplish this in C# ? i.e. my
> function GetNameList( NameList * nameList) in 'C' returns the list filled
> with names.

> Can I declare it like,

> [DllImport("mylib.dll", CharSet=CharSet.Auto, SetLastError=true)]
> static extern int GetNameList( ref NameList nameList);

> Will it work?

> or do i need to use the marshaler manually and use the 'unsafe' keyword?
If
> yes, can anybody show me how?

> I ran out of options. Any help is appreicated.

> Thanks
> Suresh



Sun, 31 Oct 2004 18:11:52 GMT  
 Need help in declaring a C# structure from C structure
What do you mean don't cross post who died and made you king of the
newsgroups.  If cross posting isn't allowed then it wouldn't be an
option in all of the viewers.

My $0.02 USD

Nick

Quote:

> Don't cross-post, this IS interop !

> Your description is ambiguous...
>  - Who sets field NameList.numItems ?
>  - Who does allocate NameList.nameList  ?
>  - Who does free it?

> It would help if you post the complete C(++) code
> of an existing caller.

> --
>   NETMaster  (Thomas Scheidegger)
>   http://www.cetus-links.org/oo_csharp.html


> > I am trying to declare the following C structure into C#.
> > typedef struct {     /* list of 0 or more object names */
> >                             unsigned int numItems;
> >                             Name *nameList;
> >                       } NameList;
> > And Name is defind as
> > typedef char Name[128];
> > More over the 'callee' fills this structure with data and I should just pass
> > a placeholder for the list. How do I accomplish this in C# ? i.e. my
> > function GetNameList( NameList * nameList) in 'C' returns the list filled
> > with names.
> > Can I declare it like,
> > [DllImport("mylib.dll", CharSet=CharSet.Auto, SetLastError=true)]
> > static extern int GetNameList( ref NameList nameList);
> > Will it work?
> > or do i need to use the marshaler manually and use the 'unsafe' keyword? If
> > yes, can anybody show me how?
> > I ran out of options. Any help is appreicated.



Sun, 31 Oct 2004 23:44:52 GMT  
 Need help in declaring a C# structure from C structure
Thanks Dumitru for the response. It shed lot of light on things I was
wondering how to do.
I will check it out.
-Suresh


Quote:
> You can do like this, where the nameList array is allocated in the dll;:
> Declare similar structs in C#

> [StructLayout(LayoutKind.Sequential)]
> struct NameList
> {
>      public int noItems;
>      public IntPtr nameList; // the array pointer
> };

> //struct to wrap the char name[128] if is a simple char array and not a
null
> terminated string

> [StructLayout(LayoutKind.Sequential)]
> struct Name
> {
>  [MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
>  char[] name;
> }

> Dll import function

> [DllImport("dllname", EntryPoint = "namefunction", CharSet=CharSet.Ansi)]
> extern static void GetNameList(ref NameList nameList);

> If the char name[128] is s simple char array you can do something like
this

>     NameList nameListStruct = new NameList(); //alloc struct
>    GetNameList(ref nameListStruct); //pass the ref

>    Name[] names = new Name[nameListStruct.noItems];

>   IntPtr pStruct = nameListStruct.nameList;
>    int sizeOfName = Marshal.SizeOf(typeof(Name)); // get the size of the
> Name -> 128
>    for(int i=0;i<nameListStruct.noItems;i++)
>    {
>         names[i] = (Name)Marshal.PtrToStructure(pStruct, typeof(Name)); //
> from a pointer get a struct
>         pStruct = (IntPtr)( (int)pStruct + sizeOfName ); // increment the
> pointer to get the next char array
>    }

> If the char name[128] is in fact a null terminated string you can do
> something like this

>    NameList nameListStruct = new NameList();
>    GetNameList(ref nameListStruct);

>    string[] names = new String[nameListStruct.noItems];
>    IntPtr pStruct = nameListStruct.nameList;
>    int sizeOfName = 128; // the size of the char[] array
>    for(int i=0;i<nameListStruct.noItems;i++)
>    {
>         names[i] = Marshal.PtrToStringAnsi(pStruct); // from pointer get a
> string
>         pStruct = (IntPtr)( (int)pStruct + sizeOfName ); // increment the
> pointer with the size of the array to get the next string
>    }

> Dumitru Sbenghe



> > Hi all,

> > I am trying to declare the following C structure into C#.

> > typedef struct {     /* list of 0 or more object names */
> >                             unsigned int numItems;
> >                             Name *nameList;
> >                       } NameList;

> > And Name is defind as

> > typedef char Name[128];

> > More over the 'callee' fills this structure with data and I should just
> pass
> > a placeholder for the list. How do I accomplish this in C# ? i.e. my
> > function GetNameList( NameList * nameList) in 'C' returns the list
filled
> > with names.

> > Can I declare it like,

> > [DllImport("mylib.dll", CharSet=CharSet.Auto, SetLastError=true)]
> > static extern int GetNameList( ref NameList nameList);

> > Will it work?

> > or do i need to use the marshaler manually and use the 'unsafe' keyword?
> If
> > yes, can anybody show me how?

> > I ran out of options. Any help is appreicated.

> > Thanks
> > Suresh



Mon, 01 Nov 2004 02:01:09 GMT  
 Need help in declaring a C# structure from C structure

Quote:
> What do you mean don't cross post who died and made you king of the
> newsgroups.  If cross posting isn't allowed then it wouldn't be an
> option in all of the viewers.

I agree - crossposting is a good thing when done correctly.

--
Arild Fines

- The computer was conceived as a tool to reduce complexity. Some people
found this loss of complexity unacceptable, and developed UNIX to
reintroduce it.



Mon, 01 Nov 2004 21:19:05 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. structure of array of structure : help needed

2. Newbie: separate big .cs file into small .cs files

3. newbe/cs student, need help w/ code

4. Help with structuring a structure:)

5. Problem declaring/initialising array of structures

6. Structures - declaring member variable's bit widths

7. defining and declaring a structure in the same file

8. Declaring mutually referential structures

9. Declaring externally-defined structures

10. Declaring Structures in ATL

11. Question: declare vs define a structure

12. declaring variable withing structure blocks

 

 
Powered by phpBB® Forum Software