Solved

Issue with hidden IE using COM Console App

Posted on 2014-03-24
4
544 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 20

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

820 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