Thanks Lance, but it doesn't work...
"As I understand it, the problem you running into occurs after the call to Navigate. When this is finished, a new instance of protected mode iexplore is started to handle the request. As a result, the spWebBrowser pointer is now defunct."
As it wrote in here I suppose to navigate and only then putVisible it...
"By the way, it's not necessary to try to bring the new browser window to the top of the z-order, as IE itself will bring itself to the top of the stack (to help prevent hidden activities)."
Well, it doesn't occur! the window stay in the background of the application. only in debug mode it appears normal. (Why is this happening !)
I tried the code you gave, but no results - the window continue to open behind my application window.
The only change I can see is - if i'm debugging - the new IE7 process is opened directly with HIGH integrity (process explorer), and the Protected Mode status says "NO"... if i'm running it non-debug it opens iexplorer.exe which runs ieuser.exe that runs iexplorer.exe with low integrity...
Here is the code (I changed it a bit - copied some missing parts to make it work on VS6...):
typedef struct _TOKEN_MANDATORY_LABEL {
SID_AND_ATTRIBUTES Label;
} TOKEN_MANDATORY_LABEL, *PTOKEN_MANDATORY_LABEL;
typedef WINADVAPI BOOL (WINAPI *CONVERTSTRINGSIDTOSID) (LPCSTR, PSID);
#define SE_GROUP_INTEGRITY (0x00000020L)
#define SE_GROUP_INTEGRITY_ENABLED (0x00000040L)
HRESULT SetThreadIntegrityLevelLow(HANDLE *phThread)
{
HANDLE hProcToken = NULL;
TOKEN_MANDATORY_LABEL TML = {0};
const TCHAR szIntegritySid[20] = _T("S-1-16-4096");
HANDLE hMICToken = NULL;
HRESULT hr = S_OK;
BOOL bOpenProcToken = FALSE;
BOOL bDuplicateToken = FALSE;
BOOL bConvertSid = FALSE;
BOOL bSetToken = FALSE;
BOOL bPILToken = FALSE;
bOpenProcToken = OpenProcessToken(GetCurrentProcess(),MAXIMUM_ALLOWED,&hProcToken);
if (bOpenProcToken)
{
bDuplicateToken = DuplicateTokenEx(hProcToken,
MAXIMUM_ALLOWED,
NULL,
SecurityImpersonation,
TokenImpersonation,
&hMICToken);
if (bDuplicateToken)
{
PSID pMICSid = NULL;
HINSTANCE hConvertDll = LoadLibrary(_T("Advapi32.dll"));
CONVERTSTRINGSIDTOSID pConvert = reinterpret_cast<CONVERTSTRINGSIDTOSID>(
GetProcAddress(hConvertDll, "ConvertStringSidToSidA"));
if(pConvert)
{
//bConvertSid = ConvertStringSidToSid(SDDL_ML_LOW, &pMICSid);
bConvertSid = pConvert(szIntegritySid, &pMICSid);
if (bConvertSid)
{
//Set Process IL to Low
TML.Label.Attributes = SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED;
TML.Label.Sid = pMICSid;
bPILToken = SetTokenInformation(hMICToken ,
(TOKEN_INFORMATION_CLASS)25,
&TML,
sizeof(TML) + GetLengthSid(pMICSid));
if (bPILToken)
{
bSetToken = SetThreadToken(phThread, hMICToken);
}
LocalFree(pMICSid);
}
CloseHandle(hMICToken);
}
}
CloseHandle(hProcToken);
}
if (!bOpenProcToken || !bDuplicateToken || !bConvertSid || !bSetToken)
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
return hr;
}
The code that opens IE window:
HRESULT hr = S_OK;
try
{
// Checking if we have internet connection to the url
CComPtr<IWebBrowser2> spWebBrowser;
HANDLE hThread = GetCurrentThread();
if (!ImpersonateSelf(SecurityImpersonation))
{
hr = HRESULT_FROM_WIN32(GetLastError());
}
hr = SetThreadIntegrityLevelLow(&hThread);
hr = spWebBrowser.CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_SERVER);
if(SUCCEEDED(hr) && !!spWebBrowser)
{
CComVariant var(0);
SetProperties(bstrProperties, spWebBrowser.p);
hr = spWebBrowser->Navigate(bstrFullUrl, &var, &var, &var, &var);
if(SUCCEEDED(hr))
{
HWND hWnd = NULL;
if(SUCCEEDED(spWebBrowser->get_HWND(reinterpret_cast<PLONG>(&hWnd))) &&::IsWindow(hWnd))
BringToFront(hWnd);
hr = spWebBrowser->put_Visible(VARIANT_TRUE);
}
}
}