The Vagaries of The Digital Black-OLE
THE ACTIVEX OBJECTS TUTORIAL

NOTE:THIS DOCUMENT IS AN EARLY RELEASE OF THE FINAL SPECIFICATION. IT IS MEANT TO SPECIFY AND ACCOMPANY THE TUTORIAL THAT IS STILL IN DEVELOPMENT.SOME OF THE INFORMATION IN THE TUTORIAL MAY BE IMPRECISE OR MAY NOT BE AN ACCURATE REPRESENTATION OF THE FUNCTIONALITY OF THE FINAL DOCUMENT OR TUTORIAL.

WE ASSUME NO RESPONSIBILITY WHATSOEVER FOR ANY DAMAGES THAT MIGHT OCCUR EITHER DIRECTLY OR INDIRECTLY FROM THESE INACCURACIES. ALL THE FAMILY TROUBLES ACCRUING AS A RESULT OF EXCESSIVE PERUSAL OF THIS TUTORIAL AND LESS OF TIME BEING SPENT IN THE HOUSE THEREOF; OR BY WAY OF SCUFFLES RELATING TO TIME-SLOTS ON THE COMPUTER; OR ANY OTHER FORM OF LEGAL/ILLEGAL HASSLES ARISING AS A DIRECT OR INDIRECT CONSEQUENCE OF THE SUBJECT MATTER OF THESE FILES, INCLUDING INTELLECTUAL DEBATES, BUT EXCLUDING PHILOSOPHICAL TYPOS; OR THE DILAPIDATION OF THE COMPUTER WHILE EXECUTING ANY OF THE CODE BUNDLED HEREWITH; OR ANY UPSURGE IN THE REGISTRATIONS AT THE LOCAL ASYLUM OR MONASTERY ACCOUNTABLE TO NAUSEATING PROGRAMS ETC. -- SHALL NOT BE THE LIABILITY OF THE AUTHORS.


THE FIRST IMPRESSION

Before we wet our beak in OLE/AXO, guess what this simple Visual C++ program will do for you :

#include <Windows.h>
#include <OleCtl.h>
#include "hlinkez.h"

int WINAPI WinMain (HINSTANCE i, HINSTANCE j, LPSTR k, int l)
{
	CoInitialize(NULL);
	HlinkSimpleNavigateToString(L"http://www.microsoft.com", 0, 0, 0, 0, 0, 0, 0);
}

All you need to do is copy the files hlinkez.lib , hlinkez.dll , hlinkez.h from the path

 d:\inetsdk\samples\HlinkeZ
to your current project directory. The project files will be z.cpp and hlinkez.lib . In the project settings , the Category of Precompiled Headers in C/C++ option will be Automatic use of Precompiled Headers. Lastly, you need to download the Microsoft Internet Explorer . Well? It gets you Microsoft's home page right on to your machine, by automatically loading Internet Explorer! Remember the large code we had to write in Java to get similar results? But here, it is merely the question of one function -- HlinkSimpleNavigatorToString ; the name might remind you of an Italian opera, but it is extremely useful! Besides it can be used not only through Visual C++, but through all the Microsoft products like Visual Basic, Visual Foxpro etc!

Let us put it this way. Remember the good ol' MessageBox function? It is present in the DLL called user32.dll . Since all the products that run under Windows have access to the DLLs, any one of them can use the function MessageBox . Similarly, HlinkSimpleNavigatorToString resides in a DLL, and can be used from any of the Microsoft product.

Do you realize what this means? If you are developing any application, or want to revamp your current applications, you can smoosh in this feature. Lets assume you are writing a 'Financial Analysis' package. Now, nothing stops you from creating, say, a button which on being clicked fetches a document from anywhere on the web! All it takes is a teeny li'l program as illustrated above. Your software can now be Internet-ready, with hardly any herculean effort.

That is why we are saying that Microsoft is the vintage car, while Java is still little more than a spoke in its wheel. That is why we have defected from Java. For Microsoft is no more the babe-in-the-woods.

In fact, it is rather unbecoming to collate Java with ActiveX Objects. For there is simply no comparison. The bar-stool Java evangelists take pleasure in prattling about Java-toasters (which at least today rates just slightly better as a pigment of imagination than your Income Tax returns!) and the world being squeezed in to one huge hard disk. Sounds great, definitely. But who makes that possible ? Microsoft's ActiveX Objects. Java today, let us face it, is little more a narrow keyhole. You cannot expect to see the world through it. Microsoft, on the other hand, is omni-present on the hard disks around the globe (in form of Windows), so their perspective is certainly much more plausible.

