Sign in to follow this  

[.net] I'm about to do some coding in C++/CLI... any pitfalls I should know about?

This topic is 4200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I've been writing a fairly large project with C#/MDX, and once I got to where I was ready to write a model loader, I realized how much easier it would be in C++ (So, instead of loading in 4 bytes at a time and BitConverter'ing all of them, I could just do file.read((char*)&header, sizeof(header))). So, I decided to write the model/map parser with C++/CLI as a class library and call it from C#. 1) Is there some reason why this might not be feasible? I can't think of any, but I tend to start projects like this and having to stop halfway through because of various problems that I didn't forsee. 2) Am I making parsing the file in C# harder than it should be?

Share this post


Link to post
Share on other sites
Just an idea for you. You can make your classes for your map [Serializable] and then just serialize and stream your classes to the file, you can then reverse the process to bring them back in. Nice and easy.

theTroll

Share this post


Link to post
Share on other sites
Why are you loading 4 bytes individually and then converting? Would BinaryReader work? It has methods like ReadInt32 and ReadUInt32.

Update: NM I think I misread. You might want to try (untested code):

unsafe bool ReadData(Stream stream, ref object obj)
{
bool result = false;
int dataSize = Marshal.SizeOf(obj);

// read header into byte array
byte[] dataBytes = new byte[dataSize];
if (stream.Read(dataBytes, 0, dataBytes.Length) == dataBytes.Length)
{
fixed (byte* dataPointer = dataBytes)
{
// get IntPtr address for byte array
IntPtr dataBuffer = Marshal.UnsafeAddrOfPinnedArrayElement(dataBytes, 0);

// convert bytes to your data object
Marshal.PtrToStructure(dataBuffer, obj);
result = true;
}
}

return result;
}

// example:
struct MyHeader
{
int asdf;
int bsdf;
//...
}

Stream stream;
MyHeader header = new MyHeader();
ReadData(stream, ref header);


[Edited by - mutex on June 13, 2006 2:56:56 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by mutex
Why are you loading 4 bytes individually and then converting? Would BinaryReader work? It has methods like ReadInt32 and ReadUInt32.

Update: NM I think I misread. You might want to try (untested code):
*** Source Snippet Removed ***


Wow, thanks for showing me how to do that. Every time I think I have a pretty deep knowledge of .NET I get hit with a reality check like this :p I modified it a bit to work out some of the problems (mainly not being able to pass anything but an object as a ref object, which causes problem with structs due to boxing.) How's this look? Anything I could fix?


[STAThread( )]
static void Main( string[] args )
{
Header head = new Header( );

Stream file = File.Open( @"Models\a_grenades.md2", FileMode.Open );

ReadData( file, ref head );

if( head.Magic == 844121161 )
{
System.Windows.Forms.MessageBox.Show( "Success!" );
}

file.Close( );
}

[StructLayout( LayoutKind.Sequential )]
public struct Header
{
public int Magic;
}

public static unsafe bool ReadData<T>( Stream stream, ref T obj )
{
bool result = false;

int dataSize = Marshal.SizeOf( obj );

//read header into byte array
byte[] dataBytes = new byte[dataSize];

if( stream.Read( dataBytes, 0, dataBytes.Length ) == dataBytes.Length )
{
//Pin dataBytes in place so it doesn't get moved around by the GC.
fixed( byte* ptr = dataBytes )
{
//get IntPtr to data
IntPtr dataBuffer = Marshal.UnsafeAddrOfPinnedArrayElement( dataBytes, 0 );

//convert to the data object
obj = (T)Marshal.PtrToStructure( dataBuffer, typeof( T ) );

result = true;
}
}

return result;
}
}


Share this post


Link to post
Share on other sites

This topic is 4200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this