473,378 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C -> C# Marshaling / IntPtr - any help?

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.
--
Regards,
Chua Wen Ching :)
Nov 16 '05 #1
1 9885
In C# you might find the "System.Buffer" class useful for manipulating byte
arrays.

As for "best practices" you are going to have to do an awful lot of work
marshalling data between C & C# - as you have already discovered.

I would suggest you try to 'decouple' your managed code in C# and your
unmanaged code in C/C++ as much as possible.
Perhaps you could build a managed C++ wrapper in VS.NET, in which you do all
that horrible data conversion, and keep your C# "clean" managed code.

If you have the C source code, is compiling it in VS.NET C++ an option?
That'll make things easier.

I've never tried anything like this, these are just some ideas that may be
worth considering. Hope it helps.

Richard.

"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:44**********************************@microsof t.com...
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.
--
Regards,
Chua Wen Ching :)

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Julia Briggs | last post by:
My server host is configured not to allow, AddType application/x-httpd-php .xyz I understand the RewriteEngine can be used to pass requests the same way like: RewriteEngine on RewriteBase ...
3
by: TJ | last post by:
Hi, I've been referring to many codes and books and always see that the stream insertion operators are overloaded as friends. Why is that? Are there any other overloading that need the same type...
3
by: Craig | last post by:
I'm trying to translate the CompareFileInfoEntries class from this page: http://aspnet.4guysfromrolla.com/articles/060403-1.2.aspx I have to just be missing punctuation or something dumb...
5
by: Chad Beckner | last post by:
First, sorry for crossposting, not sure exactly where to place this question... I can not seem to find a way to get a users (or my) tokenGroups from ADS using VB.NET. I have seen several...
4
by: Michael | last post by:
Hi Everyone, Is there a way to get the schema from the database using ADO.NET like you could with ADOX. I would like to write a small project that will take the DB structure and create functions...
1
by: frey | last post by:
i tried to create a dropdown menu and use the option as a control to change content inside another text area the code is like this: <select name="xxxx"><option onclick="changeunitprice(29.87)"...
0
by: Eric | last post by:
Visual C++ 2005 Express MVP's and experience programmer's only please!... I need to get the number of lines in a textbox so I can insert them into a listview. The text comes from my database...
1
by: CADD | last post by:
I've used http://www.bloglines.com to add RSS feeds to my sites there and setup javascript in my website to search bloglines for the input term. The code below works perfect, but I have 1 minor...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.