Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Plugin System – an alternative to GetProcAddress and interfaces

, 25 May 2007 CPOL
A powerful and extensible way of creating plugin-based applications
pluginsystem_src.zip
defmaker.exe
readme.rtf
TestImage1.bmp
TestImage2.tga
defmaker
defmaker.dsp
defmaker.dsw
DLL+EXE
Host.dsw
HostExe.dsp
BMPParser
BMPParser.dsp
Host
Host.dsp
Release
BMPParser.imp
Host.dll
HostExe.exe
TGAParser.imp
TGAParser
TGAParser.dsp
EXE
Host.dsp
Host.dsw
BMPParser
BMPParser.dsp
ImageParser
ImageParser.dsp
Release
BMPParser.imp
Host.exe
TGAParser.imp
TGAParser
TGAParser.dsp
Interface
Host.dsp
Host.dsw
BMPParser
BMPParser.dsp
Release
BMPParser.imp
Host.exe
TGAParser.imp
TGAParser
TGAParser.dsp
// BMPParser.cpp - a very simple BMP file parser
//
// Part of the Plugin System tutorial
//
///////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <stdio.h>
#include "..\Host.h"
#include "..\ImageParser\ImageParser.h"

// CBMPParser inherits from CImageParser
class CBMPParser: public CImageParser
{
public:
	virtual HBITMAP ParseFile( const char *fname );
	virtual bool SupportsType( const char *type ) const;
};

// Parses a BMP file
HBITMAP CBMPParser::ParseFile( const char *fname )
{
	FILE *f=fopen(fname,"rb");
	if (!f) return NULL;

	BITMAPFILEHEADER bmfh;
	memset(&bmfh,0,sizeof(bmfh));
	fread(&bmfh,sizeof(bmfh),1,f);
	BITMAPINFOHEADER bmih;
	memset(&bmih,0,sizeof(bmih));
	fread(&bmih,sizeof(bmih),1,f);
	int width=bmih.biWidth;
	int height=bmih.biHeight;

	if (bmih.biBitCount!=24) {
		// only 24 bit images are supported
		fclose(f);
		return NULL;
	}

	// create the HBITMAP
	void *data;
	HBITMAP bitmap=CreateBitmap(width,height,&data);
	if (!bitmap) {
		fclose(f);
		return NULL;
	}

	// read the pixels
	int pitch=(width*3+3)&~3;
	fread(data,pitch,height,f);

	fclose(f);
	return bitmap;
}

// Notifies the host that the plugin supports the BMP type
bool CBMPParser::SupportsType( const char *type ) const
{
	return stricmp(type,".bmp")==0;
}

static CBMPParser g_BMPParser;

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ivo Beltchev
Software Developer (Senior)
United States United States
Ivo started programming in 1985 on an Apple ][ clone. He graduated from Sofia University, Bulgaria with a MSCS degree. Ivo has been working as a professional programmer for over 12 years, and as a professional game programmer for over 10. He is currently employed in Pandemic Studios, a video game company in Los Angeles, California.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 25 May 2007
Article Copyright 2007 by Ivo Beltchev
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid