Saturday, April 29, 2006

Bug in Microsoft CDO For Windows 2000 Library?

I came across this while writing some code using the Outlook Object Model and CDO For Windows 2000. My requirement is simple. Convert each Outlook MailItem in the Inbox folder to CDO Message object which is processed further. The algorithm that i use is :

do
msgs(i) = new cdo message
msgs(i).from = mailitem.from
msgs(i).subject = mailitem.subject
...............
copy all the corresponding fields form outlook mailitem to cdo message object
................

for each attachment in mailitem
tmpname = tmp + "\" + attachment.name
mailitem.saveAttachment in tmpname
msgs(i).addAttachment tmpname
delete the file tmpname
loop
loop
delete the tmp folder

What i am doing is very simple, copy all Outlook MailItem fields to corresponding CDO Message object fields. Then for each attachment in the MailItem, save the attachment in some temporary directory (tmp) with the name same as the attachment name. Then add that file as an attachment to the CDO Message object. Delete the temporaryt file. Once all the messages are converted, delete the temporary folder.

Problem occurs when an attempt is made to delete the temporary file, i.e when i am deleteing the temporary file in the loop. The error was straight forward, file access error, cannot delete the file. Which meant that there is some reference, even after the file is added as attachment to the Message object. One thought which occured to my mind was that may be the CDO library implements a lazy algorithm which builds the actual object only if it is sent or a stream is obtained from it etc. So i modified the code to add a Message.GetStream call to make sure that the attachment is actually loaded in to the Message object and the mime structure is built. Everything was fine. It buiilt the actual mime structure embedding the file. But still the reference was not released. It was very much evident because i was not even able to delete the file from the filesystem.

Finally i could conclude that the reference is still not released though the mime structure is built. Workaround was to get the stream from the object, create a temporary file, write the stream in it. Set the object to 'nothing', now the references are released here and the attached files are deleted . Create a new CDO Message object, load the stream from the saved file.

Well i dunno how far my approach of getting the job done was right. May be i am using the wrong approach, or it is indeed a bug in the CDO library which does not close the stream even after the mime structure is built.

Comments are welcome :)

Saturday, April 22, 2006

OMG!!

OMG!! Looks nice.. I am thinking of making a sticker of it and paste it somewhere i can see daily..Ok...My temporary memory loss created a big havoc!! The girl thought that i was "using" her :( I only talk to her when i want and not otherwise. If i have no one to talk then i talk to her, spend time with her etc. It was all due to the lack of good written communication in me. I am not good at communicating my thoughts in writing. Well, it was a quite long chat for around 6 hrs!! Thats my longest chat ever. She stressed more on why i gave the gaps. Um....to tell you the truth, the gaps were to divert my mind from her thought. Coz i am still in the struggling phase of my career. So....Anyway...henceforth i am not gonna bother about it...if her thought comes in my mind, let it come. Who knows, how long we are gonna live!! I will just live for the moment and be happy and make others happy. Keep smiling.... :)

Monday, April 17, 2006

How to create your own widgets

Well, there might be many tutorials available on "how to write your own widgets", but i could not find one. Yes, may be i am not that smart in googling :( From nowhere i got this idea of writing a widget. Its very simple than you might think. Atleast some simple widgets are damn easy to write. All you need is a proper connection of available tools :) Before starting i would like to say that i am not any expert in these things, but just another programmer. Anyone reading this is free to comment and suggest some nice ideas.

Setup :
  • Firefox
  • Enough(mail me to know how much is "enough") knowledge of HTML, javascript.
  • Behaviour, Prototype
  • Simple ASCII editor, Notepad or Notepad++(i use it).
  • 2 minutes of your valuable time to get the widget ready and 1 minute to read this scrap.
Ok..i am going to show you how to write a very basic, "hello world" kind of widget. We will call this widget as "SayMyName" widget which takes your name as parameter and echoes it. You will place this widget in your HTML with a single line like this :

<span id="SomeId" class="SayMyName" name="Tutankhamun" />

When you place this line in your HTML, all occurences of this line will be replaced with something like :
Hello Tutankhamun!!

Ofcourse, not Tutankhamun all the time but whatever is the value for the name attribute. I will first show you the complete widget and then go into the details.

Step 1 :

Create a directory and put prototype.js, behaviour.js in it. Later save your html file and myWidget.js in it.

Step 2 :

Copy this HTML and save it as a HTML file :

