Solved

Issue with hidden IE using COM Console App

Posted on 2014-03-24
4
497 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 32

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 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

24 Experts available now in Live!

Get 1:1 Help Now