Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Issue with hidden IE using COM Console App

Posted on 2014-03-24
4
Medium Priority
?
584 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

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 1500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

670 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