<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>krishnaraj varma&#039;s blog &#187; C++/CLI</title>
	<atom:link href="http://www.krvarma.com/category/posts/cppcli/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krvarma.com</link>
	<description></description>
	<lastBuildDate>Sun, 31 Oct 2010 15:47:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Introduction to Plug-in Architecture</title>
		<link>http://www.krvarma.com/2008/08/introduction-to-plug-in-architecture/</link>
		<comments>http://www.krvarma.com/2008/08/introduction-to-plug-in-architecture/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 12:25:07 +0000</pubDate>
		<dc:creator>Krishnaraj Varma</dc:creator>
				<category><![CDATA[C++/CLI]]></category>

		<guid isPermaLink="false">http://www.krvarma.com/?p=55</guid>
		<description><![CDATA[Download the source code A plug-in is a software component to extent the functionality of an application. A Media player is a typical example of applications which make use of the plug-in architecture. All most all Media Players available now uses plug-in modal to deal with different input file formats and output device. Winamp, a [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Download the <a href="http://www.krvarma.com/files/ImageViewer.zip">source code</a></p>
<p>A plug-in is a software component to extent the functionality of an application. A Media player is a typical example of applications which make use of the plug-in architecture. All most all Media Players available now uses plug-in modal to deal with different input file formats and output device. Winamp, a popular media player, heavily uses plugging for input, output, visualization, DSP, etc&#8230; This is same in the case of Microsoft Media Player also.</p>
<p>As I already stated, a plug-in is a software component to extent an application. In Microsoft Windows, a plug-in is normally a DLL or a COM component. An application which consumes or uses the plug-in is called plug-in host. The host application loads the plug-in at start or when it is really needed to load the plug-in. Typically the host application scans a particular folder for the plug-in and loads all Plug-ins or  it looks a number of predefined folders which is specified is a configuration file of registry. The host application collects the details of all the Plug-ins and gives the user the freedom to choose which plug-in to use or automatically choose the plug-in to use according to the user input. For example in case of media players, the user can choose which visualization plug-in should be active at a time. In case of which file to play, the host determine which plug-in to use as per the user&#8217;s selection.</p>
<p>The host application uses a well defined protocol to communicate with the plug-in. The protocol defines which functions we have to export in case the plug-in is a DLL or which interface we have to implement in case of a COM component. The host can make use of any component which confirms this protocol. So the host does not depend on a particular plug-in but all Plug-ins depends on the host.</p>
<p>The sample application presented here is a simple Image Viewer application. It will display three different image files, <a href="http://en.wikipedia.org/wiki/JPEG">JPEG</a> and <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics">PNG</a> files. For JPEG and PNG images we use LIGJPEG and LIBPNG external libraries since these are very complex formats. Our Plug-ins will be simple DLL files. The host will load these DLL files and communicate with them as per the defined protocol. The format of the JPG and PNG files are complex and out of the scope of this article. </p>
<p>Now the communication protocol has to be defined. We have only one type of plug-in: input plug-in, whenever we need to read data from the file we use these plug-in. We can define the functions to be exported as:</p>
<p><b>IInputPlugin* GetPluginInterface();</b></p>
<p>This is the only function that needs to be exported. This function returns a pointer to the IInputPlugin. The IInputPlugin is defined as </p>
<p><code><br />
class CInputPlugin<br />
{<br />
public:<br />
	virtual const char* GetFileName() = 0;<br />
	virtual const char* GetPluginName()	= 0;<br />
	virtual const char* GetDescription() = 0;<br />
	virtual unsigned long GetVersion() = 0;<br />
	virtual const char* GetFilterString() = 0;<br />
	virtual const char* GetExtension() = 0;<br />
	virtual DWORD GetWidth() = 0;<br />
	virtual DWORD GetHeight() = 0;<br />
	virtual BYTE GetBitCount() = 0;<br />
	virtual BYTE GetColorType() = 0;<br />
	virtual BYTE GetPlanes() = 0;<br />
	virtual BYTE GetCompression() = 0;<br />
	virtual DWORD GetScanLineWidth() = 0;<br />
	virtual DWORD GetPasses() = 0;<br />
	virtual int OpenImage(const char* lpszFile) = 0;<br />
	virtual int ReadScanLine(unsigned char* lpBuffer,unsigned long dwSize,unsigned int uLine) = 0;<br />
	virtual int CloseImage() = 0;<br />
	virtual void Release() = 0;<br />
};</p>
<p>typedef CInputPlugin IInputPlugin;<br />
</code></p>
<p>As you can see the IInputPlugin is an ABC(Abstract Base class). The input plug-in needs to implement this interface and return the pointer to the implementation class. The host application will use this pointer to manipulate he image</p>
<p>The host application is a normal MFC application. When the application loads it scans a folder for plug-in DLLs and loads all the Plug-ins found. The host application stores all the information about the plug-in for future use. When the user opens an image file, the host application searches this information to find which plug-in to use. The host then loads the corresponding plug-in and calls the OpenImage function. If the function returns 1, the get the information about the image and create a DIB Section based on the information. Then the host reads all the scan lines and displays the image.</p>
<p>There are many ways to extend this application. One way is to implement output Plug-ins. The output Plug-ins converts the given image data to some other formats and saves this to a file. We can define the protocol just like the input Plug-ins and create different output Plug-ins. This will give the user the ability to convert from one image format to another. One another way is to support image processing Plug-ins. The image processing Plug-ins accepts the image data and processes it and gives the processed image back to the host. There are many possibilities; I left this to your imagination.</p>
<p>Although I use C++/MFC to create this, we can extend this to any other languages or platforms. Plug-in Architecture is very powerful and using this we can create easily extensible application.</p>
<p>This is a very simple introduction, to better understand this architecture just search for Plug-in Architecture; you will find a lot of information.</p>
<p>The sample application is only tested in Windows 2003.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krvarma.com/2008/08/introduction-to-plug-in-architecture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to OpenMP using Visual C++ 2005</title>
		<link>http://www.krvarma.com/2008/06/introduction-to-openmp/</link>
		<comments>http://www.krvarma.com/2008/06/introduction-to-openmp/#comments</comments>
		<pubDate>Sun, 01 Jun 2008 14:02:15 +0000</pubDate>
		<dc:creator>Krishnaraj Varma</dc:creator>
				<category><![CDATA[C++/CLI]]></category>

		<guid isPermaLink="false">http://www.krvarma.com/?p=13</guid>
		<description><![CDATA[OpenMP is an application programming interface (API) for shared memory multiprocessing programming on multiprocessor/multicore systems. OpenMP consists of a set of compiler directives and a set of support functions. OpenMP works in conjunction with Fortran, C and C++. It is tailored for Shared Memory environments. The Shared Memory model is an abstraction of centralized multiprocessor [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>OpenMP is an application programming interface (API) for shared memory multiprocessing programming on multiprocessor/multicore systems. OpenMP consists of a set of compiler directives and a set of support functions. OpenMP works in conjunction with Fortran, C and C++. It is tailored for Shared Memory environments. The Shared Memory model is an abstraction of centralized multiprocessor system consisting of a collection of processors with access to the same shared memory. Processors can interact and synchronize with each other through shared variables. </p>
<p>In shared memory model, the standard model of parallelism is Fork/Join parallelism. In Fork/Join parallelism, there will be only one thread is active at the start and end and is which is called master thread. Master thread executes the sequential portion and at some points where the parallelism is needed, master thread forks (creates or awakens) additional threads. The master thread and child threads executes in parallel through the parallel sections. At the end the created threads die or are suspended. The flow of controls returns to the master threads.</p>
<p>In OpenMP, the directives are expressed as pragmas (pragmatic informations).  Following are the compiler directives &#038; functions:</p>
<ol><b>parallel</b> which preceeds a block to be executed in parallel</ol>
<ol><b>for</b> which preceeds a for block to be executed in parallel</ol>
<ol><b>sections</b> which preceeds section of blocks to be executed in parallel</ol>
<ol><b>critical</b> which preceeds critical section</ol>
<ol><b>single</b> which preceeds block of code that is to be executed by a single thread</ol>
<ol></ol>
<ol><b>omp_get_num_procs()</b> which returns the number of processors</ol>
<ol><b>omp_get_num_threads()</b> which returns the number of threads</ol>
<ol><b>omp_get_thread_num()</b> which returns the number of thread within group of threads</ol>
<ol><b>omp_set_num_threads()</b> which sets the number of threads for the parallel region</ol>
<p>
Visual Studio Professional and above supports OpenMP. The Following program demonstrates a very simple use of OpenMP in Visual C++ 2005.<br />
<code>#define _USE_MATH_DEFINES // for math defines in math.h</p>
<p>#include <stdio.h><br />
#include <omp.h><br />
#include<br />
<math.h>
<p>#define ANGLE_MAX		360</p>
<p>void main(int argc, char* argv[])<br />
{<br />
	int		nDegree				= 0;<br />
	double	dSins[ANGLE_MAX];</p>
<p>	#pragma omp parallel for<br />
	for(nDegree=0; nDegree<ANGLE_MAX; ++nDegree)<br />
	{<br />
		// Initialize the table with sin values<br />
		dSins[nDegree]	= sin((double)nDegree * (M_PI / 180.0));<br />
	}<br />
}</code><br />
In order to compile it properly you have the use the /openmp compiler switch. You can set it from  Project Properties->Configuration Properties->C/C++->Language:</p>
<p><a href="http://www.krvarma.com/wp-content/uploads/2008/08/ompoptions.jpg"><img src="http://www.krvarma.com/wp-content/uploads/2008/08/ompoptions-150x150.jpg" alt="Ompoptions 150x150" alt="" title="ompoptions" width="150" height="150" class="alignnone size-thumbnail wp-image-41" /></a></p>
<p>For a detailed list of OpenMP directives and support function please refer to this <a href="http://msdn.microsoft.com/en-us/library/tt15eb9t(VS.80).aspx">MSDN Documentation</a>. In next few days I will try to update this post with more examples.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krvarma.com/2008/06/introduction-to-openmp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

