![]() |
|||
Extending marshal_as for objects needing contextWhen you have type conversions
which require explicit resource deallocation, you have to handle
it slightly differently. As an example I have written some code
that'll extend namespace msclr
{
namespace interop
{
template<> ref class context_node<System::Drawing::Font^, HFONT>
: public context_node_base
{
private:
System::Drawing::Font^ _font;
public:
context_node(System::Drawing::Font^% to, HFONT from)
{
to = _font = System::Drawing::Font::FromHfont((IntPtr)from);
}
~context_node()
{
this->!context_node();
}
protected:
!context_node()
{
delete _font;
}
};
template<> ref class context_node<HFONT, System::Drawing::Font^>
: public context_node_base
{
private:
HFONT _hFont;
public:
context_node(HFONT& to, System::Drawing::Font^ from)
{
to = _hFont = (HFONT)from->ToHfont().ToPointer();
}
~context_node()
{
this->!context_node();
}
protected:
!context_node()
{
DeleteObject(_hFont);
}
};
}
}
I've added two specializations of the Using these conversions is pretty similar to how HFONT hFont = CreateSampleFont();
//...
marshal_context context;
System::Drawing::Font^ font =
context.marshal_as<System::Drawing::Font^>(hFont);
HFONT hFontCopy = context.marshal_as<HFONT>(font);
//...
DeleteObject(hFont);
That's it. Pretty straightforward to extend and to use. History
LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) About the Author
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff - blog.voidnish.com. Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework. Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog. Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife. Comments and Discussions
|
About Article
This article covers basic marshal_as usage, as well as how to extend marshal_as to support additional type conversions
Top News
The Next Version of Android - Some of What's Coming Get the Insider News free each morning.Related Articles
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Votes of 3 or less require a comment