Of course, our amigos at MS are too self-respecting to provide you code like the 5-line program above, but may be that's their way of keeping people like us on our toes. Trust us to bring you code like that -- and that's not a tall promise.

The last thing to bear in mind before we rub shoulders with ActiveX Objects is that Internet Browser is not a browser as it is mythically imagined to be. It is an ActiveX Object . Naturally, everything that Microsoft will now do will be in the form of AXOs. Who knows, may be some day even Mr. Bill Gates shall be an ActiveX Object. :D

The only mistake, and a pretty big one, that Microsoft is commiting that They are not shouting from roof tops this time that, "Try this. This is the best." which in our opinion they must be doing. May be we should all write a mail a day to MS waking them up.


THE QUINTESSENCE OF OLE

"If you understand Zen Buddhism, you understand OLE 2.0."

Assume there was a prescribed pattern in which a woman was to be dated. Then so many self-respecting men would not have to spend the saturday nights in the company of the cobwebs in their room. But unfortunately, there is no such predetermined kit of rules on how to call upon the opposite gender. There is no standardization. Apparently, the software market seemed to have a similar problem. With hordes of packages pouring in, there was more and more confusion. That is when Microsoft (and they're known for their knack of visualizing unprecedented concepts) began to think about it seriously.

Since Microsoft wanted programmers to author great component code, it came out with the standard of OLE. In simple terms, OLE means -- if you can talk to one object, you can talk to all of them in the same way. The way they might respond will ofcourse be different depending on whom you are talking to. Undubitably, OLE is a rather abstract concept and people find it difficult to explain it in plain English. So let us elucidate it with the help of several similes...

  • When you wish to open a file, all you have to do is click on the menu bar option FILE and then on OPEN (or in any other set pattern). It does not matter whether you are in Microsoft Word, in Visual C++ environment or in Power Builder. You have to follow the same procedure. Of course, the way the file will be opened will be different conditioned by which software you are working with. A similar principle of correlation, or standardization applies to OLE -- if you can talk to one object, you can talk to 'em all in the same manner, for they all follow the same rules.
  • When you use a browser, you pick up an HTML file either from your own hard disk or from the net. It does not matter to you whether the file was written in Word, or 'Write', or 'Edit'. It does not even matter where exactly the file is coming in from. For lugging the file on from the local hard disk, the protocol is "FILE" and in the case of Internet, it is "HTTP" or "FTP" etc. That is all you have to worry about. OLE complies to the same philosophy. There is a rigid set of rules, and every party follows it.
  • Consider a pair of legs in Levis jeans. If you look at them from behind, it becomes an identity crisis at times -- you just cannot figure out whether the pair-of-legs is a man or a woman. On the contrary, if we had some typicality about all the jeans that could facilitate gender recognition, there would be lesser confusion. That's what OLE is for -- to curtail the confusion.
  • Most of us are acqainted with the conundrums of opening a car door. Different cars have arcane, new-fangled ways of letting you out. Would it not be so much more comfortable if we had the same method of opening the door in all the cars, whether they were the brain-children of the Japanese, the German or the Americans? Well, that is the kind of convenience that OLE epitomizes.
  • Another readily comprehensible case in point could be the Internet chats. You never know exactly who you are yapping with (and may be you don't care any more), but the manner you talk to them is enduringly the same -- you punch away your thoughts through the keyboard. OLE is like that. The manner remains the same.
Therefore, OLE is an unalterable outfit of rules that Microsoft has introduced for the purposes of putting all its software on the same plane. But that is what we wanted and that is why things will change now.

Like most other technologies in the not-everyone's-cup-of-eggnog category, OLE 2.0 too suffers from a dismal case of mistaken identity. People love to believe that it is a programming language in itself (and as a misconception, it is only next to thinking that Shakespeare was a break-dancer). So the first thing that we have to bear in mind is that OLE is not a programming language, it is a concept, a set of rules.

Let us put it this way. When you talk to a person, all your comments are not precisely pre-determined. That is, whatever you contribute to the conversation is conditioned by what the other person is saying -- the conversation is dynamic. Further, unless you are a finger-popping McDaddy from the bylanes of LA, you know you have to be mannerful, you have to keep up to the decorum of a civilized society and its unwritten laws of what is right / what is wrong, of what is acceptable / what is not. These are called protocols. For instance, if the President of United States, Mr. Bill Clinton (as of now), tours India it is a question of protocol that President of India ought to greet him. But if he visits UK or Saudi Arabia, it could be Her Highness, the Queen or His Highness, the Sheikh respectively.

In the same vein, when the programs, or a bunch of computers, communicate among themselves, there has to be a certain modus operandi , certain proprieties, certain fundamental standards. To facilitate a peaceful co-existence among various programs, Microsoft formulated a pack of rules so that everyone could talk to everyone else. And it helps. Because now programming doesn't have to be like an Indian visiting Germany, where, let alone the local speak, even the signs are in German.

Therefore, OLE is more like the lingua-franca of the programming landscape than the landscape itself. It is like the warden's whistle that every running program has to listen.

OUR VENTURE ITINERARY--
How we shall go about teaching OLE

Most of the people who have wrestled with OLE will subscribe to our point of view that there has been just one book on OLE 2. 0 as yet -- by Kraig Brockschmidt. Admittedly, it makes a wonderful read. But unfortunately, the technology is such that you have to read the book thrice if you want to get its complete flavor.

Lets look at it this way. A dose of half a quintal of aspirins and a tankful of ginger-ale is fine if you are learning and understanding a technology. But it is imparting the enlightenment that has had even the gold medalists staggering back to the garrets. For you can mull over a 5000 line program yourself, even if you have to snore on it for a couple of weeks; but when it comes to teaching it to other innocent people, who have in no way wronged against you, it is difficult to do so without being gunned down by yawns.

The problem with such mammoth programs is that their marrow is camouflaged by the various other frills thrown in. When we ran the first sample on our path towards OLEism, we were overwhelmed -- the results were simply fantastic! But the main essence of the code -- what its several segments were gabbing about, which function was doing what etc. -- was fogged out by the sheer length and the needless drivel.

It is like cooking. Roasting a gob of chicken might be easy, but what ultimately matters is the serving. The meat might be the same but there is plenty of contrast between scraping one's food out of a billycan in a cold dugout in Bosnia accompanied by the sound of bullets, and dining off china plates in a warm restaurant, with a string quartet playing Schubert in the background.

Well, culinary skills apart, we are not saying that the folks at Microsoft are post-graduates from Kissinger's Cookery Classes, but they do add lot of ostentation when it comes to their sample programs to make them look good. While learning, all that is certainly not required.

Therefore, our way of serving the programs will be slightly different, and affordably simple (at least as much as it can be). All we shall expect you to do is to type out the program a line at a time and run it. Of course, even a miracle cannot make such code work, but you will be sure what each function is for, and quite how the output is being arrived at.

Apparently, our samples do not look good in the first go, but they give you a strong upperhand over the concepts so you can ultimately hatch smart software yourself. The goal here is to inculcate initiative, not to curb it.

LEARN-AS-YOU-BURN

In addition, we will follow a 'learn-as-you-burn' policy. We could have set the tutorial in the conventional pattern -- starting with an elaborate tome on the various theoretical concepts and then following it up with the programs. But we thought we will be slightly different.

We will start right offhand, grappling with OLE from the very first program. Subsequently, the theory will take care of itself in due course.

FOR WHOM IS THE TUTORIAL

Unquestionably, this tutorial has been written with Windows programming in mind. On that account, you must be comfortable with C++; in fact, for a non-C++ programmer, this tutorial is nothing more than a few quotes. As the technology of AXO is a stretched sibling of OLE, we expect you to have heard of OLE. Nonetheless, you can always refer to DOCOBJECTS .

Here, allow us also to snuff out the myth that MFC and OLE are analogous. OLE 2.0 is OLE 2.0. The Microsoft Foundation Classes (MFC) went a long way in taking the sweat out of Windows SDK programming, its bare-bones concepts and all. With MFC, a programmer could be more productive in lesser time. When OLE 2.0 was causing an upsurge in the registrations at the asylums round the globe, MS came out with MFC for OLE as well. This made OLE programming a heap sight better. However, these MFC classes for OLE 2.0 bear no semblance to OLE 2.0, just as the MFC classes for the Windows SDK cannot be likened to Windows SDK itself.

You might be an international expert on MFC, but you might not know a tad about 'QueryInterface' (which in turn means that you are an OLE ignoramus !)

SOME DOs and DOUGHNUTS

The basic premise of this tutorial boils down to this : some people want to be Internet wizards. They love being up to the Adam's Apple in HTML, in sockets, in protocols. In their free moments, they randomly tide through the zillions of cranky sites, hoping to stumble upon some hidden, undocumented feature of the Internet. While they're organizing their socks-and-underwear drawer, they memorize long http addresses.

Well, you might not be that nerdy, you might not be the one to croon about SLIP and packet switches during coffee-hours, but when it comes to programming for the Internet, the fascination might just not be there. You just want to get the work done, go home, fill the cat's waterdish, and relax for a while. You have no intention to change and there's nothing wrong with that.

That's where this tutorial comes in handy. It might not turn you into an ActiveX Wizard right offhand, but you'll pick up a few clods of useful computing information. You'll know enough to get by quickly, clearly and with a minimum of pain so you can move on to pleasant things in life. Here are some forewarnings. . .

  • We will bypass all the technical hoopla we can, and you'll still get your work done, and while some concepts might leave you scratching your head or heading for the poker joint, they will soon be covered in detail when the concept is being elaborately explained.
  • Rest assured, for we WON'T wimp out by saying, "For further information, consult `this' site". So you needn't pull on your wading boots. All that you need to know about the AXO will be smooshed into these files somewhere or the other.
  • AXO thrives on OLE technology chances are that you come across a dab of OLE wisdom that you already possess. Feel free to skip any section that comes in smaller size.
  • This tutorial is of course designed to substitute for your computer guru, in dire straits; Nonetheless, we suggest you keep a couple of Budweisers in the nearest drawer, just in case you need a quick bribe.
  • AXO is the exciting, new way to use the computer. That mean its just about as esoteric as a car's dashboard. If you've had a brush with OLE technologies, it can be a little intuitive but that doesn't mean it is as easy as bowling the nine pins down. In fact sooner or later, every smart software-puppy will have to learn it except for Mr. Wimble, who plays squash with boss. But be prepared: There is no immediate panacea for it, no matter how you want to pronounce that word. As usual, Microsoft's software is tough as hickory nuts, but like IRS forms, they have a clump of cryptic words too.
Just as international signs over the rest rooms help you when you drink too much espresso in Paris, this tutorial will make it easier for you to know where to look in case of emergency.

TOM, DICK AND QUERY --
Our Naming Conventions

Microsoft makes a really ugly beau to names and words. Half a glance at AXO can reinforce the notion that MS and programming can bring out the worst in each other when they first meet. Their technology can be like Jalapeno in French sauce. Their naming conventions can cause cardiac arrest in a yak. What's worse, they seem to be sticking to the conundrums of what we call the Hungarian Notation.

Nevertheless, this time we will only aggravating their abominable ritual. In other words, while dealing with AXOs, we will turn over a new leaf. If you have read our Java tutorial, you must know that we are over-simplistic when it comes to naming variables, functions etc. For instance, our variables are single-letter ones, say, a, i, c etc. instead of anything like applet, instanceHandle or counter.

That is a good principle to adhere to if the programs are not too hefty. But with endless code that is so characteristic of any MS product, AXO inclusive, we'd rather use names that we can remember for at least three days at a stretch (for it might take that long to type out one given program :). Accordingly, our naming conventions in this case will be :

  • When deriving a class from an Interface, we just prefix the Interface name with a 'c'. For instance, say we are deriving a class from ITypeInfo. Our class will be called 'CITypeInfo'.
  • The variables to our classes will be prefixed with a 'm_p'. As in the above example,
    	ITypeInfo   CITypeInfo;
    	CITypeInfo   *m_pITypeInfo;
    	

THROUGH THE BACK DOOR
Before the Rubber hits the road

Let us make it adequately clear that as we go through the tutorial, our approach might get unduly simplified and our pace on level with your average banana slug. But the ultimate end here is to enable you to design programs that are far more complex than what MS will provide. Hence the plainness will only serve us well.

We will begin with the smallest possible ActiveX Object that you can write. Towards that end, we will require the following 4 files :

. cpp file ( no header file ),
. odl file,
. def file,
. rc file.

The other requirements are :

  • Visual C++ : We are using Version 4. 0 of Visual C++.
  • Internet Explorer SDK : In case you don't have this it can be downloaded from Microsoft's home page. It is a 12 MB zip file.

All you need to do next is follow these simple steps :

Step 1. Once you have downloaded it and installed it on your hard disk (we have it installed on d:\), you will find a subdirectory called inetsdk. At the command prompt give the following set commands:

path=d:\inetsdk\bin;d:\msdev\bin;%PATH%
set lib=d:\inetsdk\lib;d:\inetsdk\samples\basectl\lib;d:\msdev\lib;d:\msdev \mfc\lib
set include=d:\inetsdk\include;d:\inetsdk\samples\basectl\include;d:\msdev\
include;d:\msdev\mfc\include
Step 2. Being in the DOS mode from Windows'95, change the directory to inetsdk. Within the samples directory there is a directory called Basectl. Execute the command nmake. i. e.

d:\inetsdk\Samples\BaseCtl\>nmake
This command will compile the cpp files in the FrameWrk directory giving obj files. A new lib file is created called CtlFwd32. lib which will contain all the obj files. This file will be in the lib directory of BaseCtl.

Step 3. After having got the files required for creating the ActiveX Objects, create a new project in Visual C++ 4. 0. Let the location of the project be d:\ (root) and name the project zzz. The project type will be OLE Control Wizard which is the third choice on the list. Click on Create and then on Finish. The perspective behind using a Wizard is that you will not have to rack your brains over a trillion and one settings that have to be done. Once you have the project ready, remove all the files within this project as we will be creating our own files and inserting them into the project.

THE GEARS AND COGS

"In computer programming, the rules when you are learning are a shade different from when you are actually writing code, because the code is written for others."

People who have just heard about OLE much often look at it like its some form of ancient Black magic that they can never learn. Even if you are a C++ ace, you might feel more than a bit of trepidation when you actually see the programs. But here we go, explaining all the sprawling code in excruciating detail...

DOCOBJECTS
ACTIVE X OBJECTS
SPROIDS
SOCKETS

THE LAST WORD

"If we learn by our mistakes, then we are getting a fantastic education!"

If you have been patient enough to have read The Goodies inside an OCX Container, you might say that all this while we have been chiding what we call "The Hungarian Notation", singing paeans to Java and putting it high on a pedestal, while writing ecstatic obituaries about Microsoft. And now all of a sudden, we have been smitten by the MS bug.

Well, we have absolutely no qualms in admitting that we honestly liked Java then and we honestly like AXO now. Every one has the fundamental right to be wrong, and lets just say, we were exercising our rights to the fullest. We do not believe in taking corporate sides -- we are on the side of the best. And there seems to be no doubt about the fact that, as of today, Microsoft's technology rules the roost.

All we contemplate is splitting the latest technology apart, getting the marrow out of the can and serving it up as an unadulterated tutorial. Believe us, we will do it differently, and we guarantee that at the end of the day, you will be dish out better-looking code than the guys at Microsoft themselves.

Bon Voyage!


WITH A LITTLE HELP FROM MY FRIENDS

There are so many of us out there goofing up our programs trying hard to figure out the myriad concepts of OLE/AXO and so few who actually seem to be getting anywhere. The least we can do is be together in cyberspace and share our experiences and knowledge.

If you wish to share any comments, queries, headaches, suggestions and experiences, RUSH 'EM to us. We just might be the right ones to complicate things. :)

We would like to thank Ms. Meeta and Mr. Prashant Ajjampur for sparing us some space on their server.

If you are feeling rather techno-headed, sneak back to Vijay Mukhi's Technology Cornucopia
Yahoo was good to us. So we thought we will be good to them too. :)


THIS TUTORIAL IS A JOINT EFFORT OF

Vijay Mukhi
Sonal Kotecha
Shashank Tripathi


Vijay Mukhi's Computer Institute
B-13, Everest Building, Tardeo, Mumbai 400 034, India.
http://www.vijaymukhi.com
e-mail :vmukhi@giasbm01.vsnl.net.in
Tel : 91-22-496 4335 /6/7/9
Fax : 91-22-307 28 59