Anonymous Pipes in Windows!

Recently I had been working on a program to get a set of HTML files rendered in IE and then copy the contents from the IE window to a word document. I wanted to do this in VC++ since it provides very easy access to COM objects like IE, Word, etc. and I have also used it in the past to build BHOs (Browser Helper Objects) and like. VB also supports COM but usually has pretty complex procedures to do minor things. And I forgot about .NET and the wrapper libraries around COM objects which provides very easy access. If I want to do something like that I would better use C# instead. But as it goes, I started coding in VC++. The basic logic of the code is as follows:
– Ask the user the select a directory with all the HTML files that you need to put in word
– Ask the user for the word document to which the content will be put to
– Open the specified Word document and keep a handle to the application or document object
– Open an IE Window (Invisible)
– Render the HTML file in the invisible IE Window
– Select all the contents from the IE Window and Copy it to the Windows Clipboard
– and paste the clipboard contents and save the file
– Repeat the step from rendering the HTML file onwards until all the files content are in the word doc
– Save and close the word file and the application object and release all the COM instances
Sounds a bit complicated and it is since I later realized that you can use the Word Objects InsertHTMLFile() directly instead of using Windows Clipboard which is unreliable as well as open to other rogue applications which can hang this particular application. But since I want to use it for my own purposes and not productize it, I am okay with the said approach. Again, I had almost finished my whole application until I found out that information. Wish Microsoft/Bing/Google could relate such information without me explicitly asking for it.
Anyways, so after almost writing 60% of the application, I realized that my Office 2003 installation is fucked up and that the COM Wrapper classes generated in VC++ after adding an ActiveX component for Word gives a lot of compilation errors. I tried a lot of different things including using #imports of the GUID for VBE and Word with the no_implementation keyword. None worked since removal of compilation errors would turn into runtime errors when I tried to call them in the code.
Anyways, then I decided to use a C# helper program to achieve the feat. And I decided to use Anonymous Pipes to communicate between my main VC++ app and the client C# app. I didn’t want my pipes to be used by any other program as well as wanted to limit the lifetime of the handles in case any of the apps crash. Hence I used anonymous pipes instead of named pipes. And since anonymous pipe is a half-duplex/suplex pipe, I have created easy classes to both implement a Full Duplex or half-duplex anonymous pipes depending on the needs. In most cases, one would require Full Duplex pipes to ideally do a proper communication.
Download the code from http://www.naresh.se/phpBB/viewtopic.php?f=17&t=10. Also post your follow up discussions and comments that same forum thread.

Recently I had been working on a program to get a set of HTML files rendered in IE and then copy the contents from the IE window to a word document. I wanted to do this in VC++ since it provides very easy access to COM objects like IE, Word, etc. and I have also used it in the past to build BHOs (Browser Helper Objects) and like. VB also supports COM but usually has pretty complex procedures to do minor things. And I forgot about .NET and the wrapper libraries around COM objects which provides very easy access. If I want to do something like that I would better use C# instead. But as it goes, I started coding in VC++. The basic logic of the code is as follows:

– Ask the user the select a directory with all the HTML files that you need to put in word
– Ask the user for the word document to which the content will be put to
– Open the specified Word document and keep a handle to the application or document object
– Open an IE Window (Invisible)
– Render the HTML file in the invisible IE Window
– Select all the contents from the IE Window and Copy it to the Windows Clipboard
– and paste the clipboard contents and save the file
– Repeat the step from rendering the HTML file onwards until all the files content are in the word doc
– Save and close the word file and the application object and release all the COM instances

Sounds a bit complicated and it is since I later realized that you can use the Word Objects InsertHTMLFile() directly instead of using Windows Clipboard which is unreliable as well as open to other rogue applications which can hang this particular application. But since I want to use it for my own purposes and not productize it, I am okay with the said approach. Again, I had almost finished my whole application until I found out that information. Wish Microsoft/Bing/Google could relate such information without me explicitly asking for it.

Anyways, so after almost writing 60% of the application, I realized that my Office 2003 installation is fucked up and that the COM Wrapper classes generated in VC++ after adding an ActiveX component for Word gives a lot of compilation errors. I tried a lot of different things including using #imports of the GUID for VBE and Word with the no_implementation keyword. None worked since removal of compilation errors would turn into runtime errors when I tried to call them in the code.

Then I decided to use a C# helper program to achieve the feat. And I decided to use Anonymous Pipes to communicate between my main VC++ app and the client C# app. I didn’t want my pipes to be used by any other program as well as wanted to limit the lifetime of the handles in case any of the apps crash. Hence I used anonymous pipes instead of named pipes. And since anonymous pipe is a half-duplex/suplex pipe, I have created easy classes to both implement a Full Duplex or half-duplex anonymous pipes depending on the needs. In most cases, one would require Full Duplex pipes to ideally do a proper communication.

Download the code from http://www.naresh.se/phpBB/viewtopic.php?f=17&t=10. Also post your follow up discussions and comments that same forum thread.