Click here to Skip to main content
15,886,519 members
Articles / Desktop Programming / MFC

Dynamically generating images in ISAPI extension using GDI+ with live demo

Rate me:
Please Sign up or sign in to vote.
3.88/5 (8 votes)
3 Oct 20022 min read 65.3K   424   26  
A class wrapper to write GDI+ images to the client browser using an ISAPI extension.
#include "StdAfx.h"
#include "gdipinitializer.h"

long CGDIpInitializer::m_initcount=0;  

CGDIpInitializer::CGDIpInitializer(bool bInitCtorDtor) 
: m_bInitCtorDtor(bInitCtorDtor), 
			m_bInited(false), m_hMap(NULL), m_gdiplusToken(NULL), 
			m_gdiplusStartupInput(NULL)
{
	if (m_bInitCtorDtor) 
		Initialize();
}

CGDIpInitializer::~CGDIpInitializer()  
{
	if (m_bInitCtorDtor) 
		Deinitialize();
}

void CGDIpInitializer::Initialize()
{
		if (!m_bInited) {
			TCHAR buffer[1024];
			_stprintf(buffer, _T("GDIPlusInitID=%x"), GetCurrentProcessId());
			m_hMap = CreateFileMapping((HANDLE) INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE | SEC_COMMIT, 0, sizeof(long), buffer);
			if (m_hMap != NULL) {
				// We might have a winner
				if (GetLastError() == ERROR_ALREADY_EXISTS) { 
					CloseHandle(m_hMap); 
				} else {
					// Yes, we have a winner
					m_bInited = true;
					Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
					TRACE(_T("Inited GDIPlus\n"));
				}
			}
		}
		m_initcount++;
}

void CGDIpInitializer::Deinitialize()
{
	using namespace Gdiplus;

	m_initcount--;
	if (m_bInited && m_initcount == 0) {
		TRACE(_T("GDIPlus shutdown\n"));
		Gdiplus::GdiplusShutdown(m_gdiplusToken);
		CloseHandle(m_hMap);
		m_bInited = false;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions