Wednesday, June 5, 2013

.Net PrintDocument.Print throw the handle is invalid

I have faced this issue when I have .Net command line program which call from a window service in order to print a document to the printer. The program can be run successfully when I call it from the commend line but it will throw this error when it is triggered from a window service.

Finally I find out that when I set the target CPU to "Any CPU", it runs successfully. Originally is set to "x86" and my OS is x64

It has wasted me half day to find out and sounds to me strange that the "x86" target CPU should be more generic for compatibility, but it will have error in such case. may be the caller window service is x64 and so the command line program is required to align with it.