Question

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";//open Internet Properties window
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process.Start(proc);
SendKeys.Send("{TAB}{TAB}{TAB}{TAB}{ENTER}");// Open Lan Setting window

I've tried many way to hide/ close "Internet Properties window" and Lan Setting Window after it's called, but this code doesn't work. Help me !

Était-ce utile?

La solution

I think you should find another way to play with your proxy problem, but if you still want to use the trick which plays with showing and closing dialogs, I have a solution here which would help (I've tested) and you don't have to use SendKeys which I feel very unstable. Here is my code:

//You have to add these using first:
//using System.Runtime.InteropServices;
//using System.Threading;
//This is used to find a window
[DllImport("user32", CharSet=CharSet.Auto)]
private static extern IntPtr FindWindow(string className, string windowName);
//This is used to find Button in a window
[DllImport("user32")]    
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);
//This is to help you Click on a Button
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);    
//---------------------------
//Here are our methods 
//---------------------------    
//This is used to Get handle of a Button of a Parent window by its Text
private IntPtr GetButton(IntPtr parent, string text)
{
   return FindWindowEx(parent, IntPtr.Zero, "Button", text);
}
//This is used to Click on a Window (usually a Button) with its Handle passed in
private void ClickWindow(IntPtr hwnd)
{
   SendMessage(hwnd, 0x201, IntPtr.Zero, IntPtr.Zero);
   SendMessage(hwnd, 0x202, IntPtr.Zero, IntPtr.Zero);
}
//This is used to find the Local Area Network (LAN) Settings window
//This is called in a separate thread, because somehow the LAN settings window
//showing causes the main Form not-responding (we can't call anything in our main thread).
private void SearchForLanSettingsWindow()
{
        int i = 0;
        while (true)
        {
            Thread.Sleep(100);
            IntPtr windowHandle = FindWindow(null,"Local Area Network (LAN) Settings");
            if (windowHandle != IntPtr.Zero)
            {
                //Find the button OK, if you like, you can replace it with "Cancel",...
                IntPtr button = GetButton(windowHandle, "OK");
                //Click on that OK button to Close your Lan settings window
                //You may want to research on the DestroyWindow or CloseWindow
                //win32 api without having to click on a Button, but I think this should be better. It's up to you.
                ClickWindow(button);
                break;
            }
            i++;
            if (i > 20)//timeout
            {
                break;
            }
        }
}
//And here is your code with my code appended
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";//open Internet Properties window        
proc.UseShellExecute = false;
System.Diagnostics.Process.Start(proc);

Thread.Sleep(100);//Sleep to be sure the Window is really created
//Get the handle to the window "Internet Properties"
IntPtr mainHandle = FindWindow(null, "Internet Properties");
//Find the tab "Connections", this tab has class "#32770" and is a child window of the window "Internet Properties"
IntPtr child = FindWindowEx(mainHandle, IntPtr.Zero, "#32770", "Connections");
//Get the button "LAN settings"
IntPtr button = GetButton(child, "&LAN settings");
//Create new thread and start it to find the Lan settings window 
//we have to do this now because for some reason, after the LAN settings window shows
//we can't call any code in our class.
new Thread(SearchForLanSettingsWindow).Start();
//Click on the LAN settings button to Show the LAN settings window
ClickWindow(button);
//Get the button OK on the window "Internet Properties"
button = GetButton(mainHandle, "OK");
//Click on that button to close the window "Internet Properties"
ClickWindow(button);

And that's all.

I've found that if the computer is installed with a non-English language, the Buttons OK, LAN settings may be different. So the better solution is to use GetDlgItem() to get the buttons from their IDs. To do so, you have to import the function GetDlgItem() first:

[DllImport("user32")]
private static extern IntPtr GetDlgItem(IntPtr dlgHandle, int itemID);

I used Spy++ to know what the control ids of OK and LAN settings are. The control ID of OK is 1 and the control ID of LAN settings is 0x62C. So to get the handles of those buttons you can use this code:

IntPtr button = GetDlgItem(parent, 1);//OK button
button = GetDlgItem(parent, 0x62C);//LAN settings, remember that the dialog containing LAN settings button is Connections not the Internet Properties.

Here is another solution using Process.Kill(), I'm not sure if killing the RunDll32.exe will be OK, but if it's OK this will be another solution which is even cleaner:

//You have to add these using first:
//using System.Runtime.InteropServices;
//using System.Diagnostics;
//using System.Threading;
//This is used to find a window
[DllImport("user32", CharSet=CharSet.Auto)]
private static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32")]    
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);
//This is to help you Click on a Button
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
//This is used to get a Button (as an item) on a dialog
[DllImport("user32")]
private static extern IntPtr GetDlgItem(IntPtr dlgHandle, int itemID);
//---------------------------
//Here are our methods 
//---------------------------    
//This is used to Click on a Window (usually a Button) with its Handle passed in
private void ClickWindow(IntPtr hwnd)
{
   SendMessage(hwnd, 0x201, IntPtr.Zero, IntPtr.Zero);
   SendMessage(hwnd, 0x202, IntPtr.Zero, IntPtr.Zero);
}
//This is used to find the Local Area Network (LAN) Settings window
//This is called in a separate thread, because somehow the LAN settings window
//showing causes the main Form not-responding (we can't call anything in our main thread).
private void SearchForLanSettingsWindow()
{
    int i = 0;
    while (i < 20)
    {
        Thread.Sleep(100);
        IntPtr windowHandle = FindWindow(null,"Local Area Network (LAN) Settings");
        if (windowHandle != IntPtr.Zero)
        {
            if(runDll32 != null) runDll32.Kill();
            break;
        }
        i++;
    }
}
//the Process RunDll32
Process runDll32;
//And here is your code with my code appended
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";//open Internet Properties window        
proc.UseShellExecute = false;
runDll32 = Process.Start(proc);

Thread.Sleep(100);//Sleep to be sure the Window is really created
//Get the handle to the window "Internet Properties"
IntPtr mainHandle = FindWindow(null, "Internet Properties");
//Find the tab "Connections", this tab has class "#32770" and is a child window of the window "Internet Properties"
IntPtr child = FindWindowEx(mainHandle, IntPtr.Zero, "#32770", "Connections");
//Get the button "LAN settings"
IntPtr button = GetDlgItem(child, 0x62C);
//Create new thread and start it to find the Lan settings window 
//we have to do this now because for some reason, after the LAN settings window shows
//we can't call any code in our class.
new Thread(SearchForLanSettingsWindow).Start();
//Click on the LAN settings button to Show the LAN settings window
ClickWindow(button);

Again, I think, you should find another solution to do what you want originally. This kind of solution is just a trick. You may want to use MoveWindow win32 function to move all the dialogs out of the screen.

PS: này, em làm (hay vẫn đang học?) trong ngành tin thật à? Ở đâu vậy? How old? nice to meet you on stack over flow :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top