IE screenshot problem in c# windows service - c#

I am using win 2003 server.
I have windows service created in c# .net, it opens IE at every 5 min with predefined URL, get screen shot of it and save it to database. It seems a simple application. But my problem is, when machine is lock (window key + l) or machine is logged off, IE process is start, but cant take screen-shot, it takes "black page" only.
Is there any option to run IE at login screen? or is there any work around of this problem?

As posted in Generate WebPage Thumbmail Screenshot Image you could try using a WebBrowser control in your service and generating a screenshot from that.

The service needs to run with the "interact with desktop" check box ticked. The window needs to be upper-most in the Z-Order when the screen shot is taken and not overlapped by other windows. This approach is quite error prone...as I have found.

To interact with desktop using code:
Put this just before you start the service.
String sYourServiceName = #"MyService" //Change this to r service name
ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new System.Management.ManagementScope(#"root\CIMV2", co);
mgmtScope.Connect();
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + sYourServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
or
Try to run iexplore "Internet explorer" as a system account from the logon page. Just execute Process.Start("Iexplore.exe") from your Windows service and it should work, but I am not sure if you can execute iexplore as a system level account.
Otherwise, you can try to block the lockout screen by writing a basic key grabber or a key logger to block MENU+L lockout, you can do this by enabling interaction with desktop. I posted the code to make it easier or do it manually by going to services and clicking on your service. There you should see a checkbox with the text interact with desktop.
//M

Related

Run C# app on Startup?

I have created a simple weather application and I added the code below to let the user let it run on Startup:
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (startupCheck.Checked) {
rk.SetValue("WeTile", "\"" + Application.ExecutablePath.ToString() + "\"");
} else {
rk.DeleteValue("WeTile", false);
}
Now this runs fine on both my computers. But when I gave the app to my girlfriend. She said the app does not run on windows start up. I read it online that it could be because of the user permission or the location so I told her to move the app to c:/ and try checking the box again and then restarting. Now it works but on every startup she has the default windows message saying you want to run this app?
How do I get rid of this? What is the best way to add to windows startup that works with both windows 32/64 bit without any user permission disruptions?
It sounds like you may have run afoul of Windows' file blocking security function. Applications created on another computer are automatically blocked from executing unless the user specifically "unblocks" the file. Have your girlfriend right-click on the executable, select "Properties" and see if there is a button at the bottom of the dialog to unblock the file.
Once unblocked, you should no longer see the confirmation prompt at startup.
You could add it to the Windows startup folder, check if it's not there already and if not, add it (assuming this is what the user wants).
See How do I set a program to launch at startup

IIS 7 Print in not working

I am trying to print a document which is working fine in my Visual studio 2010 application but when i am publishing my project on IIS 7 then printing is not working and i cant see any error in the event viewer .
MyProcess = new Process();
MyProcess.StartInfo.CreateNoWindow = false;
MyProcess.StartInfo.Verb = "print";
MyProcess.StartInfo.FileName = destinationPath;
MyProcess.Start();
MyProcess.WaitForExit(10000);
MyProcess.Close();
When you're running in Visual Studio, you're running as a logged-in, interactive user.
When you're running in IIS, well, you're not any of the above.
The way this is normally done in a web application is to:
Display the document to the user in the browser
Print the document by using the 'window.print' function from JavaScript.
If anyone still cares for an answer..
I had the same problem, solution was to give IIS user access to use installed printers on the computer. When you print from IIS , you're logged in as a system default user who by default doesn't have proper printer access setup in the registry.You need to give printer access to the default system user by adding few entries in the registry. Just follow this tutorial like I did http://support.microsoft.com/?kbid=184291.
It will fix it.
If the printer is not installed on the server, nothing is going to happen.
If you're trying to print from ASP.NET code to a printer attached to the client computer it's never going to work. The server cannot get to and use any resources on the client computers.
2nd and important thing change to LoadUserProfile to true in IIS Application pool.

How to run RDC Windows application

I tried lot in running RDC windows application on there I'm trying to capture details.
Like ---- > connecting Myremotedesktop ----> Run the windows application in rdc and enter values to get details (eg if I enter rollnumber address details will come) ----> capture details data and store it on my local desktop.
Is there any way to automate this process using c# code?
I'm able to connect remote desktop.
I tried the UIAutomation and the below code working fine for local desktop:
System.Threading.Thread.Sleep(500);
AutomationElement rootElement = AutomationElement.RootElement;
Condition appCondition = new PropertyCondition(AutomationElement.NameProperty, "Untitled - Notepad");
Condition documentCondition = new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, "document");
AutomationElement documentElement = rootElement.FindFirst(TreeScope.Subtree, documentCondition);
documentElement.SetFocus();
SendKeys.SendWait("This is a test of the emergency broadcast system.")
How to do this same thing on remotedesktop?
Due to your requirement of not being able to put any software on the server side of the RDP connection remote control like you want to do is impossible unless the buttons you want to click are int he same position every time and you hard code the X,Y values of where to move the mouse in your automation script.
If you could run something on the RDP server you could take your automation code and write a listener program that would talk to your client over Virtual Channels which would allow you to pass messages to your listener over the RDP connection and the listener could perform the automation tasks server side.
Can you force the client side to be Windows 8? I believe that UI Automation requests get "tunneled" through the RDC connection on Windows 8.

