Saturday, January 29, 2011

Issue to get process id of the IExplore on Windows vista or later

If we want to get id  of the process and get detail about that process or kill that process 

Then it’s not as strait as getprocessbyName inbuilt method. 


This issue happens to get detail about process iexplore. With windows version later than vista and IE version higher than 6 this issue occurs. 

This issue is due to the way the new IE manage the session so they are managing the process created in some different way. So if we get directly id by “getProcessByName” or “getProcessByID” will not work. 

For that I have found on net one good solution to get Id of IE process on vista or later by below link..


http://www.jeff.wilcox.name/2008/09/using-ielaunchurl-in-c-to-launch-a-protected-mode-internet-explorer-7-window/

Thanks to Jeff Wilcox here it’s given nice explanation and implementation of the whole procedure to get the Id of the process for vista or later. 

So after you get the id of the process with the above method  is not only enough then after have to get parent process of the process get by above method. 


So this parent process is the actual process started by IE. So we can use id of that to do whatsoever  kill process or retrieve some information. 

Hope this will be helpful to resolve really weird issue with the IE process on vista or later.

Thanks
Tushar 

Automate windows authentication process in browser

Once i was faced issue to show the windows authenticated site  browser control in window application.

Becuase it shows pop-up to ask credentials but i want to automate that task so that it not ask for

credential when passed in code.

For that form class has to be implemented from

IOleClientSite, IServiceProvider, IAuthenticate
 
definitions for that you can find on net.
 
after insert defination of above inside our project inside on class you can implement that 
inside form code.
 
So then on form load you have to first move to "about.blank" page before actually navigate
the page you want to access. Because it useful to eliminate issue IE sometimes not
call authenicate process of IAuthentiate at first run.
So form load event has to be something like
private void form1_Load(object sender, EventArgs e)
    {
 
//Navigate to about:blank to avoid authentication
// issue on Ie
    string oURL = "about:blank";
    webBrowser1.Navigate(oURL);

   // NOe set the ole object 
    object obj = webBrowser1.ActiveXInstance;
    IOleObject oc = obj as IOleObject;
    oc.SetClientSite(this as IOleClientSite);
    } 

 two methods which needs to be implemented here is as below
 
public static Guid IID_IAuthenticate = new Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b");
    public const int INET_E_DEFAULT_ACTION = unchecked((int)0x800C0011);
    public const int S_OK = unchecked((int)0x00000000);
 
 
public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
    {
    int nRet = guidService.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
    if (nRet == 0)
    {
    nRet = riid.CompareTo(IID_IAuthenticate); // Zero returned if the compared objects are equal
    if (nRet == 0)
    {
    //This method returns an interface pointer that represents the requested interface on the specified object. It is particularly useful if you have an unmanaged method that expects to be passed an interface pointer; Add to the COM interface reference count.
    ppvObject = Marshal.GetComInterfaceForObject(this, typeof(IAuthenticate));
    return S_OK;
    }
    }
    ppvObject = new IntPtr();
    return INET_E_DEFAULT_ACTION;
    }
 
 public int Authenticate(ref IntPtr phwnd, ref IntPtr pszUsername,
    ref IntPtr pszPassword)
    {
    //Copies the contents of a managed String to a block of memory allocated from the unmanaged COM task allocator.
    IntPtr strUser = Marshal.StringToCoTaskMemAuto(txtUserID.Text);
    IntPtr strPassword = Marshal.StringToCoTaskMemAuto(txtPassword.Text);


    pszUsername = strUser;
    pszPassword = strPassword;
    return S_OK;
    }
 
 
 
I hope this above information is useful to resolve the issue  like i have faced.
 
If you want the complete souce code implementation than mail me. i will be back to you with that.
 
 
 
Thanks
Tushar