locked
IWebBrowser2 pure c++ component session problem RRS feed

  • Question

  • Hi,

    I created a Webbrowser component using IWebBrowser2 for IE version 11 in pure c++ but when i reenter or opens a new

    instance of it

    nothing is being saved from last session no autocomplete, no session data etc... looks like my code is a "standalone" browser,

    it

    doesn't even share data between my own instances.

    for example when i open the new window in IE it "remembers" all.

    i also created a dummy project in c# there it works just fine :( what am i missing?

    Regards,

    Roy


     


      

    Monday, May 28, 2018 9:32 AM

Answers

  • Minimal code with ATL =>

    #include <windows.h>
    #include <tchar.h>
    #include <exdisp.h> // IWebBrowser2
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hATLDLL;
    typedef BOOL(__stdcall *PAAWI)(void);
    PAAWI pAtlAxWinInit;
    typedef HRESULT(__stdcall *PAAGC) (HWND hWnd, IUnknown** pUnknown);
    PAAGC pAtlAxGetControl;
    
    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    	hInst = hInstance;
    	hATLDLL = LoadLibrary(_T("atl.dll"));
    	pAtlAxWinInit = (PAAWI)GetProcAddress(hATLDLL, "AtlAxWinInit");
    	if (pAtlAxWinInit)
    		pAtlAxWinInit();
    	pAtlAxGetControl = (PAAGC)GetProcAddress(hATLDLL, "AtlAxGetControl");
    	WNDCLASSEX wcex =
    	{
    		sizeof(WNDCLASSEX), 0, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),
    		LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, TEXT("WindowClass"), NULL,
    	};
    	if (!RegisterClassEx(&wcex))
    		return MessageBox(NULL, L"Cannot register class !", L"Error", MB_ICONERROR | MB_OK);
    	int nX = (GetSystemMetrics(SM_CXSCREEN) - 1024) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - 800) / 2;
    	HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"),	WS_OVERLAPPEDWINDOW, nX, nY, 1024, 800, NULL, NULL, hInst, NULL);
    	if (!hWnd)
    		return MessageBox(NULL, L"Cannot create window !", L"Error", MB_ICONERROR | MB_OK);
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	UpdateWindow(hWnd);
    	MSG msg;
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return (int)msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hHTMLWindow;
    	switch (message)
    	{
    	case WM_CREATE:
    	{
    		hHTMLWindow = CreateWindow(_T("AtlAxWin"), _T("www.google.com"), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL, 10, 10, 700, 100, hWnd, (HMENU)1000, hInst, 0);
    		if (hHTMLWindow)
    		{
    			HRESULT hr = E_FAIL;
    			IUnknown* pUnknown = NULL;
    			IWebBrowser2 *pWebBrowser = NULL;
    			hr = pAtlAxGetControl(hHTMLWindow, &pUnknown);
    			if (SUCCEEDED(hr))
    			{
    				hr = pUnknown->QueryInterface(__uuidof(IWebBrowser2), (void**)&pWebBrowser);
    				if (SUCCEEDED(hr))
    				{
    					// Test
    					/*
    					BSTR bstrURL;
    					VARIANT_BOOL vb;
    					bstrURL = SysAllocString(L"www.msdn.com");
    					VARIANT var;
    					var.vt = VT_EMPTY;
    					pWebBrowser->Navigate(bstrURL, &var, &var, &var, &var);
    					SysFreeString(bstrURL);
    					*/
    					pWebBrowser->Release();
    				}			
    			}
    		}
    		return 0;
    	}
    	break;
    	case WM_SIZE:
    		MoveWindow(hHTMLWindow, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
    		break;
    	case WM_DESTROY:
    	{
    		PostQuitMessage(0);
    		return 0;
    	}
    	break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    

    • Proposed as answer by Baron Bi Wednesday, June 6, 2018 8:31 AM
    • Marked as answer by c_roy Tuesday, June 26, 2018 11:51 AM
    Thursday, May 31, 2018 8:29 PM

All replies

  • Hi,

    thanks for posting here.

    >>IWebBrowser2 pure c++ component session problem

    Please show us more information about your IWebBrowser2 and how do you create it.

    Have you tried to call IWebBrowser2::Navigate after you create the instance?

    Here is a sample project for you, you could refer to it.

    https://www.codeproject.com/tips/800585/embedded-webbrowser-iwebbrowser-in-pure-cplusplus

    Hope this could be help of you.

    Best Regards,

    Baron Bi


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 29, 2018 1:52 AM
  • Hi,

    thanks for the quick answer. :)

    I looked at the code you sent me from codeproject and basically i'm doing the same, i downloaded the code compiled it and it behaves the same no autocomplete no cookie sharing etc... 

    can you check this project and see if you can make it work as if open in IE?

    I made a project in MFC with simple webbrowser activeX control and it behaves the same.

    I did the same in c# and there it works just fine. one thing i saw is that the c# project is using the FullyTrust security attribute for the webbrowser activeX, how do i do that in mfc/com? 

    Regards,

    Roy

    Thursday, May 31, 2018 10:10 AM
  • I cannot reproduce your problem (IE 11, VS 2015, Windows 10)

    With minimal code, I have autocompletion and other things which seem normal :

    BSTR bstrURL;
    HRESULT hr;
    VARIANT_BOOL vb;
    IWebBrowser2 *pWebBrowser ;
    
    hr = CoInitialize(NULL);
    if ( SUCCEEDED(hr) )
    {
    	hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (LPVOID *)&pWebBrowser);
    	if ( SUCCEEDED(hr) )
    	{
    		/*vb = FALSE;
    		pWebBrowser->put_AddressBar(vb);*/
    		bstrURL = SysAllocString(L"www.google.com");
    		VARIANT var;
    		var.vt = VT_EMPTY;
    		pWebBrowser->Navigate(bstrURL, &var, &var, &var, &var);
    		SysFreeString(bstrURL);
    		vb = TRUE;
    		pWebBrowser->put_Visible(vb);
    	}
    	CoUninitialize();
    }

    Thursday, May 31, 2018 10:45 AM
  • Hi,

    you are right when initializing :

    hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (LPVOID *)&pWebBrowser);
    	

    then its all ok but this is not a webBrowser ctrl this is an actual ie window with iwebbrowser2 interface you can manipulate

    the initialize of the code you sent me is :

    HRESULT hr;
    hr = ::OleCreate(CLSID_WebBrowser,
    IID_IOleObject, OLERENDER_DRAW, 0, this, this,
    (void**)&oleObject);

    or even with a simple mfc app.

    Regards,

    Roy



    • Edited by c_roy Thursday, May 31, 2018 2:25 PM
    Thursday, May 31, 2018 2:24 PM
  • It is the same thing.

    If I use "AtlAxWin" as container for IWebBrowser2 (one of the known methods to embed easily the Web Browser), it still works fine on my configuration.

    Thursday, May 31, 2018 3:35 PM
  • Hi,

    if you can send me a pure c++ example that demonstrate the webbrowser on a window so i can check if it works for me i would really appreciate it :) 

    10x,

    Roy

    Thursday, May 31, 2018 7:38 PM
  • Minimal code with ATL =>

    #include <windows.h>
    #include <tchar.h>
    #include <exdisp.h> // IWebBrowser2
    
    HINSTANCE hInst;
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    HINSTANCE hATLDLL;
    typedef BOOL(__stdcall *PAAWI)(void);
    PAAWI pAtlAxWinInit;
    typedef HRESULT(__stdcall *PAAGC) (HWND hWnd, IUnknown** pUnknown);
    PAAGC pAtlAxGetControl;
    
    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
    	hInst = hInstance;
    	hATLDLL = LoadLibrary(_T("atl.dll"));
    	pAtlAxWinInit = (PAAWI)GetProcAddress(hATLDLL, "AtlAxWinInit");
    	if (pAtlAxWinInit)
    		pAtlAxWinInit();
    	pAtlAxGetControl = (PAAGC)GetProcAddress(hATLDLL, "AtlAxGetControl");
    	WNDCLASSEX wcex =
    	{
    		sizeof(WNDCLASSEX), 0, WndProc, 0, 0, hInst, LoadIcon(NULL, IDI_APPLICATION),
    		LoadCursor(NULL, IDC_ARROW), (HBRUSH)(COLOR_WINDOW + 1), NULL, TEXT("WindowClass"), NULL,
    	};
    	if (!RegisterClassEx(&wcex))
    		return MessageBox(NULL, L"Cannot register class !", L"Error", MB_ICONERROR | MB_OK);
    	int nX = (GetSystemMetrics(SM_CXSCREEN) - 1024) / 2, nY = (GetSystemMetrics(SM_CYSCREEN) - 800) / 2;
    	HWND hWnd = CreateWindowEx(0, wcex.lpszClassName, TEXT("Test"),	WS_OVERLAPPEDWINDOW, nX, nY, 1024, 800, NULL, NULL, hInst, NULL);
    	if (!hWnd)
    		return MessageBox(NULL, L"Cannot create window !", L"Error", MB_ICONERROR | MB_OK);
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	UpdateWindow(hWnd);
    	MSG msg;
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return (int)msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static HWND hHTMLWindow;
    	switch (message)
    	{
    	case WM_CREATE:
    	{
    		hHTMLWindow = CreateWindow(_T("AtlAxWin"), _T("www.google.com"), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL, 10, 10, 700, 100, hWnd, (HMENU)1000, hInst, 0);
    		if (hHTMLWindow)
    		{
    			HRESULT hr = E_FAIL;
    			IUnknown* pUnknown = NULL;
    			IWebBrowser2 *pWebBrowser = NULL;
    			hr = pAtlAxGetControl(hHTMLWindow, &pUnknown);
    			if (SUCCEEDED(hr))
    			{
    				hr = pUnknown->QueryInterface(__uuidof(IWebBrowser2), (void**)&pWebBrowser);
    				if (SUCCEEDED(hr))
    				{
    					// Test
    					/*
    					BSTR bstrURL;
    					VARIANT_BOOL vb;
    					bstrURL = SysAllocString(L"www.msdn.com");
    					VARIANT var;
    					var.vt = VT_EMPTY;
    					pWebBrowser->Navigate(bstrURL, &var, &var, &var, &var);
    					SysFreeString(bstrURL);
    					*/
    					pWebBrowser->Release();
    				}			
    			}
    		}
    		return 0;
    	}
    	break;
    	case WM_SIZE:
    		MoveWindow(hHTMLWindow, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
    		break;
    	case WM_DESTROY:
    	{
    		PostQuitMessage(0);
    		return 0;
    	}
    	break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    

    • Proposed as answer by Baron Bi Wednesday, June 6, 2018 8:31 AM
    • Marked as answer by c_roy Tuesday, June 26, 2018 11:51 AM
    Thursday, May 31, 2018 8:29 PM
  • Hi again,

    I compiled the code you gave me for example the auto login for gmail and Facebook works but auto complete not.

    in my c# test I type my mail and it give me a dropdown with options of my user name(also in the IE itself) -

    , but in the c++ project not.

    what else should be done for it to work in the c++

    Regards,

    Roy

    Thursday, June 7, 2018 6:49 AM
  • hi again,

    I also need access to the sessionStorage, the site i'm targeting uses that feature for auth. 

    Is there a c++ example for IHTMLStorage API getItem, setItem etc...

    10x,

    Roy

    Thursday, June 21, 2018 11:48 AM