Wednesday, July 15, 2009

DotNet Division Bug?

Here's an interesting exercise for the day.

In Visual Studio 2005/2008,
1. Create a new Console Application.
2. Put in the following code in Main:
double x = 5.15;
double y = x / 100;
Console.WriteLine("y = " + y);

3. Put a breakpoint at the Console.Writeline(..).
4. Press F5 to debug the application.
5. Watch the value of y. What is its value in the watch window?
6. Press F10 to continue with the console write. What is the value of y as displayed?

Do the same for x=4.15, x=6.15,x=7.15,x=8.15.... What are the values displayed?
Try also to divide by 10 or 1000 using the same set of values for x. (E.g. 4.15/10, 6.15/1000). What is the resulting value for y?

Using pen and paper, what are the supposed results? :)

Hint: It appears that there are extra values for the results using x=5.15, x=6.15, and x=7.15. Weird eh?

Labels: , , ,

Monday, June 15, 2009

MessageBox MultiThreading

In one of my WinForms projects, I needed to show a MessageBox dialog. Given the architecture of the program, showing a MessageBox would freeze the window. The user would have to press the 'Alt' key from the parent window to show the MessageBox, and click OK.

As a solution, I multithread the calling of MessageBox.Show(). Nothing new here, but it works.

Labels: , , ,

Monday, March 09, 2009

Quick Update plus Weird VS2005 Error

Quick Update: I moved on to another company since a couple of months ago. Had no time to update this blog for almost a year!

Now, the weird error: I wasn't able to do searches in VS2005. Many of you may have already encountered this error, and I've encountered this a few times already. Thus, I am writing this down so I won't have to google my solution again.

The error in the Find Results window was:
“No files were found to look in. Find was stopped in progress.”

Apparently the solution was to press Ctrl-Scroll Lock. This is the most difficult solution to find without the internet!

Microsoft has a fancier solution.

Labels: , , , ,

Wednesday, April 09, 2008

Free .Net Obfuscator

I've stumbled upon a free .Net obfuscator--Eazfuscator.NET. So far, it works well for my purposes. Most obfuscators are costly--ranging from $200 to $2000! But this one is freeware and it works better than the free one bundled with Visual Studio 2005.

It is free and easy to use!

Labels: , , , , ,

Tuesday, April 08, 2008

Printing PDF Using WebBrowser Control in .Net 2.0

Okay, after almost a year and an offshore assignment, I finally had a chance to update this blog.

I was developing a .Net 2.0 C# windows application wherein I basically have to print PDF files from a Windows application. I tried to use the Adobe Acrobat Reader ActiveX object embedded in my form to preview and print PDF documents. It worked fine; I just have to import the COM component and Visual Studio will do most of the Interop work for me. I then have to locate the file and load it to the AxAcroPDF control:

axAcroPDF1.LoadFile(pdfFileName);

I'd then delete the PDF file immediately afterwards to somehow obscure it from the user. The ActiveX object seems to save it in memory such that I can make a call like:

axAcroPDF1.Print();


...and all went smoothly. But when I ran it on another machine with the most recent version of Acrobat (8.1.2), I got an E_NOINTERFACE exception. After much research, I then decided to use a WebBrowser .Net 2.0 control instead. Thus, my codes looked like so:

webBrowser1.Navigate(pdfFileName);


...assuming of course that the user has a PDF reader installed and integrated into his web browser. This is a much easier alternative than to using unmanaged code. However, it does not go away without problems. When I try to call:

webBrowser1.Print();


I get nothing. I can't seem to force the browser to print its PDF contents. To keep the long story short, I had to tinker with the underlying ActiveX object of the WebBrowser control. First, I needed a reference to shdocvw.dll (windows system32 directory). Then, I did something like:

object n = null;
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
wb.ExecWB(OLECMDID.OLECMDID_PRINT
, OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER
, ref n
, ref n);

It worked! (MSDN says that the two pointers are optional so I just put null in there.)

Labels: , , , , , , ,