|
|
Subject:
Detection of Dialog Boxes
Category: Computers > Programming Asked by: kirra-ga List Price: $20.00 |
Posted:
05 Mar 2003 23:03 PST
Expires: 04 Apr 2003 23:03 PST Question ID: 172549 |
We have a server-side application which uses Microsoft office automation (using C#) for converting office documents into plain text format. Under normal conditions, this process works fine, however, there are a number of situations which occurs when popup windows are generated. Examples include if the document is corrupted, if the document contains a link to another document, which requires password entry, and so forth. From Microsoft's web-site, there is the following statement: 1. Interactivity with the Desktop: Office Applications assume that they are being run under an interactive desktop, and may in some circumstances need to be made visible for certain Automation functions to work properly. If an unexpected error occurs, or an unspecified parameter is needed to complete a function, Office is designed to prompt the user with a modal dialog box that asks the user what they want to do. A modal dialog box on a non-interactive desktop cannot be dismissed, which causes that thread to stop responding (hang) indefinitely. Although certain coding practices can help reduce the likelihood of this occurring, they cannot prevent it entirely. This fact alone makes running Office Applications from a server-side environment risky and unsupported. Popups can be minimised by setting the appropriate application settings, however it is always possible under some circumstances that they will occur. Is it possible to write a program which can check for the detection of dialog boxes, and simple "press" the cancel button, so that process does not stall? Alternatively, is there a way to monitor all office applications running on a box, and terminate any which appear to be in "waiting for dialog response" state? Another possibility is to terminate an office application forcibly if it has been running longer than X minutes. Is this possible to do in all circumstances? |
|
Subject:
Re: Detection of Dialog Boxes
Answered By: samrolken-ga on 06 Mar 2003 15:02 PST |
I mixed something that will do what you need. I used Notepad instead of Microsoft Office to have Dialog Boxes, but it should work the same way. The first thing you need to do is import a couple of API functions from user32.dll. [DllImport("User32.dll",EntryPoint="FindWindow")] private static extern int FindWindow(string lpClassName,string lpWindowName); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); Then, just write a few lines of code to use these functions to do what you want. In this example, I'm using notepad. I've just typed some stuff without saving, then asked Notepad to quit. There's a dialog box on the screen asking if I want to save. //Find the window. int a = FindWindow(null, "Notepad"); //bring it up, converting the int returned by FindWindow into an IntPtr SetForegroundWindow(new System.IntPtr(a)); //sleep just long enough to let the window come to the front. System.Threading.Thread.Sleep(100); //send an N, indicating we don't want to save the file. //You could send an ESC here, or whatever you want. SendKeys.Send("N"); If there's anything I've missed, just let me know! Sources: http://www.c-sharpcorner.com/FAQ/Create1InstanceAppSC.asp http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcesdkr/htm/_wcesdk_win32_findwindow.asp -- samrolken-ga |
|
There are no comments at this time. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |