Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2004

Tip: Looking for answers? Try searching our database.

Marshaling the structure array member of Structure in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vini Deep - 23 Apr 2004 05:54 GMT
Hi,

I have a structure defined in a dll which has another structure array as a member. I need to call this Dll from C#. How do I do it?

This is the structure I have in the DLL.

typedef struct FIRST_STRUCT
{
double XSpeed;
int DRate;
unsigned int RotCtrl
} FIRST_STRUCT;

typedef struct SEC_STRUCT
{
char Model[20];
unsigned int SuptTxt;
FIRST_STRUCT CurSpd;
FIRST_STRUCT MaxRdSpd;
unsigned int cNumSpd;
FIRST_STRUCT CrwWrd[40];
FIRST_STRUCT DWrd[20];
} SEC_STRUCT;

__declspec (dllexport) unsigned int CALLBACK MyFunc(SEC_STRUCT * Info);
_________________________________________________________________________

How do I call this from my C# client? I have changed the structure as

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct First_struct
{
public System.Double XSpeed;
public System.Int32 DRate;
public System.UInt32 RotCtrl;
}

How do I marshal the SEC_STRUCT in C#. I tired using the MarshalAs(), but it gave me a run time error 'System.TypeLoadException'

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
public First_struct[] CrwWrd;

Kindly Help.

Thanks

-------------------------------
From: Vini Deep

-----------------------

<Id>1rFOprcgGEK5kR52Loph1Q==</Id
Idael Cardoso - 24 Apr 2004 16:15 GMT
The problem is that SizeConst could only be specified for fundamental types,
it doesn't work for an array of structures. You can find a solution in the
section Some Translation Details of my article C Sharp Ripper at:
http://www.thecodeproject.com/csharp/CSharpRipper.asp#Some_translation_details

Regards,
Idael.

> Hi,
>
[quoted text clipped - 48 lines]
>
> <Id>1rFOprcgGEK5kR52Loph1Q==</Id
chaor - 17 Jun 2004 20:06 GMT
thank you Idael,your article and code really helped me!!
but my trouble doesn't disappear....:(
the struct i used is more complex,but i think it's the same.the code is
following.
now ,it doesn't throw exception,and can pass in the function well,but the
function still
return false,i am sure the dll which contains the function is fine,because
i've tested it
in vc++.
-------Unmanaged
Code------------------------------------------------------------------
typedef struct
{
  char m_username[16];
  char m_password[8];
  char m_userIP[16];
}USERPARA,*PUSERPARA;

typedef struct{
   char m_starthour;
char m_startmin;
char m_stophour;
char m_stopmin;
}RECORDTIME,*PRECORDTIME;

typedef struct{
ULONG m_alarmsenstive;
ULONG m_detect[18][22];
}DETECTSCOPE,*PDETECTSCOPE;

typedef struct{
RECORDTIME m_recordtime[7][4];
ULONG m_workmode[7][4];
BOOL m_bAllDayRecord[7];
}RECORDPLAN,*PRECORDPLAN;

typedef struct
{
BYTE   m_channelName[USER_LEN];
   ULONG  m_streamType;
   ULONG  m_encodeType;
ULONG  m_videobitrate;
ULONG  m_videoFrameRate;
ULONG  m_picturequality;
ULONG  m_bitratetype;
ULONG  m_Iinterval;
   ULONG  m_brightness;
ULONG  m_contrast;
   ULONG  m_tonality;
   ULONG  m_saturation;
BOOL   m_bRecord;
RECORDPLAN  m_recordplan;
DETECTSCOPE m_detectscope;
BOOL   m_bSetcommonuser;
ULONG m_delay;
BOOL    m_bOsd;
BOOL    m_bLogo;
USERPARA m_userpara[6];
ULONG m_userdefinedrate;
}CHANNELPARA,*PCHANNELPARA;
-------------------------------------------------------------------------
--------------Managed
Code-----------------------------------------------------------
[ StructLayout( LayoutKind.Sequential)]
public class USERPARA
{
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=GlobalDefine.USER_LEN)]
 public char[] m_username=new char[GlobalDefine.USER_LEN];
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=8)]
 public char[] m_password=new char[8];
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=16)]
 public char[] m_userIP=new char[16];
}
[ StructLayout( LayoutKind.Sequential)]
public class UserParaList
{
 [MarshalAs(UnmanagedType.ByValArray, SizeConst=6*8)]
 private byte[] Data;
 public USERPARA this [int index]
 {
  get
  {
   if (index<0||index>6)
   {
    throw new IndexOutOfRangeException();
   }
   USERPARA res;
   GCHandle handle = GCHandle.Alloc(Data, GCHandleType.Pinned);
   try
   {
    IntPtr buffer = handle.AddrOfPinnedObject();
    buffer = (IntPtr)(buffer.ToInt32() +
(index*Marshal.SizeOf(typeof(USERPARA))));
    res = (USERPARA)Marshal.PtrToStructure(buffer, typeof(USERPARA));
   }
   finally
   {
    handle.Free();
   }
   return res;
  }
 }
 public UserParaList()
 {
  Data = new byte[6*Marshal.SizeOf(typeof(USERPARA))];
 }
}

