Solved

Issue with hidden IE using COM Console App

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

810 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