ActiveReports 7 throws invalid handle exception printing from Windows Service - windows-services

I have and application that is running as a windows service on Server 2008. When I run it as an application and send a print job, the print job gets processed sucessfully. However, if I send the print job to the windows service instead, it throws "The Handle is invalid" exception.
Here is the error:
Printing Thread error: The handle is invalid. StackTrace: at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e) at System.Drawing.Printing.PrintController.Print(PrintDocument document) at System.Drawing.Printing.PrintDocument.Print() at GrapeCity.ActiveReports.Viewer.Win.Printing.PrintImpl.DocumentPrinter.<Print>b__1e()
The service is running under an administrator account which has admin permissions.
Any help would be appreciated!

A coule things to check:
Make sure that the user account that the service is running under has the proper printer installed under his account and that it is set as the default printer.
Make sure you're using the Print method in ActiveReports that explicitly does not show any UI.
Try checking the "Allow service to interact with desktop" checkbox on the service's property page from the Services control panel applet. Although you may not be showing any UI, the printer driver may need that capability.
Finally, just keep in mind that in general printing from a Windows Service is kind of flaky. Even Microsoft's documentation for System.Drawing.Printing has the following disclaimer:
Classes within the System.Drawing.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.
That doesn't mean it won't work, but that it is error-prone enough that Microsoft doesn't want to help us printing in this case. I suspect this is because the printer drivers from the manufacturers are not necessarily designed for printing from non-UI processes like windows services.

Roussul
It could be a permission or printer access problem. To check whether it is related to ActiveReports or not, can you create a separate app, implement system.printing.drawing and run it as a service, just as you have it with ActiveReports and let us know.

I had this issue in a ASP.NET application. The root cause was that "Enable 32-bit applications" had been set in the IIS application pool that the website was running in.
It appears the print function tried to connect to a 32 bit driver rather than the 64 bit drivers that were installed.
Maybe in a .NET Windows app the "Target CPU" setting could also cause a similar issue? ie if you target x86 and try and run your app on x64 it will only connect to 32bit printer drivers?

Related

AspNetZero V8.0 - Debugger ends immediately

I just started coding with AspNetZero V8.0 solution. I purchased this version a couple of years ago. I had kept the raw downloaded solution in a GIT Repo without any changes.
Recently I downloaded the code for use in my project. All I have done so far is setup connection string, setup a tenant and logged into a tenant. After I got all that working I started development for my first domain class "Country". I have setup the domain classes, interfaces, application service and EF migration. All of this worked without any issues.
Now I have setup app service for Country domain and I am trying to test it from chrome console, as per the AspNetZero docs here. When I test the service I'm getting an error in the console and an error is getting logged in the AuditLogs table too. So in order to identify my issue with app service, I decided to debug the code. This is where my problem exists.
I am able to start the debugger. I see the chrome browser window open up, I see VS2019 (v16.8.4) switch to debug layout. After about 30+ seconds the browser closes and VS2019 returns to normal mode, not in debug, layout. I do not get any errors logged in chrome console, windows event log or in VS2019 output.
I have read many threads on SO about similar issues and tried many different VS2019 option changes, nothing seems to make a difference.
I tried a couple of other solutions, AspNetZero and other .NET Core, in VS2019 and I am able to debug them normally.
Has anyone else run into similar issue with AspNetZero solution or any other type of solution?
This is because they run the YOURCOMPANY.Web.Host as .exe OutputType.
To run and debug the project, open CMD on the YOURCOMPANY.Web.Host location then execute dotnet run
After that:
Attach to a running process on your local machine
In Visual Studio, select Debug > Attach to Process (or press
Ctrl+Alt+P) to open the Attach to Process dialog box.
Check the Connection type.
In most scenarios, you can use Default. Some scenarios may require a different connection type. For more info, see other sections in this article or Common debugging scenarios.
Set the Connection target your local machine name.
Finally, attach.
For more information:
Attach to running processes with the Visual Studio debugger

My application icon is receiving the "administrator shield"

My Win32 Delphi application icon is receiving the "administrator shield" on a specific Windows 7 computer despite the fact that:
I'm not using the application manifest that requires administrator access
the application name doesn't include any words like "setup", "install", or "update"
the application doesn't have "run as administrator" selected in the shortcut properties for the user, "all users", or in the advanced properties.
Fortunately, this is a very rare situation. It, however, is experienced by a customer who is very far away.
this came to my attention because the runtime library TPath.GetTempFileName method is raising an exception "The directory name is invalid". when running a test application (not as administrator), the temp path is available and R/W access is available. don't know what happens if the test application is run as administrator.
I have mused that maybe it was installed by the administrator or that the UAC settings on that computer are very strict in some way.
It is an older version of my application but I don't like the fact that it is not understood what the problem is.
Being an old version of the application, I'm not inclined to dig deeply to solve this; in a way, it has been solved but I'd like to understand why.
Only looking for quick answers here--not an in-depth community effort.
thank you!

