G
Guest
Hi there,
I had some problems converting C -> C#.
C codes:
unsigned char BYTE;
unsigned short puint8;
typedef struct {
BYTE ValueIn[255];
BYTE ValueOut[255];
} STestA;
typedef struct {
int somevalue;
puint8 ValueIn;
puint8 ValueOut;
} STestB;
typedef STestB *PSTestB;
EXTERN int WINAPI PushCommand(int somevar, PSTestB mypstestb)
{
STestA mySTestA;
memset(mySTestA.ValueIn, 0x00, 256);
memset(mySTestA.ValueOut, 0x00, 256);
if (something)
{
memcpy(mySTestA.ValueIn, mypstestb.ValueIn, sizeofValueIn);
}
}
Okay, i try to convert to c#.
C# codes:
public const int STRLEN = 256;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct STestA
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=STRLEN)]
public byte[] ValueIn;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=STRLEN)]
public byte[] ValueOut;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct STestB
{
public int somevalue;
public IntPtr ValueIn;
public IntPtr ValueOut;
public STestB(int a)
{
this.somevalue = a;
this.ValueIn =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(System.UInt16)));
this.ValueOut =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(System.UInt16)));
}
}
public int PushCommand(int somevar, STestB myTestB)
{
STestA myTestA = new STestA();
myTestB = new STestB(0);
// ignore memset, coz i had allocated 256 spaces in the structure
if (something)
{
// how do i copy the values (short) from IntPtr to byte[] ?
// i know you can do this, to read..
// e.g ushort x;
// x = (ushort)Marshal.ReadInt16(Structure.Data);
}
}
Any help please? Am i on the right track doing this? The most important how
do i do memory copy in C#?
Thanks.
I had some problems converting C -> C#.
C codes:
unsigned char BYTE;
unsigned short puint8;
typedef struct {
BYTE ValueIn[255];
BYTE ValueOut[255];
} STestA;
typedef struct {
int somevalue;
puint8 ValueIn;
puint8 ValueOut;
} STestB;
typedef STestB *PSTestB;
EXTERN int WINAPI PushCommand(int somevar, PSTestB mypstestb)
{
STestA mySTestA;
memset(mySTestA.ValueIn, 0x00, 256);
memset(mySTestA.ValueOut, 0x00, 256);
if (something)
{
memcpy(mySTestA.ValueIn, mypstestb.ValueIn, sizeofValueIn);
}
}
Okay, i try to convert to c#.
C# codes:
public const int STRLEN = 256;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct STestA
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=STRLEN)]
public byte[] ValueIn;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=STRLEN)]
public byte[] ValueOut;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct STestB
{
public int somevalue;
public IntPtr ValueIn;
public IntPtr ValueOut;
public STestB(int a)
{
this.somevalue = a;
this.ValueIn =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(System.UInt16)));
this.ValueOut =
Marshal.AllocHGlobal(Marshal.SizeOf(typeof(System.UInt16)));
}
}
public int PushCommand(int somevar, STestB myTestB)
{
STestA myTestA = new STestA();
myTestB = new STestB(0);
// ignore memset, coz i had allocated 256 spaces in the structure
if (something)
{
// how do i copy the values (short) from IntPtr to byte[] ?
// i know you can do this, to read..
// e.g ushort x;
// x = (ushort)Marshal.ReadInt16(Structure.Data);
}
}
Any help please? Am i on the right track doing this? The most important how
do i do memory copy in C#?
Thanks.