Click here to Skip to main content
Click here to Skip to main content

A Library for Quick and Easy Development of Win32 Applications

By , 5 Nov 2004
 

Introduction

Windows programming can be a very tedious task even for experienced programmers. Numerous API functions and windows messages, Component Object Model and other things on one hand make applications very powerful, on the other hand it can be very discouraging since even for a simple application one has to invest a lot of time and effort to build it. The latter is especially true for people in computer related sciences like bioinformatics when the scientist is primary concentrated on its scientific stuff and doesn’t really want to understand all bells and whistles of win32 API. As the result, in the scientific world we have a lot of software with extremely poor and unobvious interface.

Very often what we need from the Windows application is a decent looking input and handy output. It is very annoying to convert data for example from Excel into a text file, process them through an application and then reformat back to Excel. Unfortunately this is the case for most of the programs in the scientific world.

Win32Easy is a static library that facilitates and speeds up the process of development of Win32 applications. Win32Easy provides several classes that handle windowing and interaction with other applications (like IE and Excel) via COM. These classes hide complexity from the programmer, but the code of the library is very much transparent and easy to understand unlike Microsoft Foundation Classes (MFC). In addition, the library does not overwhelm with the number of classes, instead it provides the most important ones. The library is not intended to avoid completely win32 native code; instead it helps to use it in a seamless manner.

Using Win32Easy

A typical application code looks like this:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
             LPSTR lpCmdLine, int nCmdShow)
{
    /*no panic, just initializing OLE - we are communicating with Office */
    HRESULT hr;
    if(FAILED(hr = OleInitialize(0)) )
       throw ole_exception(hr); 


    HtmlWin htmlWindow(hInstance);
    htmlWindow.OpenURL(L"<A href="http://www.google.com/" target=_blank>http://www.google.com/</A>");

    //do some work
   TextOutWindow tow(hInstance);
   tow.PrintText(TEXT("Some text"));


   Chart ch(hInstance);
   double dx[] = {10,20,30,40,50,60,70};
   double dy[] = {100,400,900,1600,2500,3600,4900};
   ch.Setx(0, dx, 7);
   ch.Sety(0, dy, 7);


   tow.WaitForEnd();
   return 0;
}

That is it. You will not find in the main routine anything like a message loop or a hassle with hundreds of parameters for window creation. This hassle indeed exists in the implementation of the HtmlWin or TextOutWindow classes, but this does not disturb the clarity of the code and its primary focus – the calculations that the program has to perform. It looks like an old good C++ program with a main routine. In fact, old command-line code with the main routine can be easily ported to a GUI application.

How does it work?

The mechanism how this works is straightforward: the objects representing windows create their own thread and do all GUI job there. The program flow is concentrated in the main thread in the WinMain procedure, while message handling and other windows related operations are hidden in the implementation of the objects. Naturally, win32easy library assists development of the master-like applications, which guide the user through the entire calculation process, rather than just waiting for the user actions. Such a behavior is often very welcome especially when the developer wants to save potential users of his application from reading voluminous manuals.

Example of Some Classes

The purpose of this section is to provide examples of some useful classes and show some of their member functions in order to demonstrate how it is easy to perform tasks with the library being described that are quite complicated to accomplish otherwise using just native win32 code.

MSExcel

Should you read or save results of your computation in an Excel file, this class is very convenient. It provides functionality for creating new, opening and saving existing files. Doing this through straightforward OLE one has to create numerous interfaces for many intermediate objects making code very difficult to manage. Just some member functions:

int OpenFile(LPOLESTR fname);
int SaveAs(LPOLESTR Fname);
int SetCellsArray(LPOLESTR lpSheet, LPRECT lpr, int *pdx);
int GetCellsArray(LPOLESTR lpSheet, LPRECT lpr, double *parr) const;
int CellValue(LPOLESTR lpSheet, int iRow, int iColumn, double *d);

TerminalWindow

Many times it is necessary to inform the user about the process that is going on at the moment or provide some textual information. Using just TextOut(HDC...) function is not a very convenient way since one has to take care of text lines on the screen, scrolling, possible conversion of numbers to text. It would be great to have something like old printf(...) function that takes care about all of this but in a window and it would be also great to be able to copy that text to clipboard. TerminalWindow does all of it:

bool PrintText(int iResID, va_list args) const;
bool PrintText(LPCTSTR format, va_list args) const;
bool PrintText(int iResID) const; 
bool PrintText(LPCTSTR lszText) const;

Library Homepage

Detailed information about the library and description of each object and its member functions is available here. Also, you'll find a molecular dynamic application which is powered by win32easy that uses all modern technologies like GUI, COM and DirectX.

Enjoy easy win32 development.

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

About the Author

Dr. APo
Other University of Washington
United States United States
Member
I was born in Moscow, USSR. In 1998, I earned Master’s degree in Chemistry from the Moscow State University. In 2000, I moved to Germany to pursue a PhD degree in Genetics. Currently I live with my family in the United States.
I have been always involved is a multidisciplinary research side-by-side with biologists. The research involves physical chemistry, bioinformatics and electrical engineering.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralCatching results of operationsmemberKarl N M6 May '05 - 15:38 
It is not obvious to the novice how one might implement callbacks for events, for example OnDocumentComplete() for the HtmlWin?
 
Any takers?
 
Thanks
Karl
GeneralRe: Catching results of operationsmemberpozhit7 May '05 - 5:19 
Well, it is possible through implementing IConnectionPoint and IConnectionPointContainer. In case of win32easy, methods of those interfaces merely return S_OK. For simplicity I did not implement them, but this is possible.
Alex.
GeneralHire me!memberJohn R. Shaw14 Nov '04 - 11:23 
Just joking (I think)!
 
This reminds me of all the small programs I used to write (one man show). Before the company put a stop to it, I used to get this sales guy or that tech walking into my office and say we need a simple program to do this or that. If it was simple enough, I would nock it out in short order (no problem - it is what I do (or did)!). Well there was a problem, I had to stop working on the officaly assigned project (temporaraly)).
 