Delphi 7 Application and Microsoft Security Essentials

We have a Delphi 7 application running on numerous client machines. Recently, some of the client machines started using Microsoft Security Essentials. It started identifying our executable as malware and promptly shut it down. The message displayed by MS Security essentials is:
"Security Essentials detected items on your PC that it doesn't recognize......"
Odd thing is it does not always occur at the same option in the application. You can do the very same operation on subsequent logins and sometimes it works and other times security essentials closes it down. This makes it extremely hard to narrow down to a specific cause in our application.
I tried running the application with elevated account privileges and was still able to get it to fail. I was unable to duplicate the issue when running a Delphi XE2 compile of the same application.
Any ideas about what to look for? We are really trying to avoid adding our application to the Security Essentials exclusion list. Our application has never been identified as a problem with varying security programs (norton, mcaffee, etc.).
I once had a similar issue with an executable built using Delphi(7), though it had nothing to do with Delphi-7. It just so happens that some part of the executable matches some virus signature or the AV heuristic scan suspects that something is wrong with the executable. One thing you can try is to change some of the compiler settings such as Debug options. Changing Debug Information or using debug DCUs might result in a slightly different byte sequences in the final executable.

Developing Apps with Administrator Rights in Delphi

I'm using D2010 under Windows 7 to write an app that seems to require admin rights (I think because it uses COM to communicate with a third party .exe, which also requires admin rights).
I've added the manifest resource as required, but when I try to debug the app from the IDE, it reports
"Unable to create process. The
requested operation requires
elevation"
...and it won't run. If I run Delphi as administrator, then my app runs correctly, but this feels like a dangerous brute force approach, especially as most of the apps I develop don't need admin privileges.
Is there any way of getting Delphi to prompt for elevation just when I run my app, rather than having the whole IDE run elevated?
UAC also catches any application that has the words "setup", "update" or "install" in their name or in many of the Version Resource fields. (Company name, App Name, Description etc.
It considers any such application to be a potential "installer" application and therefore must be run with Admin privileges.
Sounds crazy, but it's true. See the "Installer Detection" section in this document.
You can get around this by including a manifest that says that it doesn't need admin privileges.
There is none, it also doesn't work for VS:
https://stackoverflow.com/questions/3265787/how-do-i-debug-an-process-as-elevated-with-visual-studio-2008-sp1-on-windows-7
I guess you could run the remote debugger elevated and attach the IDE using remote debugging though.
It doesn't work, because the process is running as another user (or using another user token).
The IDE is trying to run the debugging process using CreateProcess and that fails when the application requires elavation, more details can be found in this article:
http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx
RAD Studio could run the application using ShellExecute with the "runas" verb, but this still doesn't solve the "debugging process under other user context" issue.
In other words: An elevated process can only be debugged by an elavated debugger.
Edit:
The Delphi XE2 IDE is 32-Bit and can debug 64-Bit applications only through the remote debugger (which is cleverly hidden from the user).
I guess Embarcadero could make it possible to debug elevated applications in a similar fashion.
The only way I know to debug such an app is to run the IDE as administrator. I wouldn't recommend doing this routinely, just for debugging sessions.
Actually it may be possible to debug a process with "higher level access" from a process with "normal level access".
At least until Windows XP it is sufficient to add your user account to the Debugger Users group to debug processes running e.g. with Administrator privileges.
That doesn't solve Delphi 2010s problem to run the process at all, but may be useful if you try to attach the debugger to a already running process.
I don't think this works on UAC enabled Vista+, but I thought I'd mention it anyway. :)
To be exact the UAC split token concept disables the SeDebugPrivilege:
http://blogs.msdn.com/b/greggm/archive/2006/03/30/565303.aspx
http://blogs.msdn.com/b/mithuns/archive/2006/04/04/568291.aspx
Run Delphi (any version) as Administrator.
You can change Delphi shortcut properties also (to run it always as administrator).

"There is insufficient memory. Save the document now." error while opening word 2003 document

I have developed one windows service through which I am trying to open an word document. But when service tries to open document, it logs following error "There is insufficient memory. Save the document now."
If I run the same program as normal windows application, it works fine but problem comes only when I try to run the program as windows service.
I tried to google this problem, but there arent many answers about this problem. Is there any way to increase the memory limit for windows service or process? Something like in Java world, where we can specify memory limit while executing java class java -Xmx800M. Is there something similar for windows processes?
Does anyone knows how to solve this problem? I am using C#, .net framework 2.0 and office 2003 for development.
I found out that office automation is not recommended by microsoft on server side code.
More information can be found here.
EDIT :
I found one more article regarding office automation in server side code.

Resources