<html>
<head>
<script type="text/javascript" language="javascript" src="prototype.js"></script>
<script type="text/javascript" language="javascript" src="behaviour.js"></script>
<script type="text/javascript" language="javascript" src="myWidget.js"></script>
</head>
<body>
<span id="someId" class="SayMyName" name="Tutankhamun" />
</body>
</html>

Step 3 :

Copy this javascript and save it as myWidget.js.

var myrule = {
'.SayMyName' : function(element) {
element.onclick = function() {
var helloName = element.getAttribute("name");
new Insertion.Before(element, ' Hello ' + helloName + '!!');
Element.remove(element.firstChild);
}
}
}
Behaviour.register(myrule);

Step 4 :

You are done with the widget. Open the html file in a browser and you should see :

Hello
Tutankhamun!!

Details :

If you are comfortable with javascript, then it should not be very difficult to get it. Actually the main work is done by the libraries ;) behaviour and prototype. Behaviour lets you attach javascript code to html elements in a much cleaner manner. So the funda is,
  • decide upon some classname which you will be using to attach a handler to. In the above example it is "SayMyName".
  • Write a small rule to tell behaviour what to do if it encounters html elements with the "SayMyName" class selector.
  • Since the handler is not attached to any specific event, it is fired immediately on page load(i guess so, not sure though). The handler is also passed a reference of the element which has the said classname("SayMyName").
  • In the handler, get the value for the attribute "name", store it in a variable. Using the prototype library insert a new <i> element with proper text and the name that we want to echo: new Insertion.Before(element, ' Hello ' + helloName + '!!'); We no more need the span, so delete it : Element.remove(element.firstChild);
Well,...thats it. I know it is a bad tutorial, but it was just an effort to write something. So..instead of writing "something", this is far more better :)

Sunday, April 16, 2006

temporary memory loss...??

Well, this has been the second or the third time i have done this. For every 2 months or so, i talk to this girl and tell her that i am attracted towards her, tell her indirectly that i like her and i am telling this only because i dowanna hold on to my feelings and want to just puke it off! I also tell her that i dowanna get distracted by this thought, coz at this stage in my life my parents happines and career is important, and i should not get distracted. I also tel her that please dont take me seriously and take it just as a friend is sharing some thoughts blah blah...!

Some time passes.......minutes..hours...days...

I am very much enthusiastic, full of confidence and all set to break new records(not really) in my job. I write some shit piece of code which my boss just swallows it coz he's a nice guy :) For me my career is important, my parents and my friends. I have no thougt of making new "relations".

This happened to me thrice till now. It was the third time today. I just saw 50 First Dates. Really a nice movie. Oh hooney....i had told you to pay the electricity bills yesterday....i even reminded you today before going to the office and you say you just forgot it?? You are becoming irresponsible day by day honey... :( This is what happens in our daily life. What would happen if your beloved ones cant remember what happened yesterday!! And you still continue everyday with the same spirit, love compassion and care for each other. Thats how the movie is....Sometimes i feel do i have some kind of temporary memory loss pbm?? Not for a single day..but for a few months that i do this same thing?? God help me....


Sunday, April 02, 2006

Write something, daily.

I am writing this post only to make sure that i am in touch with the language. By profession i am a free lance programmer. Ok, i am writing this because, joel says so. He, as well as my boss says that to become a seasoned software engineer, you have to really be into it, do all that is required right from the first phase of the product development to its implementation etc. I usually dont write documentation. Not only me, i feel many of the developers out there writing a couple thousand lines of code are lazy to do it, or feel it is useless or it is not their job. Infact, it is the most critical and important aspect of software development! I have wasted many hours discussing the same, because of no proper documentation. Recently i wrote a small functional spec of a small part of a product that we are developing. I wrote it as per joel's guidelines. It was cool writing it. Also after writing the initial draft, i felt like, "ok....this is some kind of work...a software engineer..". Even my boss was a bit happy about it. He is more a mentor, than a boss to me. Now you see, how confusing this post is. I started with smoething, and i ended somewhere else! Thats what i need to improve, put your ideas in proper words, and only then you will be able to write a good document. That doesn't mean that you should start documenting only after you are a master in english. See, there are many things in life. You have to be aware of everything! You have to fight!

I will try to write something daily, so that my ability to write and put my ideas into proper words, will improve. May be i will start writing about some domain. Not yet decided.