How do I give my windows service admin rights

I have a winform that allows me to enable and disable all my 8 year old's network adapters using this code:
protected override void OnStart(string[] args)
{
//start timer
SelectQuery query = new SelectQuery("Win32_NetworkAdapter","NetConnectionStatus=2");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach (ManagementObject result in search.Get())
{
NetworkAdapter adapter = new NetworkAdapter(result);
adapter.Disable();
enabled = false;
}
InternetCheckTimer.Start();
}
This code works fine on a win form assuming I am running with admin. I have never written a win service before so the problem might be else where, I am able to install using installutill and attach the debugger to the process, however no break points are hit. I have tried starting and stopping the process and cannot get the debugger to attach so I might be doing that wrong as well... Right now I am assuming that the code is running and I am too stupid to get the debugger working. That said, I think my code requires the service to have admin like the form did in order to work.
Sorry if this is unclear, I will do my best to clear it up if you need more information.
Use user and password for the service, and assure that the user that start the service has all rights needed for the application to run.
An other possibility if you can control the service is to use impersonation, see: http://www.codeproject.com/Articles/4051/Windows-Impersonation-using-C.
You can try couple of options.
Add app.manifest
After installing window service set the service permission to run at Highest Privilege and also you can set the user name and password for the logged in user.
How to Get Full Administrator Rights in Windows 7:
Click Start
Click Computer (you might also find this icon on the desktop).
Right click on the Hard Disk icon where your OS is installed on and click Properties.
Click the Security tab.
Click the Advanced tab.
Click the Change Permissions button located after the Permission Entries list.
A new window will appear on your screen; which contains a list of all the User Accounts
Select the user account you want to give total control over your Windows 7) and click the Edit button.
Now, tick the checkbox labeled "Total Control" and press OK.
You're all done!

Selenium 2 (webdriver): Taking a Screenshot returns a black image

I am using Selenium 2 (Webdriver), in an ASP.NET website to build a service, where users can enter their URL and gets screenshots of the page, made with different browsers.
My page is hostet on an Windows Server 2008 R2.
Taking Screenshots with FirefoxDriver works perfect.
But when I am using InternetExplorerDriver, I just get an empty black file.
The App is running as Administrator - so there should't be permission issues.
My Code:
// Opening the Browser
var ieCapabilities = DesiredCapabilities.InternetExplorer();
ieCapabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
var browserIe = new InternetExplorerDriver(ieCapabilities);
browserIe.Navigate().GoToUrl("http://www.google.com");
// Screenshot
var dir = Server.MapPath("/screenshots/");
browserIe.GetScreenshot().SaveAsFile(dir + "Filename.png", ImageFormat.Png);
browserIe.Close();
Any ideas why my file is black?
THANKS!
There's probably nothing wrong with your code. Although, I'm using Java, so I can't tell for sure.
I had the same issue with IE while FF and Chrome worked fine.
This post suggests that starting the Selenium Server via a remote desktop connection could lead to problems.
Some other posts suggest that the screen saver might have something to do with it.
I just tried leaving the remote desktop connection open and it solved the black screenshot issue. Also logging in via VNC seems to work, leading me to the theory that Windows locks the screen after terminating the remote desktop connection while leaving it unlocked if using VNC.
This post suggests that disabling screenshots when the screen is locked is a Windows Security feature.
InternetExplorerDriver mydriver = new InternetExplorerDriver();
mydriver.Navigate().GoToUrl("http://www.google.com/");
Screenshot myScrennShot = ((ITakesScreenshot)iedriver).GetScreenshot();
myScrennShot.SaveAsFile(#"C:\Path\123.png", ImageFormat.Png);
//or
byte[] data = myScrennShot.AsByteArray;
It works for me, probably it does work for you too :-) If it doesn't work I suggest you to separate this code to different service (WindowsService) because in you case this issue maybe connected with application pool restrictions. Anyways, please let me know how is it going.

Categories