Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ vs. c#

Status
Not open for further replies.

stonee74

Technical User
Nov 12, 2001
49
CH
Hello There,

Finally I have decided to rewrite an API from C++ to C#.
So far all worked good but some things I do not really understand in C#. Maybe somebody can help me out!

Imagine I have the following Code in C++:


what would be the equivalent in C#? e.g. in respect to this
void* which indicates the address of some data of unknown type?



code:--------------------------------------------------------------------------------
bool Command::Send(void* PacketStart, long PacketSize)
{

if (!socket.Send(PacketStart, PacketSize))
return false;

return true;
}
--------------------------------------------------------------------------------



How would you translate such a C++ struct?
create a class?
What about the array allocation?


code:--------------------------------------------------------------------------------
struct GetRT
{
struct BRTInfo;
enum EType Type;
double Offset;
unsigned short Name[32]; };

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



What can I do to have constants in a separate file.
consts are no longer available i guess...

In C++ :

code:--------------------------------------------------------------------------------
// Unit-Conversion related constants:

const double LengthFactor = 1000.0;

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



What about these adress opertaors and sizeof function
Can i use the System.Runtime.InteropServices.Marshal.SizeOf() Method?


code:--------------------------------------------------------------------------------
bool inline Initi() {CInit Data; return SP(&Data.DataPacket, sizeof(Data.DataPacket));}

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


I really do not understand how to handle my pointers in c#.
Any help is greatly appreciated,

stonee
 
1. You can use enum for constans or declare a constants class.

2. You can use pointers with "unsafe" keyword.

3. struct example:
struct BitmapFileHeader
{
public ushort bfType;
public int bfSize;
public ushort bfReserved1;
public ushort bfReserved2;
public int bfOffBits;
public static int Length = 14;

public byte[] ToByteArray()
{
byte[] ByteArray = new byte[Length];
BitConverter.GetBytes(bfType).CopyTo(ByteArray,0);
BitConverter.GetBytes(bfSize).CopyTo(ByteArray, 2);
BitConverter.GetBytes(bfReserved1).CopyTo(ByteArray, 6);
BitConverter.GetBytes(bfReserved2).CopyTo(ByteArray, 8);
BitConverter.GetBytes(bfOffBits).CopyTo(ByteArray, 10);
return ByteArray;
}
public void FromByteArray(byte[] ByteArray)
{
bfType = BitConverter.ToUInt16(ByteArray, 0);
bfSize = BitConverter.ToInt32(ByteArray, 2);
bfReserved1 = BitConverter.ToUInt16(ByteArray, 6);
bfReserved2 = BitConverter.ToUInt16(ByteArray, 8);
bfOffBits = BitConverter.ToInt32(ByteArray, 10);
}
};

You can use the Length static member instead of sizeof.
4. Use String class for NAME.

See these help pages:

ms-help://MS.VSCC/MS.MSDNVS/csref/html/vclrfbuiltintypes.htm
ms-help://MS.VSCC/MS.MSDNVS/cscon/html/vclrfComparisonBetweenCCSharp.htm
ms-help://MS.VSCC/MS.MSDNVS/vsintro7/html/vxorilanguageequivalentskeywords.htm
ms-help://MS.VSCC/MS.MSDNVS/vsintro7/html/vxgrflanguageequivalentscodeexamples.htm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top