Google Answers Logo
View Question
 
Q: Detection of Dialog Boxes ( Answered,   0 Comments )
Question  
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?
Answer  
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
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

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 Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy