Solved

Issue with hidden IE using COM Console App

Posted on 2014-03-24
4
517 Views
Last Modified: 2014-05-19
I have a small issue with COM. I wrote a console program with COM, which opens Internet Explorer hidden to the user and operates on a backoffice. Well, the problem is that when send a " click " on a link with " PostMessage ", a new tab opens and the browser becomes visible. ¿Why is this happening? ... ¿Can I prevent this behavior?.

Some code:

HRESULT hr;
hr = CoInitialize(NULL);

if (FAILED(hr))
{
    cout << "Error ConInitialize\n";
    exit(0);
} 

IWebBrowser2 *pwb2;

hr = CoCreateInstance(CLSID_InternetExplorer,
                      NULL,
                      CLSCTX_ALL,
                      IID_IWebBrowser2,
                      (void**)&pwb2);

if (FAILED(hr))
{   
    cout << "Error CoCreateInstance\n";
    exit(0);
}

hr = pwb2->put_Visible(VARIANT_FALSE);  // Turn on hidden, works fine.

if (FAILED(hr))
{
    cout << "Error blah blah\n";
    pwb2->Quit();
    exit(0);
 }

Open in new window

After that I get the document and I send a click on a link with PostMessage and opens a new tab, but the browser becomes visible. ¿¿Why is that?? ¿¿How I can fix it??

Thanks any help. :)
0
Comment
Question by:Janicisa
4 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 39954110
the problem is that the new tab would not "inherit" the "hidden" flag of the previous tab and i don't know whether you could solve that somehow.

instead of using the PostMessage to open a tab with a new link you better would create a second instance of the browser and pass the link to it same way as you do it with the initial instance.

you also could try to write a windows application (which runs a message loop)  with a hidden main window instead of a console program. here the browser could be made child of the main application window and then it should not show-up even if new tabs (new child windows) were created.  

Sara
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39954127
One feasible woraround would be to create a hidden desktop and window station for your IE instance that is invisible to the logged on user. This is actually quite straightfoward and would eliminate all visibility issues you might wncounter, e.g.

HWINSTA hWinSta = CreateWindowStation(_T("MyHiddenWinSta"), 0, 0, NULL);
SetProcessWindowStation(hWinSta);
HDESK hDesktop = CreateDesktop(_T("MyHiddenDesktop"), NULL, NULL, 0, DESKTOP_CREATEWINDOW, NULL);
OpenDesktop(_T("MyHiddenDesktop"), 0, TRUE, GENERIC_ALL);
SetThreadDesktop(hDesktop)

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
si.lpDesktop = _T("MyHiddenDesktop");
ZeroMemory( &pi, sizeof(pi) );
TCHAR szCommand[MAX_PATH];
_sntprintf_s(szCommand, MAX_PATH, _T("%s"), _T(""MyConsoleAppToLaunchIE.exe"));
DWORD res = CreateProcess(NULL, szCommand, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);

Open in new window


See also http://msdn.microsoft.com/en-us/library/windows/desktop/ms687105%28v=vs.85%29.aspx ("Window Station and Desktop Creation")
0
 
LVL 19

Expert Comment

by:darbid73
ID: 40074257
Sorry I am not a C programmer but I have had an issue with IE opening new instances before.

You need to listen for IE' newWindow / 2 Event.  

One discussion on this. But you will find many examples in VBA / VB / C# / C++ etc with both IE and the IE Webbrowser control.

By the way it is my experience that Microsoft has "crippled" some things in a hidden IE so if you find other strange things happening, make your instance of IE visible to see if it fixes things.
0
 

Author Closing Comment

by:Janicisa
ID: 40076677
Well, I had thought of the solution to open new tabs without clicks. It is the only way for now works for IE not visible.  Moreover, you're right that the flag is not inherited. Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Changing the installation path of this MSI 5 94
dialer.exe 4 58
Build VS2015 project in bat command 3 80
Create a path if not exists 7 69
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now