0

I want to create a generation of an invisible browser window in my web application. I am aware that to open a new Internet Explorer instance in an invisible window we can do it with the following commands with PowerShell:

$ie = New-Object -COMObject InternetExplorer.Application

$ie.Navigate2("www.microsoft.com")

$ie.Visible = $False

How can I use this script in my application? I am writing the application using C# and javascript. How can I implement the above script in C#? Could you please assist me on this? thank you in advance.

1 Answers1

0

You should do it the following way:

$iexplorer = new-object -com "InternetExplorer.Application"
$iexplorer.navigate("https://www.microsoft.com")

By default it creates invisible window.

If you need to make it visible you change the Boolean value:

$iexplorer.visible = $true

To make it invisible again just do:

$iexplorer.visible = $false

Edit C# invisible start - I'm pretty sure you will find it on SO The following code should be enough:

ProcessStartInfo ieProcess = new ProcessStartInfo();
ieProcess.FileName = "iexplore.exe";
ieProcess.Arguments = "https://www.microsoft.com";
ieProcess.CreateNoWindow = true;
ieProcess.WindowStyle = ProcessWindowStyle.Hidden;
ieProcess.ErrorDialog = false;
Process.Start(ieProcess);

Edit 2 found another way to hide a window (if the above does not work On MSDN I have found a ShowWindow function. I don't have means to try it now but you could do it the following way:

You will need ShowWindow & FindWindow.

In ShowWindows function tehre are two values valid for your usecase. Definition:

BOOL WINAPI ShowWindow(
  _In_ HWND hWnd,
  _In_ int  nCmdShow
);

HWND - a handle to the window.

mCmdShow (int):

SW_HIDE 0 Hides the window and activates another window. SW_SHOW 5 Activates the window and displays it in its current size and position.

For FindWindow:

HWND WINAPI FindWindow(
  _In_opt_ LPCTSTR lpClassName,
  _In_opt_ LPCTSTR lpWindowName
);

Parameters

lpClassName [in, optional]

Type: LPCTSTR

The class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.

If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.

If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.

lpWindowName [in, optional]

Type: LPCTSTR

The window name (the window's title). If this parameter is NULL, all window names match.

This is probably the lowest you can get. (source code inspired by technet solution) using System; using System.Diagnostics; using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    // meaning defined above
    const int SW_HIDE = 0;
    const int SW_SHOW = 5;

    static void Main()
    {
        Process ieProcess= new Process();
        ieProcess.StartInfo.FileName = "iexplore.exe";
        ieProcess.StartInfo.Arguments = "https://www.microsoft.com";
        ieProcess.StartInfo.UseShellExecute = false;
        ieProcess.StartInfo.CreateNoWindow = true;
        ieProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        ieProcess.StartInfo.LoadUserProfile = true;
        ieProcess.Start();

        //IntPtr hWnd = FindWindow(windowName, null);
        IntPtr hWnd = FindWindow("Internet Explorer", null)
        if (hWnd != IntPtr.Zero)
        {
            ShowWindow(hWnd, SW_HIDE); // Hide console window
            ieProcess.WaitForExit(); // Tells you if the stdout or stderro should be synchronous or asynchronous 
        }
    }

}
tukan
  • 17,050
  • 1
  • 20
  • 48
  • how is it possible to do this in C#? Thank you –  Apr 12 '18 at 09:36
  • @homeostasis of course, I won't write it as it was already written. Check the **SO** - https://stackoverflow.com/questions/1431115/open-a-webpage-in-ie-using-c-sharp. One linder from the link `System.Diagnostics.Process.Start("iexplore", "http://www.microsoft.com");` – tukan Apr 12 '18 at 09:37
  • Why is your question tagged with *powershell*? – tukan Apr 12 '18 at 09:39
  • ok so `System.Diagnostics.Process.Start("iexplore", "http://www.microsoft.com");` but to make it invisible? –  Apr 12 '18 at 09:42
  • if you want to make it invisible you have to define it differently I'll edit the post. – tukan Apr 12 '18 at 09:58
  • @tunkan, In fact, I tried something very similar to your answer and it still both implementations was giving the same result - opening the internet explorer instance. –  Apr 12 '18 at 10:13
  • @homeostasis I see, you should have mentioned in the post. I'll check the MSDN I think there is another function to hide window. – tukan Apr 12 '18 at 10:21
  • i would be very grateful if you could help, thank you –  Apr 12 '18 at 10:33
  • thank you for your answer, where should I place ShowWindow and FindWindow functions in order to work? –  Apr 12 '18 at 11:59
  • @homeostasis did you see the source code? Do you understand it? That is complete class definition with `static void Main()`. It should be defined as it is. – tukan Apr 12 '18 at 12:08
  • it does not hide the IE window that is why i asked –  Apr 12 '18 at 12:17
  • @homeostasis I see. Did you try it without `ieProcess.WaitForExit();`? Or maybe the `windowName` is not "Internet Explorer" you need to check that. – tukan Apr 12 '18 at 12:19
  • @ one problem was that windowName for IE is "IEFrame", but still the window is visible –  Apr 12 '18 at 12:35
  • @homeostasis Hmm that is only a tab (frame) you would have to enumerate via all frames. Are you sure you got a handle? If you try `if (hWnd == IntPtr.Zero) {MessageBox.Show("no IE instance"); return; }` does it jump into this? – tukan Apr 12 '18 at 12:46
  • Maybe this can help https://stackoverflow.com/questions/37594806/get-internet-explorer-tab-title – tukan Apr 12 '18 at 12:47
  • @homeostasis OR simplier find it by page title e.g. for Microsoft.com - `IntPtr hWnd = FindWindow(null, "Microsoft - Official Home Page - Internet Explorer")`. Will change based on your location however. – tukan Apr 12 '18 at 12:55
  • In fact the hWnd value it is not 0 every time I have put if to catch if it is zero. Problem is that hide function does not work properly –  Apr 12 '18 at 13:01
  • @homeostasis well then I would do it as follows: First test it if it works on simple application like notepad. If it woks then you have some kind of ie exception and you can play around with it, if it does not work even for notepad then there is mistake in the code. The least effort, in my eyes, would be to run the powershell script from the c# code. – tukan Apr 12 '18 at 16:03