Thursday, December 13, 2007

From Nish.

//code starts here
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);
            }
        };
    }
}
//code ends here

Wow. Our first use of a context. Instead of writing a function that implements a specialization of the template function, you write a class that implements a specialization of the template class. Since it's a class, it can have member variables, which you clean up when the context goes out of scope.

Here's how you might use it:

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);

Kate

posted on 12/13/2007 5:15:57 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1]

Related Posts:
RECT, CRect, and System::Drawing::Rectangle
_GUID and System::Guid
Some handy file system flags

Tracked by:
http://www.google.com/search?q=ixmeozfw [Pingback]
http://www.wintec.ac.nz/index.asp?PageID=2145828112 [Pingback]
http://www.surfrider.org/press_releases.aspx?PR_Id=216 [Pingback]
http://www.wintec.ac.nz/index.asp?PageID=2145828098 [Pingback]
http://www.legis.state.wv.us/News_release/newsrelease_RecordView1.cfm?RecordID=1... [Pingback]
http://www.wintec.ac.nz/index.asp?PageID=2145828132 [Pingback]
http://www.ovt.com/products/part_detail.asp?id=155 [Pingback]
http://www.ovt.com/products/part_detail.asp?id=186 [Pingback]
http://www.surfrider.org/press_releases.aspx?PR_Id=184 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=215 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=179 [Pingback]
http://www.xbrl.org/us/nmpxbrl.aspx?id=233 [Pingback]
http://www.surfrider.org/press_releases.aspx?PR_Id=195 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=183 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=203 [Pingback]
http://www.xbrl.org/us/nmpxbrl.aspx?id=224 [Pingback]
http://www.surfrider.org/press_releases.aspx?PR_Id=225 [Pingback]
http://www.legis.state.wv.us/News_release/newsrelease_RecordView1.cfm?RecordID=1... [Pingback]
http://www.datcp.state.wi.us/press_release/result.jsp?prid=2071 [Pingback]
http://www.datcp.state.wi.us/press_release/result.jsp?prid=2100 [Pingback]
http://www.landandfarm.com/lf/news/display_article.asp?cid=684 [Pingback]
http://www.xbrl.org/us/nmpxbrl.aspx?id=237 [Pingback]
http://www.legis.state.wv.us/News_release/newsrelease_RecordView1.cfm?RecordID=1... [Pingback]
http://www.ovt.com/products/part_detail.asp?id=145 [Pingback]
http://www.stockroom.com/sandm/product1.aspx?product_id=784757 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=209 [Pingback]
http://www.photospin.com/free_tips.asp?archiveID=135 [Pingback]
http://www.landandfarm.com/lf/news/display_article.asp?cid=707 [Pingback]
http://www.landandfarm.com/lf/news/display_article.asp?cid=664 [Pingback]
http://www.datcp.state.wi.us/press_release/result.jsp?prid=2081 [Pingback]
http://morningside.edu/mics/_notes/pages/soma/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/cialis/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lipitor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/rainbow-brite/index.html [Pingback]
http://blastpr.com/wiki/js/pages/rainbow-brite/index.html [Pingback]
http://blastpr.com/wiki/js/pages/hoodia/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cymbalta/index.html [Pingback]
http://blastpr.com/wiki/js/pages/paxil/index.html [Pingback]
http://blastpr.com/wiki/js/pages/prozac/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/melatonin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/coumadin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/wellbutrin/index.html [Pingback]
http://blastpr.com/wiki/js/pages/cialis/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/accutane/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prozac/index.html [Pingback]
http://blastpr.com/wiki/js/pages/zoloft/index.html [Pingback]
http://blastpr.com/wiki/js/pages/tramadol/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celebrex/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/nexium/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/paxil/index.html [Pingback]
http://blastpr.com/wiki/js/pages/effexor/index.html [Pingback]
http://blastpr.com/wiki/js/pages/lipitor/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/lexapro/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/celexa/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/prilosec/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/wellbutrin/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/viagra/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/hoodia/index.html [Pingback]
http://blastpr.com/wiki/js/pages/nexium/index.html [Pingback]
http://morningside.edu/mics/_notes/pages/ultram/index.html [Pingback]
http://blastpr.com/wiki/js/pages/celexa/index.html [Pingback]
http://blastpr.com/wiki/js/pages/clomid/index.html [Pingback]
http://blastpr.com/wiki/js/pages/ultram/index.html [Pingback]
2/6/2008 4:30:40 PM (Eastern Standard Time, UTC-05:00)
Having looked at the MSDN page about specialising the marshalling (see link) it seems that:
"Marshaling requires a context only when you marshal from managed to native data types and the native type you are converting to does not have a destructor for automatic clean up"

Is there any benefit (other than symmetry) to marshalling from HFONT to Font^ in a context class?

(See link at http://msdn2.microsoft.com/en-us/library/bb384865.aspx)
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):