[ StructLayout( LayoutKind.Sequential)]
public class DETECTSCOPE
{
 public int m_alarmsenstive;
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=396)]
 public int[] m_detect=new int[396];
}

[ StructLayout( LayoutKind.Sequential)]
public class RECORDTIME
{
 public char m_starthour;
 public char m_startmin;
 public char m_stophour;
 public char m_stopmin;
}

[ StructLayout( LayoutKind.Sequential)]
public class RecordTimeList2
{
 [MarshalAs(UnmanagedType.ByValArray, SizeConst=28*8)]
 private byte[] Data;
 public RECORDTIME this [int x,int y]
 {
  get
  {
   if ( (x < 0) | (y < 0)| (x >= 7)| (y >= 4))
   {
    throw new IndexOutOfRangeException();
   }
   RECORDTIME res;
   GCHandle handle = GCHandle.Alloc(Data, GCHandleType.Pinned);
   try
   {
    IntPtr buffer = handle.AddrOfPinnedObject();
    buffer = (IntPtr)(buffer.ToInt32() +
((x*4+y)*Marshal.SizeOf(typeof(RECORDTIME))));
    res = (RECORDTIME)Marshal.PtrToStructure(buffer, typeof(RECORDTIME));
   }
   finally
   {
    handle.Free();
   }
   return res;
  }
 }
 public RecordTimeList2()
 {
  Data = new byte[28*Marshal.SizeOf(typeof(RECORDTIME))];
 }
}

[ StructLayout( LayoutKind.Sequential)]
public class RECORDPLAN
{
 public RecordTimeList2 m_recordtime=new RecordTimeList2();
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=28)]
 public int[] m_workmode=new int[28];
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=7)]
 public bool[] m_bAllDayRecord=new bool[7];
}

[ StructLayout( LayoutKind.Sequential)]
public class CHANNELPARA
{
 [MarshalAs(UnmanagedType.ByValArray,SizeConst=GlobalDefine.USER_LEN)]
 public byte[] m_channelName=new byte[GlobalDefine.USER_LEN];
 public int  m_streamType;
 public int  m_encodeType;
 public int  m_videobitrate;
 public int  m_videoFrameRate;
 public int  m_picturequality;
 public int  m_bitratetype;
 public int  m_Iinterval;
 public int  m_brightness;
 public int  m_contrast;
 public int  m_tonality;
 public int  m_saturation;
 public bool   m_bRecord;
 public RECORDPLAN  m_recordplan= new RECORDPLAN();
 public DETECTSCOPE m_detectscope=new DETECTSCOPE();
 public bool   m_bSetcommonuser;  public int m_delay;
 public bool    m_bOsd;
 public bool    m_bLogo;
 public UserParaList m_userpara =new UserParaList();
 public int m_userdefinedrate;
}
-------------------------------------------------------------------------
chaor - 17 Jun 2004 22:58 GMT
Hi Idael,
I made some mistake in my last post,so the function returns false.And now
I've solved it using your solution!
Thank you!!!!

Regards,
chaor

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.