Thursday, November 22, 2007

From Jason Olson.

//code starts here

#pragma once
#include
<msclr\marshal.h>
#include <msclr\marshal_windows.h>

namespace msclr {
namespace interop {

////////////////////////////////////////////////////////////////////
// GUID Conversions
////////////////////////////////////////////////////////////////////

template<>
inline _GUID marshal_as<_GUID, System::Guid>(const System::Guid& from) {

    System::Guid^ source = from;
    array<Byte>^ guidData = source->ToByteArray();
    pin_ptr<Byte> data = &(guidData[ 0 ]);
    return *(_GUID *)data;

}

template<>
inline Guid marshal_as<System::Guid, _GUID>(const _GUID& from) {
    
return System::Guid( from.Data1, from.Data2, from.Data3, from.Data4[ 0 ], from.Data4[ 1 ],
                                 from.Data4[ 2 ], from.Data4[ 3 ], from.Data4[ 4 ], from.Data4[ 5 ],
                                 from.Data4[ 6 ], from.Data4[ 7 ] );

}

}

}

//code ends here

At first glance the pin_ptr is a little scary, but this code doesn't return it, it dereferences it. So no "GC hole" to worry about.

Kate

posted on 11/22/2007 8:41:32 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]

From Jason Olson.

//code starts here

#pragma once
#include
<msclr\marshal.h>
#include <msclr\marshal_windows.h>
namespace msclr {
namespace interop {

////////////////////////////////////////////////////////////////////
// File I/O Conversions (constants from System::IO)
///////////////////////////////////////////////////////////////////

template<>
inline DWORD marshal_as<DWORD, System::IO::FileMode>(const System::IO::FileMode& from) {

if (from != System::IO::FileMode::Append)
   
return (DWORD)from;
else
   
return (DWORD)System::IO::FileMode::OpenOrCreate;

}

template<>
inline DWORD marshal_as<DWORD, System::IO::FileAccess>(const System::IO::FileAccess& from) {
if (from == System::IO::FileAccess::Read)
   
return GENERIC_READ;
else
   
return GENERIC_WRITE;

}

template<>
inline DWORD marshal_as<DWORD, System::IO::FileShare>(const System::IO::FileShare& from) {
   
return (DWORD)from;

}

}

}

//code ends here

Nice and simple.

Kate

posted on 11/22/2007 8:35:35 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]
 Monday, November 19, 2007

Here we are!

What's going on here? Think pinvoke.net for the marshaling library. Send me your specializations (my email address isn't hard to find, or comment on this post -- if I use comments I'll remove them from here as I go) and I'll post them, one post per specialization, after the barest of quality control (I might make sure your code compiles) and then others can find them and comment on them. Together we will build a full-size marshaling library for all the common structs and classes that might pass back and forth across the managed-native boundary.

Some of you may have some specializations already written. Great! I'll post them as I get them and if I can't keep up, I have a volunteer team waiting to help. So let's go!

Kate Gregory

 

posted on 11/19/2007 10:00:51 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0]