![]() |
| Licence | CPOL |
| First Posted | 12 Jul 2007 |
| Views | 28,442 |
| Bookmarked | 21 times |
When 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 marshal_as to support conversions
between .NET Font objects and native Windows HFONT
structures
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 context_node
template class which is used by the marshal_context class to handle conversions that need a context. In the
constructors I create the object that's requested and in the
destructor/finalizer I free the resource. In the case of HFONT, I have called
DeleteObject whereas
for Font, I have called delete (which
calls Dispose). I need not have done that for Font
as the Font finalizer would have come
into play eventually, but for GDI objects it's preferable to
free them as soon as they are not required as they tend to be
rather heavy on the memory side.
Using these conversions is pretty similar to how marshal_as conversions are used for
const char*
or BSTR (where we use a context object too).
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.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Nish Sivakumar![]() United StatesMember |
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. |
| You must Sign In to use this message board. (secure sign-in) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Suggestion
Question
Bug
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Votes of 3 or less require a comment