P.S. Ignore the spelling, I offten have trouble getting the characters in the correct order, let alone the correct spelling.
 
P.P.S. My realm is concept and ideas. The details tend to get lost (the concepts are forerever)!
 
INTP
GeneralRe: Hire me!memberpozhit14 Nov '04 - 14:42 
Dear John,
I'm convinced that a professional programming product can only come from a company or at least an organized group of individuals due to apparent reasons. However, especially in science, these "one-man-shows" are very often. Most of the times these products are very unprofessionally looking. I tried to bring some beauty in this chaotic world. Well, I hope it is helpful to some extent.
Alex.
GeneralNeeds a tad bit of work...sussAnonymous13 Oct '04 - 10:19 
Its not a bad idea, however this is only good for very small apps. I remember seeing something similar to this a while back...
GeneralRe: Needs a tad bit of work...memberpozhit13 Oct '04 - 10:37 
Anonymous wrote:
Its not a bad idea, however this is only good for very small apps. I remember seeing something similar to this a while back...
 
Well, you are right. It will be hard to program something like a full scale word or image processor, but it is all what we (I mean mostly scientists) often need. Some neat app with a good GUI which does some clever calculations and at the same time it is very undesirable to clog the code with all these win32 function calls...
Alex.
QuestionWhy not just make a console app?memberDon Clugston11 Oct '04 - 18:02 
>As the result, in the scientific world we have a lot of software with extremely poor and unobvious interface.
 
This is certainly true!
But I think you need more explanation of why anyone would use your code instead of just creating a standard Win32 console project.
 
And why the HRESULT and the call to OleInitialize() ? You can't understand what that's doing without knowing a lot about the Windows API! Surely that belongs in your framework. Likewise, why does the Chart object need the hInstance?
Sorry, but the design looks it still needs a lot of a work.
AnswerRe: Why not just make a console app?memberpozhit12 Oct '04 - 4:12 
Dear Don,
Thank you for the comment, in fact people make this console applications, especially in science. But the application then looks ugly - a black "ms-dos" window doesn't look very professional Wink | ;) . It will be even more funny to have an "ms-dos" window and other windows created on top of it. It reminds me many Java applications that look like this.
OleInitialize() was necessary since we were going to use a Microsoft Office Chart object (OLE object) in the demo project. The demo project can be used as a template for other applications, people can leave this API call there, It won't harm even if they don't use OLE.
Concerning Chart object, please take a look at the demo code, it is easy to follow. Chart is public from Window, whose constructor requires HINSTANCE.

 
Alex.
GeneralRe: Why not just make a console app?memberJim Crafton12 Oct '04 - 4:41 
If you want to make a "console" app (i.e. using the traditional "int main( int argc, char** argv)" entry point there's a (relatively) easy way to do this in VC and NOT have the console box show up.
 
(note: this is for VC6 - VC7 is similar)
 
Add a "int main( int argc, char** argv)" function as normal.
 
Go to you project's Linker settings, and go to the "Output" section. In the "Entry-point symbol" field add "mainCRTStartup".
 
Go to the edit box at the bottom where all the project's options are editable. Find the text "/subsystem:console" - change this so that the app runs in windows subsystem like so:
"/subsystem:windows"
 
Rebuild and run - you shouldn't see any dos box if you create windows, etc.
 
You can get the HINSTANCE you need by calling GetModuleHandle(NULL) - this will return the HINSTANCE associated with the exe.
 
This is exactly what I use in the VCF[^] to allow for using main() in both "console" and "window" programs.
 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
 
SELECT * FROM User WHERE Clue > 0
0 rows returned

GeneralRe: Why not just make a console app?memberJim Crafton12 Oct '04 - 4:52 
This is also possible using the Borland compilers as well. However the process is a bit different. There some discussion[^] of this on the VCF forums.
 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
 
SELECT * FROM User WHERE Clue > 0
0 rows returned

GeneralRe: Why not just make a console app?memberpozhit12 Oct '04 - 7:41 
Jim Crafton wrote:
If you want to make a "console" app (i.e. using the traditional "int main( int argc, char** argv)" entry point there's a (relatively) easy way to do this in VC and NOT have the console box show up.
 
Hi Jim,
Win32easy is not intended to emulate console apps. It brings just a little taste of them for better code clarity and promotes the app to be more active. Implementation of the Window object has everything that you'll find in a standard win32 app (message loop, CreateWindow, etc). Of course, one can create windows from a console application, hide an "ms-dos" window and make OLE interaction with other applications but it looks a bit clunky, sort of scratching your right ear with the left hand Big Grin | :-D
Alex.
GeneralRe: Why not just make a console app?memberJim Crafton12 Oct '04 - 11:05 
Yes I understand all that. The point of my post was to explain how to create a regular windows app without using WinMain as your entry point, and instead using the more traditional main().
 
The presence (or absence) of the DOS box is due to what subsystem you application runs in. Anything that run in the "console" subsystem gets the dos box. An application that runs in the "windows" subsystem does not get a DOS box launched. GUI apps always run in the "windows" susbsystem.

 
¡El diablo está en mis pantalones! ¡Mire, mire!
 
Real Mentats use only 100% pure, unfooled around with Sapho Juice(tm)!
 
SELECT * FROM User WHERE Clue > 0
0 rows returned

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 5 Nov 2004
Article Copyright 2004 by Dr. APo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid