I know there are a lot of articles about this on the internet, and I tried many of them. Although I can make my browser load a webpage on internet, somehow I can't manage to make it load HTML from memory.
Most of the time, the two methods below just don't have any visible effect; other times, they throw errors. Although other people say that "neither pPSI nor pHtmlDoc2 point to a valid object", I don't know if this is true. I have marked the lines that cause the error with a comment.
At the end of my question is the fully working code if you want to reproduce the problem.
Method 1
void WebBrowser::setHTML(const wchar_t *htmlSource) {
HRESULT hr;
IDispatch *pDispatch = 0;
IHTMLDocument2 *pHtmlDoc2 = 0;
IPersistStreamInit *pPSI = 0;
IStream *pStream = 0;
HGLOBAL hHTMLContent;
Navigate(L"about:blank");
hr = webBrowser2->get_Document(&pDispatch);
if (SUCCEEDED(hr) && pDispatch) hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDoc2);
//BSTR p = 0;
//if (SUCCEEDED(hr)) hr = pHtmlDoc2->get_readyState(&p);
if (SUCCEEDED(hr) && pHtmlDoc2) hr = pHtmlDoc2->QueryInterface(IID_IPersistStreamInit, (void **)&pPSI);
// allocate global memory to copy the HTML content to
hHTMLContent = ::GlobalAlloc(GMEM_MOVEABLE, (::wcslen(htmlSource) + 1) * sizeof(TCHAR));
if (hHTMLContent)
{
wchar_t * p_content(static_cast<wchar_t *>(GlobalLock(hHTMLContent)));
::wcscpy(p_content, htmlSource); // Debbug: p_content contains HTML string
GlobalUnlock(hHTMLContent);
// create a stream object based on the HTML content
if (SUCCEEDED(hr) && pPSI) hr = ::CreateStreamOnHGlobal(hHTMLContent, TRUE, &pStream);
{
hr = pPSI->InitNew(); //////////// <- Sometime throw error.
if (SUCCEEDED(hr)) hr = pPSI->Load(pStream);
}
}
if (pStream) pStream->Release();
if (pPSI) pPSI->Release();
if (pHtmlDoc2) pHtmlDoc2->Release();
if (pDispatch) pDispatch->Release();
}
The error:
Exception thrown at 0x51539FB1 (mshtml.dll) in Test.exe: 0xC0000005:
Access violation reading location 0x00000030
Method 2
This one is modified from this article on CodeProject. In that source, it works perfectly, but it doesn't work when I try to adapt to my code:
void WebBrowser::setHTML2(const wchar_t *htmlSource) {
HRESULT hr;
IDispatch *pDispatch = 0;
IHTMLDocument2 *pHtmlDoc2 = 0;
this->Navigate(L"about:blank");
hr = this->webBrowser2->get_Document(&pDispatch);
if (SUCCEEDED(hr) && pDispatch) hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDoc2);
if (SUCCEEDED(hr) && pHtmlDoc2)
{
BSTR bstr = SysAllocString(htmlSource);
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (psaStrings)
{
VARIANT *param;
hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
if (SUCCEEDED(hr) && param)
{
param->vt = VT_BSTR;
param->bstrVal = bstr;
//if (SUCCEEDED(hr)) hr = SafeArrayUnaccessData(psaStrings);
if (SUCCEEDED(hr))
{
hr = pHtmlDoc2->write(psaStrings); //////////// <- Sometime throw error.
}
}
SafeArrayDestroy(psaStrings);
}
}
if (pHtmlDoc2) pHtmlDoc2->Release();
if (pDispatch) pDispatch->Release();
}
The error:
Exception thrown at 0x51577F2E (mshtml.dll) in Test.exe: 0xC0000005:
Access violation reading location 0x000002C4
A possible third method, Loading HTML content from a Stream from Microsoft, suggests I load from the stream on a DWebBrowserEvents2::DocumentComplete event, which I have tried and failed to figure out how to implement.
Full Source Code
Below is full code. I create a new Win32 project and create/modify the following files:
Browser.h
#include <comdef.h>
#include <Exdisp.h>
#include <ExDispid.h>
#include <MsHTML.h>
#include <Mshtmhst.h>
#include <string>
#include <tchar.h>
#include <Windows.h>
using namespace std;
class WebBrowser :
public IOleClientSite,
public IOleInPlaceSite,
public IStorage
{
public:
WebBrowser(HWND hWndParent);
bool CreateBrowser();
void SetText(const wchar_t* t);
void setHTML(const wchar_t *htmlSource);
void setHTML2(const wchar_t *htmlSource);
RECT PixelToHiMetric(const RECT& _rc);
virtual void SetRect(const RECT& _rc);
// ----- Control methods -----
void GoBack();
void GoForward();
void Refresh();
void Navigate(wstring szUrl);
// ----- IUnknown -----
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,
void**ppvObject) override;
virtual ULONG STDMETHODCALLTYPE AddRef(void);
virtual ULONG STDMETHODCALLTYPE Release(void);
// ---------- IOleWindow ----------
virtual HRESULT STDMETHODCALLTYPE GetWindow(
__RPC__deref_out_opt HWND *phwnd) override;
virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(
BOOL fEnterMode) override;
// ---------- IOleInPlaceSite ----------
virtual HRESULT STDMETHODCALLTYPE CanInPlaceActivate(void) override;
virtual HRESULT STDMETHODCALLTYPE OnInPlaceActivate(void) override;
virtual HRESULT STDMETHODCALLTYPE OnUIActivate(void) override;
virtual HRESULT STDMETHODCALLTYPE GetWindowContext(
__RPC__deref_out_opt IOleInPlaceFrame **ppFrame,
__RPC__deref_out_opt IOleInPlaceUIWindow **ppDoc,
__RPC__out LPRECT lprcPosRect,
__RPC__out LPRECT lprcClipRect,
__RPC__inout LPOLEINPLACEFRAMEINFO lpFrameInfo) override;
virtual HRESULT STDMETHODCALLTYPE Scroll(
SIZE scrollExtant) override;
virtual HRESULT STDMETHODCALLTYPE OnUIDeactivate(
BOOL fUndoable) override;
virtual HWND GetControlWindow();
virtual HRESULT STDMETHODCALLTYPE OnInPlaceDeactivate(void) override;
virtual HRESULT STDMETHODCALLTYPE DiscardUndoState(void) override;
virtual HRESULT STDMETHODCALLTYPE DeactivateAndUndo(void) override;
virtual HRESULT STDMETHODCALLTYPE OnPosRectChange(
__RPC__in LPCRECT lprcPosRect) override;
// ---------- IOleClientSite ----------
virtual HRESULT STDMETHODCALLTYPE SaveObject(void) override;
virtual HRESULT STDMETHODCALLTYPE GetMoniker(
DWORD dwAssign,
DWORD dwWhichMoniker,
__RPC__deref_out_opt IMoniker **ppmk) override;
virtual HRESULT STDMETHODCALLTYPE GetContainer(
__RPC__deref_out_opt IOleContainer **ppContainer) override;
virtual HRESULT STDMETHODCALLTYPE ShowObject(void) override;
virtual HRESULT STDMETHODCALLTYPE OnShowWindow(
BOOL fShow) override;
virtual HRESULT STDMETHODCALLTYPE RequestNewObjectLayout(void) override;
// ----- IStorage -----
virtual HRESULT STDMETHODCALLTYPE CreateStream(
__RPC__in_string const OLECHAR *pwcsName,
DWORD grfMode,
DWORD reserved1,
DWORD reserved2,
__RPC__deref_out_opt IStream **ppstm) override;
virtual HRESULT STDMETHODCALLTYPE OpenStream(
const OLECHAR *pwcsName,
void *reserved1,
DWORD grfMode,
DWORD reserved2,
IStream **ppstm) override;
virtual HRESULT STDMETHODCALLTYPE CreateStorage(
__RPC__in_string const OLECHAR *pwcsName,
DWORD grfMode,
DWORD reserved1,
DWORD reserved2,
__RPC__deref_out_opt IStorage **ppstg) override;
virtual HRESULT STDMETHODCALLTYPE OpenStorage(
__RPC__in_opt_string const OLECHAR *pwcsName,
__RPC__in_opt IStorage *pstgPriority,
DWORD grfMode,
__RPC__deref_opt_in_opt SNB snbExclude,
DWORD reserved,
__RPC__deref_out_opt IStorage **ppstg) override;
virtual HRESULT STDMETHODCALLTYPE CopyTo(
DWORD ciidExclude,
const IID *rgiidExclude,
__RPC__in_opt SNB snbExclude,
IStorage *pstgDest) override;
virtual HRESULT STDMETHODCALLTYPE MoveElementTo(
__RPC__in_string const OLECHAR *pwcsName,
__RPC__in_opt IStorage *pstgDest,
__RPC__in_string const OLECHAR *pwcsNewName,
DWORD grfFlags) override;
virtual HRESULT STDMETHODCALLTYPE Commit(
DWORD grfCommitFlags) override;
virtual HRESULT STDMETHODCALLTYPE Revert(void) override;
virtual HRESULT STDMETHODCALLTYPE EnumElements(
DWORD reserved1,
void *reserved2,
DWORD reserved3,
IEnumSTATSTG **ppenum) override;
virtual HRESULT STDMETHODCALLTYPE DestroyElement(
__RPC__in_string const OLECHAR *pwcsName) override;
virtual HRESULT STDMETHODCALLTYPE RenameElement(
__RPC__in_string const OLECHAR *pwcsOldName,
__RPC__in_string const OLECHAR *pwcsNewName) override;
virtual HRESULT STDMETHODCALLTYPE SetElementTimes(
__RPC__in_opt_string const OLECHAR *pwcsName,
__RPC__in_opt const FILETIME *pctime,
__RPC__in_opt const FILETIME *patime,
__RPC__in_opt const FILETIME *pmtime) override;
virtual HRESULT STDMETHODCALLTYPE SetClass(
__RPC__in REFCLSID clsid) override;
virtual HRESULT STDMETHODCALLTYPE SetStateBits(
DWORD grfStateBits,
DWORD grfMask) override;
virtual HRESULT STDMETHODCALLTYPE Stat(
__RPC__out STATSTG *pstatstg,
DWORD grfStatFlag) override;
protected:
IOleObject* oleObject;
IOleInPlaceObject* oleInPlaceObject;
IWebBrowser2* webBrowser2;
LONG iComRefCount;
RECT rObject;
HWND hWndParent;
HWND hWndControl;
};
Browser.cpp
#include "stdafx.h"
#include "Browser.h"
namespace tkString
{
wchar_t* Format(const wchar_t* format, ...)
{
va_list args;
va_start(args, format);
wchar_t *w = NULL;
int len = _vsnwprintf(NULL, 0, format, args) + 1;
if (len > 0)
{
w = new wchar_t[len];
w[0] = 0;
_vsnwprintf_s(w, len, len, format, args);
}
va_end(args);
return w;
}
}
void WebBrowser::SetText(const wchar_t* t)
{
const wchar_t* html = L"<html><head><meta http-equiv='x-ua-compatible' content='IE=edge'></head><body><code>%ls</code></body></html>";
if (t)
{
wchar_t *w = tkString::Format(html, t);
setHTML(w);
//setHTML2(w);
delete[] w;
}
else this->Navigate(L"https://google.com.vn");
}
void WebBrowser::setHTML(const wchar_t *htmlSource) {
HRESULT hr;
IDispatch *pDispatch = 0;
IHTMLDocument2 *pHtmlDoc2 = 0;
IPersistStreamInit *pPSI = 0;
IStream *pStream = 0;
HGLOBAL hHTMLContent;
Navigate(L"about:blank");
hr = webBrowser2->get_Document(&pDispatch);
if (SUCCEEDED(hr) && pDispatch) hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDoc2);
//BSTR p = 0;
//if (SUCCEEDED(hr)) hr = pHtmlDoc2->get_readyState(&p);
if (SUCCEEDED(hr) && pHtmlDoc2) hr = pHtmlDoc2->QueryInterface(IID_IPersistStreamInit, (void **)&pPSI);
// allocate global memory to copy the HTML content to
hHTMLContent = ::GlobalAlloc(GMEM_MOVEABLE, (::wcslen(htmlSource) + 1) * sizeof(TCHAR));
if (hHTMLContent)
{
wchar_t * p_content(static_cast<wchar_t *>(GlobalLock(hHTMLContent)));
::wcscpy(p_content, htmlSource);
GlobalUnlock(hHTMLContent);
// create a stream object based on the HTML content
if (SUCCEEDED(hr) && pPSI) hr = ::CreateStreamOnHGlobal(hHTMLContent, TRUE, &pStream);
if (SUCCEEDED(hr) && pStream) hr = pPSI->InitNew();
if (SUCCEEDED(hr)) hr = pPSI->Load(pStream);
}
if (pStream) pStream->Release();
if (pPSI) pPSI->Release();
if (pHtmlDoc2) pHtmlDoc2->Release();
if (pDispatch) pDispatch->Release();
}
void WebBrowser::setHTML2(const wchar_t *htmlSource) {
HRESULT hr;
IDispatch *pDispatch = 0;
IHTMLDocument2 *pHtmlDoc2 = 0;
this->Navigate(L"about:blank");
hr = this->webBrowser2->get_Document(&pDispatch);
if (SUCCEEDED(hr) && pDispatch) hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDoc2);
if (SUCCEEDED(hr) && pHtmlDoc2)
{
BSTR bstr = SysAllocString(htmlSource);
SAFEARRAY *psaStrings = SafeArrayCreateVector(VT_VARIANT, 0, 1);
if (psaStrings)
{
VARIANT *param;
hr = SafeArrayAccessData(psaStrings, (LPVOID*)¶m);
if (SUCCEEDED(hr) && param)
{
param->vt = VT_BSTR;
param->bstrVal = bstr;
//if (SUCCEEDED(hr)) hr = SafeArrayUnaccessData(psaStrings);
if (SUCCEEDED(hr)) hr = pHtmlDoc2->write(psaStrings);
}
SafeArrayDestroy(psaStrings);
}
}
if (pHtmlDoc2) pHtmlDoc2->Release();
if (pDispatch) pDispatch->Release();
}
WebBrowser::WebBrowser(HWND _hWndParent)
{
iComRefCount = 0;
::SetRect(&rObject, 0, 0, 600, 400);
hWndParent = _hWndParent;
if (CreateBrowser() == FALSE)
{
return;
}
webBrowser2->put_Visible(TRUE);
ShowWindow(GetControlWindow(), SW_SHOW);
this->Navigate(_T("about:blank"));
}
bool WebBrowser::CreateBrowser()
{
HRESULT hr;
hr = ::OleCreate(CLSID_WebBrowser,
IID_IOleObject, OLERENDER_DRAW, 0, this, this,
(void**)&oleObject);
if (FAILED(hr))
{
MessageBox(NULL, _T("Cannot create oleObject CLSID_WebBrowser"),
_T("Error"),
MB_ICONERROR);
return FALSE;
}
hr = oleObject->SetClientSite(this);
hr = OleSetContainedObject(oleObject, TRUE);
RECT posRect;
::SetRect(&rObject, 0, 0, 600, 400);
hr = oleObject->DoVerb(OLEIVERB_INPLACEACTIVATE,
NULL, this, -1, hWndParent, &posRect);
if (FAILED(hr))
{
MessageBox(NULL, _T("oleObject->DoVerb() failed"),
_T("Error"),
MB_ICONERROR);
return FALSE;
}
hr = oleObject->QueryInterface(&webBrowser2);
if (FAILED(hr))
{
MessageBox(NULL, _T("oleObject->QueryInterface(&webBrowser2) failed"),
_T("Error"),
MB_ICONERROR);
return FALSE;
}
return TRUE;
}
RECT WebBrowser::PixelToHiMetric(const RECT& _rc)
{
static bool s_initialized = false;
static int s_pixelsPerInchX, s_pixelsPerInchY;
if (!s_initialized)
{
HDC hdc = ::GetDC(0);
s_pixelsPerInchX = ::GetDeviceCaps(hdc, LOGPIXELSX);
s_pixelsPerInchY = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(0, hdc);
s_initialized = true;
}
RECT rc;
rc.left = MulDiv(2540, _rc.left, s_pixelsPerInchX);
rc.top = MulDiv(2540, _rc.top, s_pixelsPerInchY);
rc.right = MulDiv(2540, _rc.right, s_pixelsPerInchX);
rc.bottom = MulDiv(2540, _rc.bottom, s_pixelsPerInchY);
return rc;
}
void WebBrowser::SetRect(const RECT& _rc)
{
rObject = _rc;
{
RECT hiMetricRect = PixelToHiMetric(rObject);
SIZEL sz;
sz.cx = hiMetricRect.right - hiMetricRect.left;
sz.cy = hiMetricRect.bottom - hiMetricRect.top;
oleObject->SetExtent(DVASPECT_CONTENT, &sz);
}
if (oleInPlaceObject != 0)
{
oleInPlaceObject->SetObjectRects(&rObject, &rObject);
}
}
// ----- Control methods -----
void WebBrowser::GoBack()
{
this->webBrowser2->GoBack();
}
void WebBrowser::GoForward()
{
this->webBrowser2->GoForward();
}
void WebBrowser::Refresh()
{
this->webBrowser2->Refresh();
}
void WebBrowser::Navigate(wstring szUrl)
{
bstr_t url(szUrl.c_str());
variant_t flags(0x02u); //navNoHistory
this->webBrowser2->Navigate(url, &flags, 0, 0, 0);
}
// ----- IUnknown -----
HRESULT STDMETHODCALLTYPE WebBrowser::QueryInterface(REFIID riid,
void**ppvObject)
{
if (riid == __uuidof(IUnknown))
{
(*ppvObject) = static_cast<IOleClientSite*>(this);
}
else if (riid == __uuidof(IOleInPlaceSite))
{
(*ppvObject) = static_cast<IOleInPlaceSite*>(this);
}
else
{
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
ULONG STDMETHODCALLTYPE WebBrowser::AddRef(void)
{
iComRefCount++;
return iComRefCount;
}
ULONG STDMETHODCALLTYPE WebBrowser::Release(void)
{
iComRefCount--;
return iComRefCount;
}
// ---------- IOleWindow ----------
HRESULT STDMETHODCALLTYPE WebBrowser::GetWindow(
__RPC__deref_out_opt HWND *phwnd)
{
(*phwnd) = hWndParent;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::ContextSensitiveHelp(
BOOL fEnterMode)
{
return E_NOTIMPL;
}
// ---------- IOleInPlaceSite ----------
HRESULT STDMETHODCALLTYPE WebBrowser::CanInPlaceActivate(void)
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OnInPlaceActivate(void)
{
OleLockRunning(oleObject, TRUE, FALSE);
oleObject->QueryInterface(&oleInPlaceObject);
oleInPlaceObject->SetObjectRects(&rObject, &rObject);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OnUIActivate(void)
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::GetWindowContext(
__RPC__deref_out_opt IOleInPlaceFrame **ppFrame,
__RPC__deref_out_opt IOleInPlaceUIWindow **ppDoc,
__RPC__out LPRECT lprcPosRect,
__RPC__out LPRECT lprcClipRect,
__RPC__inout LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
HWND hwnd = hWndParent;
(*ppFrame) = NULL;
(*ppDoc) = NULL;
(*lprcPosRect).left = rObject.left;
(*lprcPosRect).top = rObject.top;
(*lprcPosRect).right = rObject.right;
(*lprcPosRect).bottom = rObject.bottom;
*lprcClipRect = *lprcPosRect;
lpFrameInfo->fMDIApp = false;
lpFrameInfo->hwndFrame = hwnd;
lpFrameInfo->haccel = NULL;
lpFrameInfo->cAccelEntries = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::Scroll(
SIZE scrollExtant)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OnUIDeactivate(
BOOL fUndoable)
{
return S_OK;
}
HWND WebBrowser::GetControlWindow()
{
if (hWndControl != 0)
return hWndControl;
if (oleInPlaceObject == 0)
return 0;
oleInPlaceObject->GetWindow(&hWndControl);
return hWndControl;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OnInPlaceDeactivate(void)
{
hWndControl = 0;
oleInPlaceObject = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::DiscardUndoState(void)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::DeactivateAndUndo(void)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OnPosRectChange(
__RPC__in LPCRECT lprcPosRect)
{
return E_NOTIMPL;
}
// ---------- IOleClientSite ----------
HRESULT STDMETHODCALLTYPE WebBrowser::SaveObject(void)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::GetMoniker(
DWORD dwAssign,
DWORD dwWhichMoniker,
__RPC__deref_out_opt IMoniker **ppmk)
{
if ((dwAssign == OLEGETMONIKER_ONLYIFTHERE) &&
(dwWhichMoniker == OLEWHICHMK_CONTAINER))
return E_FAIL;
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::GetContainer(
__RPC__deref_out_opt IOleContainer **ppContainer)
{
return E_NOINTERFACE;
}
HRESULT STDMETHODCALLTYPE WebBrowser::ShowObject(void)
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OnShowWindow(
BOOL fShow)
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::RequestNewObjectLayout(void)
{
return E_NOTIMPL;
}
// ----- IStorage -----
HRESULT STDMETHODCALLTYPE WebBrowser::CreateStream(
__RPC__in_string const OLECHAR *pwcsName,
DWORD grfMode,
DWORD reserved1,
DWORD reserved2,
__RPC__deref_out_opt IStream **ppstm)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OpenStream(
const OLECHAR *pwcsName,
void *reserved1,
DWORD grfMode,
DWORD reserved2,
IStream **ppstm)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::CreateStorage(
__RPC__in_string const OLECHAR *pwcsName,
DWORD grfMode,
DWORD reserved1,
DWORD reserved2,
__RPC__deref_out_opt IStorage **ppstg)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::OpenStorage(
__RPC__in_opt_string const OLECHAR *pwcsName,
__RPC__in_opt IStorage *pstgPriority,
DWORD grfMode,
__RPC__deref_opt_in_opt SNB snbExclude,
DWORD reserved,
__RPC__deref_out_opt IStorage **ppstg)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::CopyTo(
DWORD ciidExclude,
const IID *rgiidExclude,
__RPC__in_opt SNB snbExclude,
IStorage *pstgDest)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::MoveElementTo(
__RPC__in_string const OLECHAR *pwcsName,
__RPC__in_opt IStorage *pstgDest,
__RPC__in_string const OLECHAR *pwcsNewName,
DWORD grfFlags)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::Commit(
DWORD grfCommitFlags)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::Revert(void)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::EnumElements(
DWORD reserved1,
void *reserved2,
DWORD reserved3,
IEnumSTATSTG **ppenum)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::DestroyElement(
__RPC__in_string const OLECHAR *pwcsName)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::RenameElement(
__RPC__in_string const OLECHAR *pwcsOldName,
__RPC__in_string const OLECHAR *pwcsNewName)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::SetElementTimes(
__RPC__in_opt_string const OLECHAR *pwcsName,
__RPC__in_opt const FILETIME *pctime,
__RPC__in_opt const FILETIME *patime,
__RPC__in_opt const FILETIME *pmtime)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::SetClass(
__RPC__in REFCLSID clsid)
{
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::SetStateBits(
DWORD grfStateBits,
DWORD grfMask)
{
return E_NOTIMPL;
}
HRESULT STDMETHODCALLTYPE WebBrowser::Stat(
__RPC__out STATSTG *pstatstg,
DWORD grfStatFlag)
{
return E_NOTIMPL;
}
Win32Project1.cpp
#include "stdafx.h"
#include "Win32Project1.h"
#include "Browser.h"
HWND mainHWND;
WebBrowser* myBrowser = NULL;
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
OleInitialize(NULL);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
myBrowser = new WebBrowser(mainHWND);
//myBrowser->Navigate(L"https://google.com.vn");
myBrowser->SetText(L"Hello!");
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
mainHWND = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!mainHWND)
{
return FALSE;
}
ShowWindow(mainHWND, nCmdShow);
UpdateWindow(mainHWND);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code that uses hdc here...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
// Removed
return (INT_PTR)FALSE;
}
Other source files created by Visual Studio remain untouched.
Question
What problem causes my program to fail to load the HTML string, and how do I fix it? Or do you have any working example (no MFC or ATL) to load an HTML string to the WebBrowser?
Note: I know I can write the HTML string to hard drive and pass the file path to the browser, but I don't want the hard disk involved at all.
Note 2: To win the bounty, please give a complete example of working code, using as much of my code as possible. The browser should load HTML string successfully. Even when it's loading an online web page, it should stop to process this new command.
Navigate(L"about:blank");
The posted source code does not demonstrate the problem well. It shows no errors, but also no "Hello". Only by omitting the Navigate() call does it get close to failing on nullptr errors. The code is simply not finished, it is missing the required plumbing to capture the DocumentComplete event. Only after this event is fired can the code inside WebBrowser::setHtml() work.
Have the WebBrowser class implement the IDispatch and IUnknown interfaces:
class WebBrowser :
public IDispatch,
public IUnknown,
public IOleClientSite,
public IOleInPlaceSite,
public IStorage
{
private:
HRESULT OnCompleted(DISPPARAMS* args);
wchar_t* htmlSource;
IConnectionPoint* callback;
DWORD eventCookie;
// etc...
};
Note how the htmlSource variable is now a class member, the string needs to be stored until the DocumentComplete event fires.
We need to implement IDispatch. That's pretty easy to do, I put the code inline:
// ---------- IDispatch ----------
HRESULT GetTypeInfoCount(UINT *pctinfo) { return E_FAIL; }
HRESULT GetTypeInfo(UINT, LCID, ITypeInfo **) { return E_FAIL; }
HRESULT GetIDsOfNames(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_FAIL; }
HRESULT Invoke(DISPID dispIdMember, REFIID, LCID, WORD,
DISPPARAMS *pDispParams, VARIANT *pVarResult,
EXCEPINFO*, UINT*) {
if (dispIdMember == DISPID_DOCUMENTCOMPLETE) return OnCompleted(pDispParams);
else return S_OK;
}
QueryInterface() needs to be tweaked:
HRESULT STDMETHODCALLTYPE WebBrowser::QueryInterface(REFIID riid, void**ppvObject)
{
if (riid == __uuidof(IUnknown)) *ppvObject = static_cast<IUnknown*>(this);
else if (riid == __uuidof(IDispatch)) *ppvObject = static_cast<IDispatch*>(this);
else if (riid == __uuidof(IOleClientSite)) *ppvObject = static_cast<IOleClientSite*>(this);
else if (riid == __uuidof(IOleInPlaceSite)) *ppvObject = static_cast<IOleInPlaceSite*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
The constructor needs to subscribe the event interface:
WebBrowser::WebBrowser(HWND _hWndParent)
{
//...
// appended:
htmlSource = nullptr;
IConnectionPointContainer* container = nullptr;
webBrowser2->QueryInterface(IID_IConnectionPointContainer, (void**)&container);
container->FindConnectionPoint(__uuidof(DWebBrowserEvents2), &callback);
IUnknown* punk = nullptr;
this->QueryInterface(IID_IUnknown, (void**)&punk);
callback->Advise(punk, &eventCookie);
punk->Release();
container->Release();
}
The setText() function becomes very simple, we're not going to worry about setHtml() at all. We'll just set the htmlSource member so we know what to do when the DocumentComplete event fires:
void WebBrowser::SetText(const wchar_t* t)
{
const wchar_t* html = L"<html><head><meta http-equiv='x-ua-compatible' content='IE=edge'></head><body><code>%ls</code></body></html>";
if (htmlSource) delete[] htmlSource;
htmlSource = tkString::Format(html, t);
this->Navigate(L"about::blank");
}
And finally the added function that runs when the DocumentComplete event fires. Most of the code got moved from setHtml:
HRESULT WebBrowser::OnCompleted(DISPPARAMS* args) {
HRESULT hr;
IDispatch *pDispatch = 0;
IHTMLDocument2 *pHtmlDoc2 = 0;
IPersistStreamInit *pPSI = 0;
IStream *pStream = 0;
HGLOBAL hHTMLContent;
if (!htmlSource) return S_OK;
hr = webBrowser2->get_Document(&pDispatch);
if (SUCCEEDED(hr) && pDispatch) hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDoc2);
if (SUCCEEDED(hr) && pHtmlDoc2) hr = pHtmlDoc2->QueryInterface(IID_IPersistStreamInit, (void **)&pPSI);
// allocate global memory to copy the HTML content to
hHTMLContent = ::GlobalAlloc(GMEM_MOVEABLE, (::wcslen(htmlSource) + 1) * sizeof(TCHAR));
if (hHTMLContent)
{
wchar_t * p_content(static_cast<wchar_t *>(GlobalLock(hHTMLContent)));
::wcscpy(p_content, htmlSource);
GlobalUnlock(hHTMLContent);
// create a stream object based on the HTML content
if (SUCCEEDED(hr) && pPSI) hr = ::CreateStreamOnHGlobal(hHTMLContent, TRUE, &pStream);
if (SUCCEEDED(hr) && pStream) hr = pPSI->InitNew();
if (SUCCEEDED(hr)) hr = pPSI->Load(pStream);
}
if (pStream) pStream->Release();
if (pPSI) pPSI->Release();
if (pHtmlDoc2) pHtmlDoc2->Release();
if (pDispatch) pDispatch->Release();
delete[] htmlSource;
htmlSource = nullptr;
return S_OK;
}
I'll leave proper cleanup as a // todo. Running this code now produces the desired outcome:
Based on Hans Passant's answer, bellow is the full working code, for anyone want to test.
Create new Win32 project named "Win32Project1" and add/edit following files:
WebBrowser.h
#pragma once
#include <comdef.h>
#include <Exdisp.h>
#include <ExDispid.h>
#include <MsHTML.h>
#include <Mshtmhst.h>
#include <string>
#include <tchar.h>
#include <Windows.h>
class WebBrowser :
public IDispatch,
public IOleClientSite,
public IOleInPlaceSite,
public IStorage
{
public:
WebBrowser(HWND hWndParent);
void SetText(const wchar_t* t);
void setHTML(const wchar_t *htmlText, bool shouldWrapinBODYtag = true);
// ----- Create Browser -----
HRESULT RegisterGIWebBrowser();
HRESULT GetGIWebBrowser(IWebBrowser2** pwb);
bool CreateBrowser();
// ----- For Resize Window -----
RECT PixelToHiMetric(const RECT& _rc);
virtual void SetRect(const RECT& _rc);
virtual HWND GetControlWindow();
// ----- Control methods -----
void GoBack();
void GoForward();
void Refresh();
void Navigate(const wchar_t *szUrl);
// ----- IUnknown -----
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void**ppvObject) override;
virtual ULONG STDMETHODCALLTYPE AddRef(void);
virtual ULONG STDMETHODCALLTYPE Release(void);
// ---------- IOleWindow ----------
virtual HRESULT STDMETHODCALLTYPE GetWindow(__RPC__deref_out_opt HWND *phwnd) override;
virtual HRESULT STDMETHODCALLTYPE ContextSensitiveHelp(BOOL fEnterMode) override;
// ---------- IOleInPlaceSite ----------
virtual HRESULT STDMETHODCALLTYPE WebBrowser::CanInPlaceActivate(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OnUIActivate(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OnInPlaceActivate(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::GetWindowContext(__RPC__deref_out_opt IOleInPlaceFrame **ppFrame, __RPC__deref_out_opt IOleInPlaceUIWindow **ppDoc, __RPC__out LPRECT lprcPosRect, __RPC__out LPRECT lprcClipRect, __RPC__inout LPOLEINPLACEFRAMEINFO lpFrameInfo) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::Scroll(SIZE scrollExtant) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OnUIDeactivate(BOOL fUndoable) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::DiscardUndoState(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::DeactivateAndUndo(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OnPosRectChange(__RPC__in LPCRECT lprcPosRect) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OnInPlaceDeactivate(void) override;
// ---------- IOleClientSite ----------
virtual HRESULT STDMETHODCALLTYPE WebBrowser::SaveObject(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::GetContainer(__RPC__deref_out_opt IOleContainer **ppContainer) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::ShowObject(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OnShowWindow(BOOL fShow) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::RequestNewObjectLayout(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, __RPC__deref_out_opt IMoniker **ppmk) override;
// ----- IStorage -----
virtual HRESULT STDMETHODCALLTYPE WebBrowser::CreateStream(__RPC__in_string const OLECHAR *pwcsName, DWORD grfMode, DWORD reserved1, DWORD reserved2, __RPC__deref_out_opt IStream **ppstm) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OpenStream(const OLECHAR *pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream **ppstm) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::CreateStorage(__RPC__in_string const OLECHAR *pwcsName, DWORD grfMode, DWORD reserved1, DWORD reserved2, __RPC__deref_out_opt IStorage **ppstg) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::OpenStorage(__RPC__in_opt_string const OLECHAR *pwcsName, __RPC__in_opt IStorage *pstgPriority, DWORD grfMode, __RPC__deref_opt_in_opt SNB snbExclude, DWORD reserved, __RPC__deref_out_opt IStorage **ppstg) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::CopyTo(DWORD ciidExclude, const IID *rgiidExclude, __RPC__in_opt SNB snbExclude, IStorage *pstgDest) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::MoveElementTo(__RPC__in_string const OLECHAR *pwcsName, __RPC__in_opt IStorage *pstgDest, __RPC__in_string const OLECHAR *pwcsNewName, DWORD grfFlags) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::Commit(DWORD grfCommitFlags) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::Revert(void) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::EnumElements(DWORD reserved1, void *reserved2, DWORD reserved3, IEnumSTATSTG **ppenum) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::DestroyElement(__RPC__in_string const OLECHAR *pwcsName) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::RenameElement(__RPC__in_string const OLECHAR *pwcsOldName, __RPC__in_string const OLECHAR *pwcsNewName) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::SetElementTimes(__RPC__in_opt_string const OLECHAR *pwcsName, __RPC__in_opt const FILETIME *pctime, __RPC__in_opt const FILETIME *patime, __RPC__in_opt const FILETIME *pmtime) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::SetClass(__RPC__in REFCLSID clsid) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::SetStateBits(DWORD grfStateBits, DWORD grfMask) override;
virtual HRESULT STDMETHODCALLTYPE WebBrowser::Stat(__RPC__out STATSTG *pstatstg, DWORD grfStatFlag) override;
// ---------- IDispatch ----------
virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount(__RPC__out UINT *pctinfo) override;
virtual HRESULT STDMETHODCALLTYPE GetTypeInfo(UINT, LCID, __RPC__deref_out_opt ITypeInfo **) override;
virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames(__RPC__in REFIID riid, __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames, __RPC__in_range(0, 16384) UINT cNames, LCID lcid, __RPC__out_ecount_full(cNames) DISPID *rgDispId) override;
virtual HRESULT STDMETHODCALLTYPE Invoke(_In_ DISPID dispIdMember, _In_ REFIID, _In_ LCID, _In_ WORD, _In_ DISPPARAMS *pDispParams, _Out_opt_ VARIANT *pVarResult, _Out_opt_ EXCEPINFO*, _Out_opt_ UINT*) override;
private:
IOleObject* oleObject;
IOleInPlaceObject* oleInPlaceObject;
IWebBrowser2* webBrowser2;
LONG iComRefCount;
RECT rObject;
HWND hWndParent;
HWND hWndControl;
HRESULT OnCompleted(DISPPARAMS* args);
wchar_t* htmlSource;
IConnectionPoint* callback;
DWORD eventCookie;
DWORD dwGIWebBrowserCookie;
bool isFullyCreated;
};
WebBrowser.cpp
#include "stdafx.h"
#include "WebBrowser.h"
namespace tkString
{
wchar_t* Format(const wchar_t* format, ...)
{
va_list args;
va_start(args, format);
wchar_t* w = NULL;
int len = _vscwprintf(format, args) + 1;
if (len > 1)
{
w = new wchar_t[len];
w[0] = 0;
_vsnwprintf_s(w, len, len, format, args);
}
va_end(args);
return w;
}
}
void WebBrowser::SetText(const wchar_t* t)
{
if (isFullyCreated)
{
// TODO: escape text
const wchar_t* html = L"<html><head><meta http-equiv='x-ua-compatible' content='IE=edge'></head><body><code>%ls</code></body></html>";
if (htmlSource) delete[] htmlSource;
htmlSource = tkString::Format(html, t);
this->Navigate(L"about:blank");
}
else {}
}
void WebBrowser::setHTML(const wchar_t* htmlText, bool shouldWrapinBODYtag)
{
if (isFullyCreated)
{
if (htmlSource) delete[] htmlSource;
htmlSource = NULL;
if (shouldWrapinBODYtag)
{
const wchar_t* html = L"<html><head><meta http-equiv='x-ua-compatible' content='IE=edge'></head><body>%ls</body></html>";
htmlSource = tkString::Format(html, htmlText);
}
else
{
if (htmlText)
{
size_t len = wcslen(htmlText) + 1;
htmlSource = new wchar_t[len];
wmemcpy(htmlSource, htmlText, len);
}
}
this->Navigate(L"about:blank");
}
else {}
}
HRESULT WebBrowser::OnCompleted(DISPPARAMS* args)
{
HRESULT hr;
IDispatch *pDispatch = 0;
IHTMLDocument2 *pHtmlDoc2 = 0;
IPersistStreamInit *pPSI = 0;
IStream *pStream = 0;
HGLOBAL hHTMLContent;
if (!htmlSource) return S_OK;
hr = webBrowser2->get_Document(&pDispatch);
if (SUCCEEDED(hr) && pDispatch) hr = pDispatch->QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDoc2);
if (SUCCEEDED(hr) && pHtmlDoc2) hr = pHtmlDoc2->QueryInterface(IID_IPersistStreamInit, (void **)&pPSI);
// allocate global memory to copy the HTML content to
size_t len = wcslen(htmlSource) + 1;
hHTMLContent = ::GlobalAlloc(GMEM_MOVEABLE, len * sizeof(wchar_t));
if (hHTMLContent)
{
wchar_t * p_content(static_cast<wchar_t *>(GlobalLock(hHTMLContent)));
wmemcpy_s(p_content, len, htmlSource, len);
GlobalUnlock(hHTMLContent);
// create a stream object based on the HTML content
if (SUCCEEDED(hr) && pPSI) hr = ::CreateStreamOnHGlobal(hHTMLContent, TRUE, &pStream);
if (SUCCEEDED(hr) && pStream) hr = pPSI->InitNew();
if (SUCCEEDED(hr)) hr = pPSI->Load(pStream);
}
if (pStream) pStream->Release();
if (pPSI) pPSI->Release();
if (pHtmlDoc2) pHtmlDoc2->Release();
if (pDispatch) pDispatch->Release();
delete[] htmlSource;
htmlSource = nullptr;
return S_OK;
}
// ----- Control methods -----
void WebBrowser::Navigate(const wchar_t *szUrl)
{
HRESULT hr = E_FAIL;
IWebBrowser2 *pwb = NULL;
hr = GetGIWebBrowser(&pwb);
if (SUCCEEDED(hr) && pwb)
{
bstr_t url(szUrl);
variant_t flags(0x02u); //navNoHistory
pwb->Navigate(url, &flags, 0, 0, 0);
}
else { /* Handle error */ }
}
void WebBrowser::GoBack() { this->webBrowser2->GoBack(); }
void WebBrowser::GoForward() { this->webBrowser2->GoForward(); }
void WebBrowser::Refresh() { this->webBrowser2->Refresh(); }
WebBrowser::WebBrowser(HWND _hWndParent) :
isFullyCreated(false)
{
HRESULT hr = E_FAIL;
htmlSource = nullptr;
IConnectionPointContainer* container = nullptr;
IUnknown* punk = nullptr;
iComRefCount = 0;
::SetRect(&rObject, 0, 0, 300, 300);
hWndParent = _hWndParent;
if (CreateBrowser())
{
this->Navigate(L"about:blank");
hr = RegisterGIWebBrowser();
if (SUCCEEDED(hr))
{
webBrowser2->put_Visible(TRUE);
ShowWindow(GetControlWindow(), SW_SHOW);
hr = webBrowser2->QueryInterface(IID_IConnectionPointContainer, (void**)&container);
if (SUCCEEDED(hr) && container) hr = container->FindConnectionPoint(__uuidof(DWebBrowserEvents2), &callback);
if (SUCCEEDED(hr) && container) hr = this->QueryInterface(IID_IUnknown, (void**)&punk);
if (SUCCEEDED(hr) && container) hr = callback->Advise(punk, &eventCookie);
if (SUCCEEDED(hr) && eventCookie) isFullyCreated = true;
if (punk) punk->Release();
if (container) container->Release();
}
}
}
bool WebBrowser::CreateBrowser()
{
HRESULT hr;
hr = ::OleCreate(CLSID_WebBrowser, IID_IOleObject, OLERENDER_DRAW, 0, this, this, (void**)&oleObject);
if (SUCCEEDED(hr) && oleObject)
{
if (SUCCEEDED(hr)) hr = oleObject->SetClientSite(this);
if (SUCCEEDED(hr)) hr = OleSetContainedObject(oleObject, TRUE);
if (SUCCEEDED(hr))
{
RECT posRect;
::SetRect(&posRect, -300, -300, 300, 300);
hr = oleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, this, -1, hWndParent, &posRect);
}
hr = oleObject->QueryInterface(&webBrowser2);
}
if (FAILED(hr) || !webBrowser2)
{
MessageBox(NULL, L"Create Browser failed!", L"Error", MB_ICONERROR);
return false;
}
return true;
}
RECT WebBrowser::PixelToHiMetric(const RECT& _rc)
{
static bool s_initialized = false;
static int s_pixelsPerInchX, s_pixelsPerInchY;
if (!s_initialized)
{
HDC hdc = ::GetDC(0);
s_pixelsPerInchX = ::GetDeviceCaps(hdc, LOGPIXELSX);
s_pixelsPerInchY = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(0, hdc);
s_initialized = true;
}
RECT rc;
rc.left = MulDiv(2540, _rc.left, s_pixelsPerInchX);
rc.top = MulDiv(2540, _rc.top, s_pixelsPerInchY);
rc.right = MulDiv(2540, _rc.right, s_pixelsPerInchX);
rc.bottom = MulDiv(2540, _rc.bottom, s_pixelsPerInchY);
return rc;
}
void WebBrowser::SetRect(const RECT& _rc)
{
rObject = _rc;
{
RECT hiMetricRect = PixelToHiMetric(rObject);
SIZEL sz;
sz.cx = hiMetricRect.right - hiMetricRect.left;
sz.cy = hiMetricRect.bottom - hiMetricRect.top;
oleObject->SetExtent(DVASPECT_CONTENT, &sz);
}
if (oleInPlaceObject != 0)
{
oleInPlaceObject->SetObjectRects(&rObject, &rObject);
}
}
HWND WebBrowser::GetControlWindow() { if (!hWndControl && oleInPlaceObject) oleInPlaceObject->GetWindow(&hWndControl); return hWndControl; }
// Register IWebBrowser2 instance for use in threads
HRESULT WebBrowser::RegisterGIWebBrowser()
{
HRESULT hr = E_FAIL;
IUnknown* pIUnknown = NULL;
IGlobalInterfaceTable* pIGlobalInterfaceTable = NULL;
dwGIWebBrowserCookie = 0;
hr = ::CoCreateInstance(CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, IID_IGlobalInterfaceTable, (void **)&pIGlobalInterfaceTable);
if (SUCCEEDED(hr) && pIGlobalInterfaceTable) hr = webBrowser2->QueryInterface(IID_IUnknown, (void**)&pIUnknown);
if (SUCCEEDED(hr) && pIUnknown) hr = pIGlobalInterfaceTable->RegisterInterfaceInGlobal(pIUnknown, __uuidof(IWebBrowser2), &dwGIWebBrowserCookie);
if (pIUnknown) pIUnknown->Release();
if (pIGlobalInterfaceTable) pIGlobalInterfaceTable->Release();
if (SUCCEEDED(hr) && dwGIWebBrowserCookie) return hr;
return E_FAIL;
}
// Get IWebBrowser2 instance from threads
HRESULT WebBrowser::GetGIWebBrowser(IWebBrowser2** pwb)
{
HRESULT hr = E_FAIL;
IGlobalInterfaceTable* pIGlobalInterfaceTable = NULL;
*pwb = NULL;
if (dwGIWebBrowserCookie) hr = ::CoCreateInstance(CLSID_StdGlobalInterfaceTable, NULL, CLSCTX_INPROC_SERVER, IID_IGlobalInterfaceTable, (void **)&pIGlobalInterfaceTable);
if (SUCCEEDED(hr) && pIGlobalInterfaceTable) hr = pIGlobalInterfaceTable->GetInterfaceFromGlobal(dwGIWebBrowserCookie, __uuidof(IWebBrowser2), (void **)pwb);
if (pIGlobalInterfaceTable) pIGlobalInterfaceTable->Release();
return hr;
}
// ----- IUnknown -----
ULONG STDMETHODCALLTYPE WebBrowser::AddRef(void) { return ++iComRefCount; }
ULONG STDMETHODCALLTYPE WebBrowser::Release(void) { return --iComRefCount; }
HRESULT STDMETHODCALLTYPE WebBrowser::QueryInterface(REFIID riid, void**ppvObject)
{
if (riid == __uuidof(IUnknown)) *ppvObject = static_cast<IDispatch*>(this);
else if (riid == __uuidof(IDispatch)) *ppvObject = static_cast<IDispatch*>(this);
else if (riid == __uuidof(IOleClientSite)) *ppvObject = static_cast<IOleClientSite*>(this);
else if (riid == __uuidof(IOleInPlaceSite)) *ppvObject = static_cast<IOleInPlaceSite*>(this);
else
return E_NOINTERFACE;
AddRef();
return S_OK;
}
// ---------- IOleInPlaceSite ----------
HRESULT STDMETHODCALLTYPE WebBrowser::CanInPlaceActivate(void){ return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::OnUIActivate(void){ return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::OnInPlaceActivate(void)
{
OleLockRunning(oleObject, TRUE, FALSE);
oleObject->QueryInterface(&oleInPlaceObject);
oleInPlaceObject->SetObjectRects(&rObject, &rObject);
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::GetWindowContext(__RPC__deref_out_opt IOleInPlaceFrame **ppFrame, __RPC__deref_out_opt IOleInPlaceUIWindow **ppDoc, __RPC__out LPRECT lprcPosRect, __RPC__out LPRECT lprcClipRect, __RPC__inout LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
HWND hwnd = hWndParent;
(*ppFrame) = NULL;
(*ppDoc) = NULL;
(*lprcPosRect).left = rObject.left;
(*lprcPosRect).top = rObject.top;
(*lprcPosRect).right = rObject.right;
(*lprcPosRect).bottom = rObject.bottom;
*lprcClipRect = *lprcPosRect;
lpFrameInfo->fMDIApp = false;
lpFrameInfo->hwndFrame = hwnd;
lpFrameInfo->haccel = NULL;
lpFrameInfo->cAccelEntries = 0;
return S_OK;
}
HRESULT STDMETHODCALLTYPE WebBrowser::Scroll(SIZE scrollExtant) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::OnUIDeactivate(BOOL fUndoable) { return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::DiscardUndoState(void) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::DeactivateAndUndo(void) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::OnPosRectChange(__RPC__in LPCRECT lprcPosRect) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::OnInPlaceDeactivate(void) { hWndControl = 0; oleInPlaceObject = 0; return S_OK; }
// ---------- IDispatch ----------
HRESULT WebBrowser::GetTypeInfoCount(__RPC__out UINT *pctinfo) { return E_FAIL; }
HRESULT WebBrowser::GetTypeInfo(UINT, LCID, __RPC__deref_out_opt ITypeInfo **) { return E_FAIL; }
HRESULT WebBrowser::GetIDsOfNames(__RPC__in REFIID riid, __RPC__in_ecount_full(cNames) LPOLESTR *rgszNames, __RPC__in_range(0, 16384) UINT cNames, LCID lcid, __RPC__out_ecount_full(cNames) DISPID *rgDispId) { return E_FAIL; }
HRESULT STDMETHODCALLTYPE WebBrowser::Invoke(_In_ DISPID dispIdMember, _In_ REFIID, _In_ LCID, _In_ WORD, _In_ DISPPARAMS *pDispParams, _Out_opt_ VARIANT *pVarResult, _Out_opt_ EXCEPINFO*, _Out_opt_ UINT*)
{
if (dispIdMember == DISPID_DOCUMENTCOMPLETE) return OnCompleted(pDispParams);
else return S_OK;
}
// ---------- IOleWindow ----------
HRESULT STDMETHODCALLTYPE WebBrowser::GetWindow(__RPC__deref_out_opt HWND *phwnd) { (*phwnd) = hWndParent; return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::ContextSensitiveHelp(BOOL fEnterMode) { return E_NOTIMPL; }
// ---------- IOleClientSite ----------
HRESULT STDMETHODCALLTYPE WebBrowser::SaveObject(void) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::GetContainer(__RPC__deref_out_opt IOleContainer **ppContainer) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::ShowObject(void) { return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::OnShowWindow(BOOL fShow) { return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::RequestNewObjectLayout(void) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::GetMoniker(DWORD dwAssign, DWORD dwWhichMoniker, __RPC__deref_out_opt IMoniker **ppmk) { if ((dwAssign == OLEGETMONIKER_ONLYIFTHERE) && (dwWhichMoniker == OLEWHICHMK_CONTAINER))return E_FAIL; return E_NOTIMPL; }
// ----- IStorage -----
HRESULT STDMETHODCALLTYPE WebBrowser::CreateStream(__RPC__in_string const OLECHAR *pwcsName, DWORD grfMode, DWORD reserved1, DWORD reserved2, __RPC__deref_out_opt IStream **ppstm) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::OpenStream(const OLECHAR *pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream **ppstm) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::CreateStorage(__RPC__in_string const OLECHAR *pwcsName, DWORD grfMode, DWORD reserved1, DWORD reserved2, __RPC__deref_out_opt IStorage **ppstg) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::OpenStorage(__RPC__in_opt_string const OLECHAR *pwcsName, __RPC__in_opt IStorage *pstgPriority, DWORD grfMode, __RPC__deref_opt_in_opt SNB snbExclude, DWORD reserved, __RPC__deref_out_opt IStorage **ppstg) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::CopyTo(DWORD ciidExclude, const IID *rgiidExclude, __RPC__in_opt SNB snbExclude, IStorage *pstgDest) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::MoveElementTo(__RPC__in_string const OLECHAR *pwcsName, __RPC__in_opt IStorage *pstgDest, __RPC__in_string const OLECHAR *pwcsNewName, DWORD grfFlags) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::Commit(DWORD grfCommitFlags) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::Revert(void) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::EnumElements(DWORD reserved1, void *reserved2, DWORD reserved3, IEnumSTATSTG **ppenum) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::DestroyElement(__RPC__in_string const OLECHAR *pwcsName) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::RenameElement(__RPC__in_string const OLECHAR *pwcsOldName, __RPC__in_string const OLECHAR *pwcsNewName) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::SetElementTimes(__RPC__in_opt_string const OLECHAR *pwcsName, __RPC__in_opt const FILETIME *pctime, __RPC__in_opt const FILETIME *patime, __RPC__in_opt const FILETIME *pmtime) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::SetClass(__RPC__in REFCLSID clsid) { return S_OK; }
HRESULT STDMETHODCALLTYPE WebBrowser::SetStateBits(DWORD grfStateBits, DWORD grfMask) { return E_NOTIMPL; }
HRESULT STDMETHODCALLTYPE WebBrowser::Stat(__RPC__out STATSTG *pstatstg, DWORD grfStatFlag) { return E_NOTIMPL; }
Win32Project1.cpp
#include "stdafx.h"
#include "Win32Project1.h"
#include "WebBrowser.h"
HWND hWnd = NULL;
HWND tkWebBrowserHWND = NULL;
WebBrowser* tkWebBrowser = NULL;
void callFromThread();
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
OleInitialize(NULL);
MyRegisterClass(hInstance);
if (!InitInstance(hInstance, nCmdShow)) return FALSE;
tkWebBrowserHWND = CreateWindow(L"Static", NULL, WS_CHILD | WS_VISIBLE, 221, 0, 300, 300, hWnd, NULL, hInstance, 0);
tkWebBrowser = new WebBrowser(tkWebBrowserHWND);
// Use one of following command
//tkWebBrowser->SetText(L"Hello!");
//tkWebBrowser->setHTML(L"<h1>Title</h1><div>Content</div>");
//tkWebBrowser->setHTML(L"<html><body><h1>Title 2</h1><div>Content 2</div></body></html>", false);
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)callFromThread, 0, 0, 0);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
void callFromThread()
{
OleInitialize(NULL);
// Use one of following command
//tkWebBrowser->SetText(L"Hello from thread!");
//tkWebBrowser->setHTML(L"<h1>Title from thread</h1><div>Content from thread</div>");
tkWebBrowser->setHTML(L"<html><body><h1>Title 2 from thread</h1><div>Content 2 from thread</div></body></html>", false);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
HINSTANCE hInst;
const wchar_t* szTitle = L"Title";
const wchar_t* szWindowClass = L"Window Class";
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd) return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
Related
I'm new to c++ and try to write a update function.
The download with URLDownloadToFile is working without problems but if I changed the url to an invalid one, it stilled return S_OK ... How can I check if the download succede or not?
#include <WinInet.h>
#include <iomanip>
int download_file (const TCHAR urldownload[],const TCHAR target[] )
{
DownloadProgress progress;
IBindStatusCallback* callback = (IBindStatusCallback*)&progress;
SCP(40, NULL); cout << target;
HRESULT status = URLDownloadToFile(NULL, urldownload, target, 0, static_cast<IBindStatusCallback*>(&progress));
Sleep(200);
DeleteUrlCacheEntry(urldownload);
wcout << status;
if (status == S_OK) cout << "yes";
else(cout << "Download failed");
Sleep(10000); return 1;
}
class DownloadProgress : public IBindStatusCallback {
public:
HRESULT __stdcall QueryInterface(const IID &, void **) {
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void) {
return 1;
}
ULONG STDMETHODCALLTYPE Release(void) {
return 1;
}
HRESULT STDMETHODCALLTYPE OnStartBinding(DWORD dwReserved, IBinding *pib) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetPriority(LONG *pnPriority) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnLowResource(DWORD reserved) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(HRESULT hresult, LPCWSTR szError) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(DWORD *grfBINDF, BINDINFO *pbindinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(REFIID riid, IUnknown *punk) {
return E_NOTIMPL;
}
virtual HRESULT __stdcall OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
{
//wcout << ulProgress << L" of " << ulProgressMax << endl; Sleep(200);
if (ulProgress != 0 && ulProgressMax != 0)
{
double output = (double(ulProgress) / ulProgressMax)*100;
cout << "\r" << "Downloading: " << fixed << setprecision(2) << output << " % " ; Sleep(20);
}
return S_OK;
}
};
MSDN article has the answer for you:
URLDownloadToFile returns S_OK even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile. For best control over the download and its progress, an IBindStatusCallback interface is recommended.
You need to provide a status callback to receive status of the asynchronous operation. Your code snippet already has the base. OnProgress and OnStopBinding should get you the download failure result.
I have such an implementation
void coAudioPlayerSampleGrabber::test(SoundDataType dataType,
unsigned char const * pData,
int64_t dataLen)
{
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
IMFSourceReader *pReader = NULL;
IMFByteStream * spByteStream = NULL;
HRESULT hr = S_OK;
// Initialize the COM library.
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
// Initialize the Media Foundation platform.
if (SUCCEEDED(hr))
{
hr = MFStartup(MF_VERSION);
}
hr = MFCreateMFByteStreamOnStreamEx((IUnknown*)pData, &spByteStream);
if (FAILED(hr))
{
printf("Error MFCreateMFByteStreamOnStreamEx");
}
IMFAttributes * Atrr = NULL;
hr = MFCreateAttributes(&Atrr, 10);
if (FAILED(hr))
{
printf("Error MFCreateAttributes");
}
hr = Atrr->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, true);
if (FAILED(hr))
{
printf("Error Atrr->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, true)");
}
hr = MFCreateSourceReaderFromByteStream(spByteStream, Atrr, &pReader);
if (FAILED(hr))
{
printf("Error MFCreateSourceReaderFromByteStream");
}
if (FAILED(hr))
{
printf("Error opening input file");
}
IMFMediaType *pAudioType = NULL; // Represents the PCM audio format.
hr = ConfigureAudioStream(dataType, pReader, &pAudioType);
if (FAILED(hr))
{
printf("Error ConfigureAudioStream");
}
IMFSample *pSample = NULL;
IMFMediaBuffer *pBuffer = NULL;
BYTE *pAudioData = NULL;
DWORD cbBuffer = 0;
std::vector<SampleData> samples_vec;
while (true)
{
DWORD dwFlags = 0;
hr = pReader->ReadSample((DWORD)MF_SOURCE_READER_FIRST_AUDIO_STREAM, 0, NULL, &dwFlags, NULL, &pSample);
if (FAILED(hr)) { break; }
if (dwFlags & MF_SOURCE_READERF_CURRENTMEDIATYPECHANGED)
{
printf("Type change - not supported by WAVE file format.\n");
break;
}
if (dwFlags & MF_SOURCE_READERF_ENDOFSTREAM)
{
printf("End of input file.\n");
break;
}
hr = pSample->ConvertToContiguousBuffer(&pBuffer);
if (FAILED(hr)) { break; }
hr = pBuffer->Lock(&pAudioData, NULL, &cbBuffer);
if (FAILED(hr)) { break; }
//Do something with the pAudioData which is an array of unsigned chars of lenth cbBuffer
SampleData tmp;
tmp.pAudioData = new byte[cbBuffer];
memcpy(tmp.pAudioData, pAudioData, cbBuffer);
tmp.cbBuffer = cbBuffer;
samples_vec.push_back(tmp);
// Unlock the buffer.
hr = pBuffer->Unlock();
pAudioData = NULL;
if (FAILED(hr)) { break; }
}
SafeRelease(&pReader);
SafeRelease(&pSample);
SafeRelease(&pBuffer);
SafeRelease(&spByteStream);
SafeRelease(&Atrr);
// Shut down Media Foundation.
MFShutdown();
CoUninitialize();
}
So as you can see I have pointer to data and size this is actually my data I need to decode it. Problem is that here
hr = MFCreateMFByteStreamOnStreamEx((IUnknown*)pData, &spByteStream);
I got an error access violation and as far as I see this is because I try to convert pData to IUnknown*. Question is - How to convert it right?
You can't cut corners like this:
unsigned char const * pData;
...
hr = MFCreateMFByteStreamOnStreamEx((IUnknown*)pData, &spByteStream);
IUnknown is not yet another fancy alias for a byte. You are supposed to literally supply interface pointer representing stream, as documented.
Media Foundation does offer you means to read from memory bytes. You need to create a create a real stream, IStream or IRandomAccessStream or IMFByteStream per docuemntation. Also supply IMFAttributes you created with proper attributes to specify data type (which otherwise in the case of a file are derived from extension or MIME type) and then Source Reader API would be able to process memory bytes as source of media file data, and suitable decoder would decode audio into PCM data (similar to this).
Something you can do real quick: CreateStreamOnHGlobal to create IStream implementation and copy your bytes into underlying buffer (see docs). Then MFCreateMFByteStreamOnStream would create a IMFByteStream wrappr over it, and you can use this wrapper as MFCreateSourceReaderFromByteStream argument.
I used to use a VectorStream class for such stuff. Some functions are not implemented, but you should get the basic idea.
class VectorStream : public IStream
{
public:
bool ReadOnly = false;
ULONG r = 1;
std::vector<char> d;
size_t p = 0;
VectorStream()
{
}
void Clear()
{
d.clear();
p = 0;
}
// IUnknown
virtual HRESULT STDMETHODCALLTYPE QueryInterface(
/* [in] */ REFIID riid,
/* [iid_is][out] */ _COM_Outptr_ void __RPC_FAR* __RPC_FAR* ppvObject)
{
if (riid == __uuidof(IUnknown) || riid == __uuidof(IStream))
{
*ppvObject = (IStream*)this;
r++;
return S_OK;
}
return E_NOINTERFACE;
}
virtual ULONG STDMETHODCALLTYPE AddRef(void)
{
return ++r;
}
virtual ULONG STDMETHODCALLTYPE Release(void)
{
return --r;
}
HRESULT __stdcall Clone(
IStream** ppstm
)
{
return E_NOTIMPL;
}
HRESULT __stdcall Commit(
DWORD grfCommitFlags
)
{
return S_OK;
}
HRESULT __stdcall CopyTo(
IStream* pstm,
ULARGE_INTEGER cb,
ULARGE_INTEGER* pcbRead,
ULARGE_INTEGER* pcbWritten
)
{
return E_NOINTERFACE;
}
HRESULT __stdcall LockRegion(
ULARGE_INTEGER libOffset,
ULARGE_INTEGER cb,
DWORD dwLockType
)
{
return S_OK;
}
HRESULT __stdcall UnlockRegion(
ULARGE_INTEGER libOffset,
ULARGE_INTEGER cb,
DWORD dwLockType
)
{
return S_OK;
}
HRESULT __stdcall Revert()
{
return E_NOTIMPL;
}
HRESULT __stdcall Seek(
LARGE_INTEGER dlibMove,
DWORD dwOrigin,
ULARGE_INTEGER* plibNewPosition
)
{
LARGE_INTEGER lo = { 0 };
if (dwOrigin == STREAM_SEEK_SET)
{
p = dlibMove.QuadPart;
}
if (dwOrigin == STREAM_SEEK_CUR)
{
p += dlibMove.QuadPart;
}
if (dwOrigin == STREAM_SEEK_END)
{
p = d.size() - dlibMove.QuadPart;
}
if (p >= d.size())
p = d.size();
if (plibNewPosition)
plibNewPosition->QuadPart = p;
return S_OK;
}
HRESULT __stdcall SetSize(
ULARGE_INTEGER libNewSize
)
{
d.resize(libNewSize.QuadPart);
return S_OK;
}
int eb = 0;
HRESULT __stdcall Stat(
STATSTG* pstatstg,
DWORD grfStatFlag
)
{
pstatstg->type = STGTY_STREAM;
pstatstg->cbSize.QuadPart = d.size();
pstatstg->grfLocksSupported = true;
return S_OK;
}
unsigned long long readbytes = 0;
HRESULT __stdcall Read(
void* pv,
ULONG cb,
ULONG* pcbRead
)
{
auto av = d.size() - p;
if (cb < av)
av = cb;
memcpy(pv, d.data() + p, av);
p += av;
if (pcbRead)
*pcbRead = (ULONG)av;
// if (av < cb)
// return S_FALSE;
return S_OK;
}
HRESULT __stdcall Write(
const void* pv,
ULONG cb,
ULONG* pcbWritten
)
{
if (ReadOnly)
return STG_E_ACCESSDENIED;
if (d.size() < (p + cb))
{
auto exc = (p + cb) - d.size();
d.resize(d.size() + exc);
}
memcpy(d.data() + p, pv, cb);
p += cb;
if (pcbWritten)
*pcbWritten = cb;
return S_OK;
}
};
It encapsulates std::vector<> in a IStream.
This question is related to my previous question.
I need to obtain IContextMenu* interface pointer for files in different directories (or even drives).
See my code, which is not working properly (e.g. the file properties dialog shows wrong information), because I provide wrong relative PIDLs (as mentioned in this answer).
int main() {
CoInitialize(NULL);
LPOLESTR pszFile = OLESTR("c:\\Windows\\notepad.exe");
LPOLESTR pszFile2 = OLESTR("c:\\Windows\\System32\\notepad.exe");
//LPOLESTR pszDir = OLESTR("c:\\Windows\\");
LPITEMIDLIST pidl = NULL;
LPITEMIDLIST pidl2 = NULL;
LPITEMIDLIST pidlDir;
LPCITEMIDLIST pidlItem;
LPCITEMIDLIST pidlItem2;
HRESULT hr;
IShellFolder* pFolder;
//IShellFolder* pDir;
IShellFolder* pDesktop;
IContextMenu* pContextMenu;
HMENU hMenu;
CMINVOKECOMMANDINFO cmi;
TCHAR szTemp[256];
hr = SHGetDesktopFolder(&pDesktop);
if (FAILED(hr)) {
CoUninitialize();
return 0;
}
HWND wnd = ::CreateWindowA("STATIC", "dummy", WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, NULL, NULL);
/*hr = pDesktop->ParseDisplayName(wnd, NULL, pszDir, NULL, &pidlDir, NULL);
if (FAILED(hr)) {
goto clear;
}
hr = pDesktop->BindToObject(pidlDir, 0, IID_IShellFolder, (void**)&pDir);
if (FAILED(hr)) {
goto clear;
}
*/
hr = pDesktop->ParseDisplayName(wnd, NULL, pszFile, NULL, &pidl, NULL);
if (FAILED(hr)) {
goto clear;
}
hr = pDesktop->ParseDisplayName(wnd, NULL, pszFile2, NULL, &pidl2, NULL);
if (FAILED(hr)) {
goto clear;
}
hr = SHBindToParent(pidl, IID_IShellFolder, (void **)&pFolder, &pidlItem);
if (FAILED(hr)) {
goto clear;
}
pFolder->Release();
hr = SHBindToParent(pidl2, IID_IShellFolder, (void **)&pFolder, &pidlItem2);
if (FAILED(hr)) {
goto clear;
}
LPCITEMIDLIST list[] = {pidlItem, pidlItem2};
hr = pFolder->GetUIObjectOf(wnd, 2, (LPCITEMIDLIST *)list, IID_IContextMenu, NULL, (void **)&pContextMenu);
pFolder->Release();
if (SUCCEEDED(hr)) {
hMenu = CreatePopupMenu();
if (hMenu) {
hr = pContextMenu->QueryContextMenu(hMenu, 0, 1, 0x7fff, CMF_EXPLORE);
if (SUCCEEDED(hr)) {
int idCmd = TrackPopupMenu(hMenu,
TPM_LEFTALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON,
1, 1, 0, wnd, NULL);
if (idCmd) {
cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
cmi.fMask = 0;
cmi.hwnd = wnd;
cmi.lpVerb = MAKEINTRESOURCEA(idCmd-1);
cmi.lpParameters = NULL;
cmi.lpDirectory = NULL;
cmi.nShow = SW_SHOWNORMAL;
cmi.dwHotKey = 0;
cmi.hIcon = NULL;
hr = pContextMenu->InvokeCommand(&cmi);
if (!SUCCEEDED(hr)) {
wsprintf(szTemp, _T("InvokeCommand failed. hr=%lx"), hr);
MessageBox(0, szTemp, 0, 0);
PostQuitMessage(0);
}
}
}
DestroyMenu(hMenu);
}
pContextMenu->Release();
}
MSG msg;
BOOL bRet;
while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0) {
if (bRet == -1) {
// Handle Error
}
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
clear:
pDesktop->Release();
SHFree(pidl);
SHFree(pidl2);
CoUninitialize();
return 0;
}
Maybe it is possible to achieve using SHCreateDefaultContextMenu or CDefFolderMenu_Create2 but I don't know how.
It is possible if you embed IExplorerBrowser UI object into your app, which is available since Windows Vista.
The you can fill it with any PIDLs through IResultsFolder.
Here is sample code, which is just slightly modified example from Win7 Platform SDK (C:\Program Files\Microsoft SDKs\Windows\v7.1\Samples\winui\shell\appplatform\ExplorerBrowserCustomContents\ExplorerBrowserCustomContents.sln)
There is definitely an another method using IShellView which works on Windows XP but I didn't find it anyway.
#define STRICT_TYPED_ITEMIDS
#include <windows.h>
#include <windowsx.h>
#include <shlobj.h>
#include <shellapi.h>
#include <shlwapi.h>
#include <propkey.h>
#include <new>
#include "resource.h"
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
HINSTANCE g_hinst = 0;
UINT const KFD_SELCHANGE = WM_USER;
class CFillResultsOnBackgroundThread;
class CExplorerBrowserHostDialog : public IServiceProvider, public ICommDlgBrowser
{
public:
CExplorerBrowserHostDialog() : _cRef(1), _hdlg(NULL), _peb(NULL), _fEnumerated(FALSE), _prf(NULL)
{
}
HRESULT DoModal(HWND hwnd)
{
DialogBoxParam(g_hinst, MAKEINTRESOURCE(IDD_DIALOG1), hwnd, s_DlgProc, (LPARAM)this);
return S_OK;
}
// IUnknown
STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
{
static const QITAB qit[] =
{
QITABENT(CExplorerBrowserHostDialog, IServiceProvider),
QITABENT(CExplorerBrowserHostDialog, ICommDlgBrowser),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
STDMETHODIMP_(ULONG) AddRef()
{
return InterlockedIncrement(&_cRef);
}
STDMETHODIMP_(ULONG) Release()
{
long cRef = InterlockedDecrement(&_cRef);
if (!cRef)
delete this;
return cRef;
}
// IServiceProvider
STDMETHODIMP QueryService(REFGUID guidService, REFIID riid, void **ppv)
{
HRESULT hr = E_NOINTERFACE;
*ppv = NULL;
if (guidService == SID_SExplorerBrowserFrame)
{
// responding to this SID allows us to hook up our ICommDlgBrowser
// implementation so we get selection change events from the view
hr = QueryInterface(riid, ppv);
}
return hr;
}
// ICommDlgBrowser
STDMETHODIMP OnDefaultCommand(IShellView * /* psv */)
{
_OnExplore();
return S_OK;
}
STDMETHODIMP OnStateChange(IShellView * /* psv */, ULONG uChange)
{
if (uChange == CDBOSC_SELCHANGE)
{
PostMessage(_hdlg, KFD_SELCHANGE, 0, 0);
}
return S_OK;
}
STDMETHODIMP IncludeObject(IShellView * /* psv */, PCUITEMID_CHILD /* pidl */)
{
return S_OK;
}
void FillResultsOnBackgroundThread(IResultsFolder *prf);
private:
~CExplorerBrowserHostDialog()
{
}
static INT_PTR CALLBACK s_DlgProc(HWND hdlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CExplorerBrowserHostDialog *pebhd = reinterpret_cast<CExplorerBrowserHostDialog *>(GetWindowLongPtr(hdlg, DWLP_USER));
if (uMsg == WM_INITDIALOG)
{
pebhd = reinterpret_cast<CExplorerBrowserHostDialog *>(lParam);
pebhd->_hdlg = hdlg;
SetWindowLongPtr(hdlg, DWLP_USER, reinterpret_cast<LONG_PTR>(pebhd));
}
return pebhd ? pebhd->_DlgProc(uMsg, wParam, lParam) : 0;
}
INT_PTR _DlgProc(UINT uMsg, WPARAM wParam, LPARAM lParam);
HRESULT _FillViewWithKnownFolders(IResultsFolder *prf);
void _OnInitDlg();
void _OnDestroyDlg();
void _StartFolderEnum();
void _OnSelChange();
void _OnExplore();
void _OnRefresh();
long _cRef;
HWND _hdlg;
IExplorerBrowser *_peb;
IResultsFolder *_prf;
BOOL _fEnumerated;
static const UINT c_rgControlsShownOnEnum[3]; // controls that will be shown while known folder list is populated
static const UINT c_rgControlsHiddenOnEnum[4]; // controls that will be hidden while known folder list is populated
};
const UINT CExplorerBrowserHostDialog::c_rgControlsShownOnEnum[] =
{
IDC_STATUS,
IDC_ENUMNAME,
IDC_ENUMPATH
};
const UINT CExplorerBrowserHostDialog::c_rgControlsHiddenOnEnum[] =
{
IDC_FOLDERNAME,
IDC_FOLDERPATH,
IDC_LBLFOLDER,
IDC_LBLPATH
};
HRESULT CExplorerBrowserHostDialog::_FillViewWithKnownFolders(IResultsFolder *prf)
{
LPOLESTR pszFile = OLESTR("c:\\Windows\\notepad.exe");
LPOLESTR pszFile2 = OLESTR("c:\\Windows\\System32\\notepad.exe");
IShellFolder* pDesktop;
PIDLIST_RELATIVE pidl = NULL;
PIDLIST_RELATIVE pidl2 = NULL;
HRESULT hr;
hr = SHGetDesktopFolder(&pDesktop);
if (FAILED(hr)) {
return E_FAIL;
}
hr = pDesktop->ParseDisplayName(0, NULL, pszFile, NULL, &pidl, NULL);
if (FAILED(hr)) {
pDesktop->Release();
return E_FAIL;
}
hr = pDesktop->ParseDisplayName(0, NULL, pszFile2, NULL, &pidl2, NULL);
if (FAILED(hr)) {
pDesktop->Release();
return E_FAIL;
}
prf->AddIDList((PIDLIST_ABSOLUTE)pidl, 0);
prf->AddIDList((PIDLIST_ABSOLUTE)pidl2, 0);
pDesktop->Release();
IFolderView2 *pfv2;
IShellView *shellView;
hr = _peb->GetCurrentView(IID_PPV_ARGS(&pfv2));
if (SUCCEEDED(hr)) {
pfv2->SelectItem(0, SVSI_SELECT);
pfv2->SelectItem(1, SVSI_SELECT);
//hr = pfv2->QueryInterface(IID_IOleWindow, (void**)&oleWnd);
hr = _peb->GetCurrentView(IID_PPV_ARGS(&shellView));
if (SUCCEEDED(hr)) {
HWND wnd = 0;
shellView->GetWindow(&wnd);
//SendMessage(wnd, WM_CONTEXTMENU, 0, MAKELPARAM(0,0)); not working
}
}
return S_OK;
}
void CExplorerBrowserHostDialog::_OnInitDlg()
{
// Hide initial folder information
for (UINT i = 0; i < ARRAYSIZE(c_rgControlsHiddenOnEnum); i++)
{
ShowWindow(GetDlgItem(_hdlg, c_rgControlsHiddenOnEnum[i]), SW_HIDE);
}
HWND hwndStatic = GetDlgItem(_hdlg, IDC_BROWSER);
if (hwndStatic)
{
RECT rc;
GetWindowRect(hwndStatic, &rc);
MapWindowRect(HWND_DESKTOP, _hdlg, &rc);
HRESULT hr = CoCreateInstance(CLSID_ExplorerBrowser, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&_peb));
if (SUCCEEDED(hr))
{
IUnknown_SetSite(_peb, static_cast<IServiceProvider *>(this));
FOLDERSETTINGS fs = {0};
fs.ViewMode = FVM_DETAILS;
fs.fFlags = FWF_AUTOARRANGE | FWF_NOWEBVIEW;
hr = _peb->Initialize(_hdlg, &rc, &fs);
if (SUCCEEDED(hr))
{
_peb->SetOptions(EBO_NAVIGATEONCE); // do not allow navigations
// Initialize the explorer browser so that we can use the results folder
// as the data source. This enables us to program the contents of
// the view via IResultsFolder
hr = _peb->FillFromObject(NULL, EBF_NODROPTARGET);
if (SUCCEEDED(hr))
{
IFolderView2 *pfv2;
hr = _peb->GetCurrentView(IID_PPV_ARGS(&pfv2));
if (SUCCEEDED(hr))
{
IColumnManager *pcm;
hr = pfv2->QueryInterface(IID_PPV_ARGS(&pcm));
if (SUCCEEDED(hr))
{
PROPERTYKEY rgkeys[] = {PKEY_ItemNameDisplay, PKEY_ItemFolderPathDisplay};
hr = pcm->SetColumns(rgkeys, ARRAYSIZE(rgkeys));
if (SUCCEEDED(hr))
{
CM_COLUMNINFO ci = {sizeof(ci), CM_MASK_WIDTH | CM_MASK_DEFAULTWIDTH | CM_MASK_IDEALWIDTH};
hr = pcm->GetColumnInfo(PKEY_ItemFolderPathDisplay, &ci);
if (SUCCEEDED(hr))
{
ci.uWidth += 100;
ci.uDefaultWidth += 100;
ci.uIdealWidth += 100;
pcm->SetColumnInfo(PKEY_ItemFolderPathDisplay, &ci);
}
}
pcm->Release();
}
hr = pfv2->GetFolder(IID_PPV_ARGS(&_prf));
if (SUCCEEDED(hr))
{
_StartFolderEnum();
}
pfv2->Release();
}
}
}
}
// If we fail to initialize properly, close the dialog
if (FAILED(hr))
{
EndDialog(_hdlg, IDCLOSE);
}
}
}
// pass -1 for the current selected item
// returns an IShellItem type object
HRESULT GetItemFromView(IFolderView2 *pfv, int iItem, REFIID riid, void **ppv)
{
*ppv = NULL;
HRESULT hr = S_OK;
if (iItem == -1)
{
hr = pfv->GetSelectedItem(-1, &iItem); // Returns S_FALSE if none selected
}
if (S_OK == hr)
{
hr = pfv->GetItem(iItem, riid, ppv);
}
else
{
hr = E_FAIL;
}
return hr;
}
void CExplorerBrowserHostDialog::_OnSelChange()
{
if (_fEnumerated)
{
IFolderView2 *pfv2;
HRESULT hr = _peb->GetCurrentView(IID_PPV_ARGS(&pfv2));
if (SUCCEEDED(hr))
{
IShellItem2 *psi;
hr = GetItemFromView(pfv2, -1, IID_PPV_ARGS(&psi));
if (SUCCEEDED(hr))
{
PWSTR pszName;
hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &pszName);
if (SUCCEEDED(hr))
{
SetDlgItemText(_hdlg, IDC_FOLDERNAME, pszName);
CoTaskMemFree(pszName);
}
hr = psi->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &pszName);
if (SUCCEEDED(hr))
{
SetDlgItemText(_hdlg, IDC_FOLDERPATH, pszName);
CoTaskMemFree(pszName);
}
psi->Release();
}
else if (hr == S_FALSE)
{
SetDlgItemText(_hdlg, IDC_FOLDERNAME, TEXT(""));
SetDlgItemText(_hdlg, IDC_FOLDERPATH, TEXT(""));
}
EnableWindow(GetDlgItem(_hdlg, IDC_EXPLORE), hr == S_OK);
pfv2->Release();
}
}
}
void CExplorerBrowserHostDialog::_OnDestroyDlg()
{
if (_peb)
{
IUnknown_SetSite(_peb, NULL);
_peb->Destroy();
_peb->Release();
_peb = NULL;
}
if (_prf)
{
_prf->Release();
_prf = NULL;
}
}
void CExplorerBrowserHostDialog::_OnExplore()
{
IFolderView2 *pfv2;
HRESULT hr = _peb->GetCurrentView(IID_PPV_ARGS(&pfv2));
if (SUCCEEDED(hr))
{
IShellItem *psi;
hr = GetItemFromView(pfv2, -1, IID_PPV_ARGS(&psi));
if (SUCCEEDED(hr))
{
PIDLIST_ABSOLUTE pidl;
hr = SHGetIDListFromObject(psi, &pidl);
if (SUCCEEDED(hr))
{
SHELLEXECUTEINFO ei = { sizeof(ei) };
ei.fMask = SEE_MASK_INVOKEIDLIST;
ei.hwnd = _hdlg;
ei.nShow = SW_NORMAL;
ei.lpIDList = pidl;
ShellExecuteEx(&ei);
CoTaskMemFree(pidl);
}
psi->Release();
}
pfv2->Release();
}
}
void CExplorerBrowserHostDialog::_OnRefresh()
{
_fEnumerated = FALSE;
// Update UI
EnableWindow(GetDlgItem(_hdlg, IDC_EXPLORE), FALSE);
EnableWindow(GetDlgItem(_hdlg, IDC_REFRESH), FALSE);
if (SUCCEEDED(_peb->RemoveAll()))
{
_StartFolderEnum();
}
}
void CExplorerBrowserHostDialog::_StartFolderEnum()
{
FillResultsOnBackgroundThread(_prf);
}
void CExplorerBrowserHostDialog::FillResultsOnBackgroundThread(IResultsFolder *prf)
{
_FillViewWithKnownFolders(prf);
_fEnumerated = TRUE;
/*
// Adjust dialog to show proper view info and buttons
for (UINT k = 0; k < ARRAYSIZE(c_rgControlsShownOnEnum); k++)
{
ShowWindow(GetDlgItem(_hdlg, c_rgControlsShownOnEnum[k]), SW_HIDE);
}
for (UINT l = 0; l < ARRAYSIZE(c_rgControlsHiddenOnEnum); l++)
{
ShowWindow(GetDlgItem(_hdlg, c_rgControlsHiddenOnEnum[l]), SW_SHOW);
}*/
EnableWindow(GetDlgItem(_hdlg, IDC_REFRESH), TRUE);
}
INT_PTR CExplorerBrowserHostDialog::_DlgProc(UINT uMsg, WPARAM wParam, LPARAM /* lParam */)
{
INT_PTR iRet = 1; // default for all handled cases in switch below
switch (uMsg)
{
case WM_INITDIALOG:
_OnInitDlg();
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDOK:
case IDCANCEL:
case IDC_CANCEL:
return EndDialog(_hdlg, TRUE);
case IDC_REFRESH:
_OnRefresh();
break;
case IDC_EXPLORE:
_OnExplore();
break;
}
break;
case KFD_SELCHANGE:
_OnSelChange();
break;
case WM_DESTROY:
_OnDestroyDlg();
break;
default:
iRet = 0;
break;
}
return iRet;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int)
{
g_hinst = hInstance;
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
OleInitialize(0); // for drag and drop
CExplorerBrowserHostDialog *pdlg = new (std::nothrow) CExplorerBrowserHostDialog();
if (pdlg)
{
pdlg->DoModal(NULL);
pdlg->Release();
}
OleUninitialize();
CoUninitialize();
}
return 0;
}
I'm new to c++ and try to write a update function.
The download with URLDownloadToFile is working without problems but if I changed the url to an invalid one, it stilled return S_OK ... How can I check if the download succede or not?
#include <WinInet.h>
#include <iomanip>
int download_file (const TCHAR urldownload[],const TCHAR target[] )
{
DownloadProgress progress;
IBindStatusCallback* callback = (IBindStatusCallback*)&progress;
SCP(40, NULL); cout << target;
HRESULT status = URLDownloadToFile(NULL, urldownload, target, 0, static_cast<IBindStatusCallback*>(&progress));
Sleep(200);
DeleteUrlCacheEntry(urldownload);
wcout << status;
if (status == S_OK) cout << "yes";
else(cout << "Download failed");
Sleep(10000); return 1;
}
class DownloadProgress : public IBindStatusCallback {
public:
HRESULT __stdcall QueryInterface(const IID &, void **) {
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef(void) {
return 1;
}
ULONG STDMETHODCALLTYPE Release(void) {
return 1;
}
HRESULT STDMETHODCALLTYPE OnStartBinding(DWORD dwReserved, IBinding *pib) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetPriority(LONG *pnPriority) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnLowResource(DWORD reserved) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE OnStopBinding(HRESULT hresult, LPCWSTR szError) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE GetBindInfo(DWORD *grfBINDF, BINDINFO *pbindinfo) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pformatetc, STGMEDIUM *pstgmed) {
return E_NOTIMPL;
}
virtual HRESULT STDMETHODCALLTYPE OnObjectAvailable(REFIID riid, IUnknown *punk) {
return E_NOTIMPL;
}
virtual HRESULT __stdcall OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
{
//wcout << ulProgress << L" of " << ulProgressMax << endl; Sleep(200);
if (ulProgress != 0 && ulProgressMax != 0)
{
double output = (double(ulProgress) / ulProgressMax)*100;
cout << "\r" << "Downloading: " << fixed << setprecision(2) << output << " % " ; Sleep(20);
}
return S_OK;
}
};
MSDN article has the answer for you:
URLDownloadToFile returns S_OK even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile. For best control over the download and its progress, an IBindStatusCallback interface is recommended.
You need to provide a status callback to receive status of the asynchronous operation. Your code snippet already has the base. OnProgress and OnStopBinding should get you the download failure result.
I need to convert an interface defined in Delpi to C++. I made small demo project that contains all of the stuff shown below.
First is the Delphi function that the person claims to be working on this thread:
function CanFileBeDeletedToRecycleBin(const AFileName: UnicodeString): Boolean;
var
RecycleBinManager: IRecycleBinManager;
begin
OleCheck(CoCreateInstance(CLSID_RecycleBinManager, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IRecycleBinManager, RecycleBinManager));
try
Result := RecycleBinManager.WillRecycle(PWideChar(AFileName)) = S_OK;
finally
RecycleBinManager := nil;
end;
end;
The rest of the Delphi interface definitions are here.
So I came up with the following C++ code that unfortunately crashes. (Again, let me repeat that the person who wrote Delphi version claims that it works fine for him.)
HRESULT hr;
CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
// {4A04656D-52AA-49DE-8A09-CB178760E748}
const CLSID CLSID_RecycleBinManager = {0x4A04656D, 0x52AA, 0x49DE, {0x8A, 0x09, 0xCB, 0x17, 0x87, 0x60, 0xE7, 0x48}};
// {5869092D-8AF9-4A6C-AE84-1F03BE2246CC}
const IID IID_IRecycleBinManager = {0x5869092D, 0x8AF9, 0x4A6C, {0xAE, 0x84, 0x1F, 0x03, 0xBE, 0x22, 0x46, 0xCC}};
IRecycleBinManager* pIRBM = NULL;
hr = CoCreateInstance(CLSID_RecycleBinManager, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
IID_IRecycleBinManager, (void**) &pIRBM);
// hr = SHCoCreateInstance(NULL, &CLSID_RecycleBinManager, NULL, IID_IRecycleBinManager, (void **)&pIRBM);
if (SUCCEEDED(hr))
{
//I get a crash on the next line:
//Unhandled exception at 0x76175ed2: 0xC0000005: Access violation writing location 0xa84d252b.
hr = pIRBM->WillRecycle(L"C:\\test del");
pIRBM->Release();
}
I tried my best to convert that interface from Delphi (here) to C++. Here's what I got (after #RemyLebeau's suggestions, which unfortunately still didn't work for me.)
#pragma pack(push,1)
struct DELETEDITEM
{
DWORD dwFileSizeLow;
DWORD dwFileSizeHigh;
#pragma pack(push,8)
FILETIME ftDeletionTime;
#pragma pack(pop)
WCHAR szOriginalPath[MAX_PATH];
WCHAR szDisplacedPath[MAX_PATH];
};
#pragma pack(pop)
enum RECYCLEBIN_TYPE : unsigned char
{
RBTYPE_VOLUME,
RBTYPE_KNOWNFOLDER
};
#if defined(__cplusplus) && !defined(CINTERFACE)
MIDL_INTERFACE("6E325F88-D12F-49E5-895B-8EC98630C021")
IEnumRecycleItems : public IUnknown
{
public:
//virtual HRESULT __stdcall Next(unsigned celt, /* out */ tagDELETEDITEM &rgelt, unsigned &pceltFetched) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Next(
__RPC__in ULONG celt,
__RPC__deref_out DELETEDITEM* rgelt,
__RPC__deref_out ULONG* pceltFetched
) = 0;
//virtual HRESULT __stdcall Skip(unsigned celt) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Skip(
__RPC__in ULONG celt
) = 0;
//virtual HRESULT __stdcall Reset(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Reset(
void
) = 0;
//virtual HRESULT __stdcall Clone(/* out */ _di_IEnumRecycleItems &ppenum) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Clone(
__RPC__deref_out IEnumRecycleItems** ppenum
) = 0;
};
MIDL_INTERFACE("0125E62F-8349-443A-854B-A55FB84CFA35")
IRecycle : public IUnknown
{
public:
//virtual HRESULT __stdcall Compact(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Compact(void) = 0;
//virtual HRESULT __stdcall GetFileData(const System::WideChar * pszPath, /* out */ tagDELETEDITEM &lpData) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetFileData(
__RPC__in const WCHAR* pszPath,
__RPC__deref_out DELETEDITEM* lpData
) = 0;
//virtual HRESULT __stdcall GetItemCount(/* out */ __int64 &lpCount) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetItemCount(
__RPC__deref_out __int64* lpCount
) = 0;
//virtual HRESULT __stdcall GetUsedSpace(/* out */ __int64 &lpUsedSpace) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetUsedSpace(
__RPC__deref_out __int64* lpUsedSpace
) = 0;
//virtual HRESULT __stdcall IsEmpty(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE IsEmpty(
void
) = 0;
//virtual HRESULT __stdcall PurgeAll(_di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE PurgeAll(
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall PurgeItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE PurgeItems(
__RPC__in const WCHAR* lpstrItems,
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall SuspendUpdating(BOOL fSuspend) = 0 ;
virtual HRESULT STDMETHODCALLTYPE SuspendUpdating(
__RPC__in BOOL fSuspend
) = 0;
//virtual HRESULT __stdcall RecycleItem(const System::WideChar * lpstrItem, const unsigned dwAttrs, const __int64 iFileSize, /* out */ _di_IShellItem &psi) = 0 ;
virtual HRESULT STDMETHODCALLTYPE RecycleItem(
__RPC__in const WCHAR* lpstrItem,
__RPC__in const DWORD dwAttrs,
__RPC__in const __int64 iFileSize,
__RPC__deref_out IShellItem** psi
) = 0;
//virtual HRESULT __stdcall RestoreItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE RestoreItems(
__RPC__in const WCHAR* lpstrItems,
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall IsRecycled(const System::WideChar * pszPath, PBOOL lpRecycled) = 0 ;
virtual HRESULT STDMETHODCALLTYPE IsRecycled(
__RPC__in const WCHAR* pszPath,
__RPC__out PBOOL lpRecycled
) = 0;
//virtual HRESULT __stdcall EnumItems(unsigned dwFlags, /* out */ _di_IEnumRecycleItems &EnumRecycleItems) = 0 ;
virtual HRESULT STDMETHODCALLTYPE EnumItems(
__RPC__in DWORD dwFlags,
__RPC__deref_out IEnumRecycleItems** enm
) = 0;
//virtual HRESULT __stdcall WillRecycle(const System::WideChar * pszPath) = 0 ;
virtual HRESULT STDMETHODCALLTYPE WillRecycle(
__RPC__in const WCHAR* pszPath
) = 0;
};
MIDL_INTERFACE("F964AD97-96F4-48AB-B444-E8588BC7C7B3")
IRecycleBin : public IUnknown
{
public:
//virtual HRESULT __stdcall Compact(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Compact(void) = 0;
//virtual HRESULT __stdcall GetFileData(const System::WideChar * pszPath, /* out */ tagDELETEDITEM &lpData) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetFileData(
__RPC__in const WCHAR* pszPath,
__RPC__deref_out DELETEDITEM* lpData
) = 0;
//virtual HRESULT __stdcall GetItemCount(/* out */ __int64 &lpCount) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetItemCount(
__RPC__deref_out __int64* lpCount
) = 0;
//virtual HRESULT __stdcall GetUsedSpace(/* out */ __int64 &lpUsedSpace) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetUsedSpace(
__RPC__deref_out __int64* lpUsedSpace
) = 0;
//virtual HRESULT __stdcall IsEmpty(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE IsEmpty(
void
) = 0;
//virtual HRESULT __stdcall PurgeAll(_di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE PurgeAll(
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall PurgeItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE PurgeItems(
__RPC__in const WCHAR* lpstrItems,
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall SuspendUpdating(BOOL fSuspend) = 0 ;
virtual HRESULT STDMETHODCALLTYPE SuspendUpdating(
__RPC__in BOOL fSuspend
) = 0;
//virtual HRESULT __stdcall RecycleItem(const System::WideChar * lpstrItem, const unsigned dwAttrs, const __int64 iFileSize, /* out */ _di_IShellItem &psi) = 0 ;
virtual HRESULT STDMETHODCALLTYPE RecycleItem(
__RPC__in const WCHAR* lpstrItem,
__RPC__in const DWORD dwAttrs,
__RPC__in const __int64 iFileSize,
__RPC__deref_out IShellItem** psi
) = 0;
//virtual HRESULT __stdcall RestoreItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE RestoreItems(
__RPC__in const WCHAR* lpstrItems,
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall IsRecycled(const System::WideChar * pszPath, PBOOL lpRecycled) = 0 ;
virtual HRESULT STDMETHODCALLTYPE IsRecycled(
__RPC__in const WCHAR* pszPath,
__RPC__out PBOOL lpRecycled
) = 0;
//virtual HRESULT __stdcall EnumItems(unsigned dwFlags, /* out */ _di_IEnumRecycleItems &EnumRecycleItems) = 0 ;
virtual HRESULT STDMETHODCALLTYPE EnumItems(
__RPC__in DWORD dwFlags,
__RPC__deref_out IEnumRecycleItems** enm
) = 0;
//virtual HRESULT __stdcall WillRecycle(const System::WideChar * pszPath) = 0 ;
virtual HRESULT STDMETHODCALLTYPE WillRecycle(
__RPC__in const WCHAR* pszPath
) = 0;
//virtual HRESULT __stdcall Initialize(const tagRECYCLEBIN_TYPE rbType, const System::WideChar * pszID) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Initialize(
__RPC__in const RECYCLEBIN_TYPE rbType,
__RPC__in const WCHAR* pszID
) = 0;
//virtual HRESULT __stdcall GetTypeID(/* out */ tagRECYCLEBIN_TYPE &rbType, System::WideChar * &pszID) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetTypeID(
__RPC__deref_out RECYCLEBIN_TYPE* rbType,
__RPC__in const WCHAR* pszID
) = 0;
//virtual HRESULT __stdcall GetIDList(/* out */ Winapi::Shlobj::PItemIDList &ppidl) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetIDList(
__RPC__deref_out LPITEMIDLIST *ppidl
) = 0;
//virtual HRESULT __stdcall GetLocation(System::WideChar * pszPathBuffer, unsigned cchMax) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetLocation(
__RPC__in WCHAR* pszPathBuffer,
__RPC__in UINT cchMax
) = 0;
//virtual HRESULT __stdcall GetMaxCapacityRange(/* out */ __int64 &lpMin, /* out */ __int64 &lpMax) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetMaxCapacityRange(
__RPC__deref_out __int64* lpMin,
__RPC__deref_out __int64* lpMax
) = 0;
//virtual HRESULT __stdcall GetMaxCapacity(/* out */ __int64 &lpCapacity) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetMaxCapacity(
__RPC__deref_out __int64* lpCapacity
) = 0;
//virtual HRESULT __stdcall SetMaxCapacity(const __int64 lpCapacity) = 0 ;
virtual HRESULT STDMETHODCALLTYPE SetMaxCapacity(
__RPC__in const __int64 lpCapacity
) = 0;
//virtual HRESULT __stdcall GetPurgeOnDelete(/* out */ BOOL &fNukeOnDelete) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetPurgeOnDelete(
__RPC__deref_out BOOL* fNukeOnDelete
) = 0;
//virtual HRESULT __stdcall SetPurgeOnDelete(const BOOL fNukeOnDelete) = 0 ;
virtual HRESULT STDMETHODCALLTYPE SetPurgeOnDelete(
__RPC__in const BOOL fNukeOnDelete
) = 0;
};
MIDL_INTERFACE("5869092D-8AF9-4A6C-AE84-1F03BE2246CC")
IRecycleBinManager : public IUnknown
{
public:
//virtual HRESULT __stdcall Compact(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Compact(void) = 0;
//virtual HRESULT __stdcall GetFileData(const System::WideChar * pszPath, /* out */ tagDELETEDITEM &lpData) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetFileData(
__RPC__in const WCHAR* pszPath,
__RPC__deref_out DELETEDITEM* pDeletedItem
) = 0;
//virtual HRESULT __stdcall GetItemCount(/* out */ __int64 &lpCount) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetItemCount(
__RPC__deref_out __int64* lpCount
) = 0;
//virtual HRESULT __stdcall GetUsedSpace(/* out */ __int64 &lpUsedSpace) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetUsedSpace(
__RPC__deref_out __int64* lpUsedSpace
) = 0;
//virtual HRESULT __stdcall IsEmpty(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE IsEmpty(
void
) = 0;
//virtual HRESULT __stdcall PurgeAll(_di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE PurgeAll(
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall PurgeItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE PurgeItems(
__RPC__in const WCHAR* lpstrItems,
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall SuspendUpdating(BOOL fSuspend) = 0 ;
virtual HRESULT STDMETHODCALLTYPE SuspendUpdating(
__RPC__in BOOL fSuspend
) = 0;
//virtual HRESULT __stdcall RecycleItem(const System::WideChar * lpstrItem, const unsigned dwAttrs, const __int64 iFileSize, /* out */ _di_IShellItem &psi) = 0 ;
virtual HRESULT STDMETHODCALLTYPE RecycleItem(
__RPC__in const WCHAR* lpstrItem,
__RPC__in const DWORD dwAttrs,
__RPC__in const __int64 iFileSize,
__RPC__deref_out IShellItem** psi
) = 0;
//virtual HRESULT __stdcall RestoreItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT STDMETHODCALLTYPE RestoreItems(
__RPC__in const WCHAR* lpstrItems,
__RPC__in IFileOperation* pfo
) = 0;
//virtual HRESULT __stdcall IsRecycled(const System::WideChar * pszPath, PBOOL lpRecycled) = 0 ;
virtual HRESULT STDMETHODCALLTYPE IsRecycled(
__RPC__in const WCHAR* pszPath,
__RPC__out PBOOL lpRecycled
) = 0;
//virtual HRESULT __stdcall EnumItems(unsigned dwFlags, /* out */ _di_IEnumRecycleItems &EnumRecycleItems) = 0 ;
virtual HRESULT STDMETHODCALLTYPE EnumItems(
__RPC__in DWORD dwFlags,
__RPC__deref_out IEnumRecycleItems** enm
) = 0;
//virtual HRESULT __stdcall WillRecycle(const System::WideChar * pszPath) = 0 ;
virtual HRESULT STDMETHODCALLTYPE WillRecycle(
__RPC__in const WCHAR* pszPath
) = 0;
//virtual HRESULT __stdcall DelayCompaction(const BOOL fDelay) = 0 ;
virtual HRESULT STDMETHODCALLTYPE DelayCompaction(
__RPC__in const BOOL fDelay
) = 0;
//virtual HRESULT __stdcall GetRecycleBinCount(/* out */ int &iCount) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetRecycleBinCount(
__RPC__deref_out int* iCount
) = 0;
//virtual HRESULT __stdcall GetRecycleBinAt(const int index, const GUID &iid, /* out */ void *ppv) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetRecycleBinAt(
__RPC__in const int index,
const GUID *iid,
__RPC__deref_out void **ppv
) = 0;
//virtual HRESULT __stdcall GetRecycleBin(const System::WideChar * pszPath, const GUID &iid, /* out */ void *ppv) = 0 ;
virtual HRESULT STDMETHODCALLTYPE GetRecycleBin(
__RPC__in const WCHAR* pszRootPath,
const GUID *iid,
__RPC__deref_out void **ppv
) = 0;
//virtual HRESULT __stdcall Refresh(void) = 0 ;
virtual HRESULT STDMETHODCALLTYPE Refresh(
void
) = 0;
};
#endif
Can someone take a look. What am I missing there?
Rather than translating the interface code from Delphi to C++, you can take the original code from the Russian site, put it in a .pas file as-is, and then add that file to your C++ project. When you build the project (or just compile the .pas file by itself), a C++ .hpp file will be generated for you, which you can then #include into your C++ code.
Doing it this way, the following code works fine for me in C++Builder XE2 on Windows 7:
#include "URecycleBinStuff.hpp"
#include <ComObj.hpp>
bool __fastcall CanFileBeDeletedToRecycleBin(const UnicodeString &AFileName)
{
DelphiInterface<IRecycleBinManager> RecycleBinManager;
OleCheck(CoCreateInstance(CLSID_RecycleBinManager, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IRecycleBinManager, (void**)&RecycleBinManager));
return (RecycleBinManager->WillRecycle(AFileName.c_str()) == S_OK);
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (CanFileBeDeletedToRecycleBin(L"C:\\test del"))
ShowMessage("Can be deleted"); // <-- code makes it here
else
ShowMessage("Can not be deleted");
}
Here is what the generated .hpp file looks like:
// CodeGear C++Builder
// Copyright (c) 1995, 2011 by Embarcadero Technologies, Inc.
// All rights reserved
// (DO NOT EDIT: machine generated header) 'URecycleBinStuff.pas' rev: 23.00 (Win32)
#ifndef UrecyclebinstuffHPP
#define UrecyclebinstuffHPP
#pragma delphiheader begin
#pragma option push
#pragma option -w- // All warnings off
#pragma option -Vx // Zero-length empty class member functions
#pragma pack(push,8)
#include <System.hpp> // Pascal unit
#include <SysInit.hpp> // Pascal unit
#include <Winapi.Windows.hpp> // Pascal unit
#include <Winapi.Messages.hpp> // Pascal unit
#include <Winapi.ShlObj.hpp> // Pascal unit
//-- user supplied -----------------------------------------------------------
namespace Urecyclebinstuff
{
//-- type declarations -------------------------------------------------------
#pragma option push -b-
enum tagRECYCLEBIN_TYPE : unsigned char { RBTYPE_VOLUME, RBTYPE_KNOWNFOLDER };
#pragma option pop
typedef tagRECYCLEBIN_TYPE TRecycleBinType;
struct tagDELETEDITEM;
typedef tagDELETEDITEM *PDeletedItem;
#pragma pack(push,1)
struct DECLSPEC_DRECORD tagDELETEDITEM
{
public:
unsigned dwFileSizeLow;
unsigned dwFileSizeHigh;
#pragma pack(push,8)
_FILETIME ftDeletionTime;
#pragma pack(pop)
System::StaticArray<System::WideChar, 260> szOriginalPath;
System::StaticArray<System::WideChar, 260> szDisplacedPath;
};
#pragma pack(pop)
typedef tagDELETEDITEM TDeletedItem;
__interface IEnumRecycleItems;
typedef System::DelphiInterface<IEnumRecycleItems> _di_IEnumRecycleItems;
__interface INTERFACE_UUID("{6E325F88-D12F-49E5-895B-8EC98630C021}") IEnumRecycleItems : public System::IInterface
{
public:
virtual HRESULT __stdcall Next(unsigned celt, /* out */ tagDELETEDITEM &rgelt, unsigned &pceltFetched) = 0 ;
virtual HRESULT __stdcall Skip(unsigned celt) = 0 ;
virtual HRESULT __stdcall Reset(void) = 0 ;
virtual HRESULT __stdcall Clone(/* out */ _di_IEnumRecycleItems &ppenum) = 0 ;
};
__interface IRecycle;
typedef System::DelphiInterface<IRecycle> _di_IRecycle;
__interface INTERFACE_UUID("{0125E62F-8349-443A-854B-A55FB84CFA35}") IRecycle : public System::IInterface
{
public:
virtual HRESULT __stdcall Compact(void) = 0 ;
virtual HRESULT __stdcall GetFileData(const System::WideChar * pszPath, /* out */ tagDELETEDITEM &lpData) = 0 ;
virtual HRESULT __stdcall GetItemCount(/* out */ __int64 &lpCount) = 0 ;
virtual HRESULT __stdcall GetUsedSpace(/* out */ __int64 &lpUsedSpace) = 0 ;
virtual HRESULT __stdcall IsEmpty(void) = 0 ;
virtual HRESULT __stdcall PurgeAll(_di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall PurgeItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall SuspendUpdating(BOOL fSuspend) = 0 ;
virtual HRESULT __stdcall RecycleItem(const System::WideChar * lpstrItem, const unsigned dwAttrs, const __int64 iFileSize, /* out */ _di_IShellItem &psi) = 0 ;
virtual HRESULT __stdcall RestoreItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall IsRecycled(const System::WideChar * pszPath, PBOOL lpRecycled) = 0 ;
virtual HRESULT __stdcall EnumItems(unsigned dwFlags, /* out */ _di_IEnumRecycleItems &EnumRecycleItems) = 0 ;
virtual HRESULT __stdcall WillRecycle(const System::WideChar * pszPath) = 0 ;
};
__interface IRecycleBin;
typedef System::DelphiInterface<IRecycleBin> _di_IRecycleBin;
__interface INTERFACE_UUID("{F964AD97-96F4-48AB-B444-E8588BC7C7B3}") IRecycleBin : public System::IInterface
{
public:
virtual HRESULT __stdcall Compact(void) = 0 ;
virtual HRESULT __stdcall GetFileData(const System::WideChar * pszPath, /* out */ tagDELETEDITEM &lpData) = 0 ;
virtual HRESULT __stdcall GetItemCount(/* out */ __int64 &lpCount) = 0 ;
virtual HRESULT __stdcall GetUsedSpace(/* out */ __int64 &lpUsedSpace) = 0 ;
virtual HRESULT __stdcall IsEmpty(void) = 0 ;
virtual HRESULT __stdcall PurgeAll(_di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall PurgeItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall SuspendUpdating(BOOL fSuspend) = 0 ;
virtual HRESULT __stdcall RecycleItem(const System::WideChar * lpstrItem, const unsigned dwAttrs, const __int64 iFileSize, /* out */ _di_IShellItem &psi) = 0 ;
virtual HRESULT __stdcall RestoreItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall IsRecycled(const System::WideChar * pszPath, PBOOL lpRecycled) = 0 ;
virtual HRESULT __stdcall EnumItems(unsigned dwFlags, /* out */ _di_IEnumRecycleItems &EnumRecycleItems) = 0 ;
virtual HRESULT __stdcall WillRecycle(const System::WideChar * pszPath) = 0 ;
virtual HRESULT __stdcall Initialize(const tagRECYCLEBIN_TYPE rbType, const System::WideChar * pszID) = 0 ;
virtual HRESULT __stdcall GetTypeID(/* out */ tagRECYCLEBIN_TYPE &rbType, System::WideChar * &pszID) = 0 ;
virtual HRESULT __stdcall GetIDList(/* out */ Winapi::Shlobj::PItemIDList &ppidl) = 0 ;
virtual HRESULT __stdcall GetLocation(System::WideChar * pszPathBuffer, unsigned cchMax) = 0 ;
virtual HRESULT __stdcall GetMaxCapacityRange(/* out */ __int64 &lpMin, /* out */ __int64 &lpMax) = 0 ;
virtual HRESULT __stdcall GetMaxCapacity(/* out */ __int64 &lpCapacity) = 0 ;
virtual HRESULT __stdcall SetMaxCapacity(const __int64 lpCapacity) = 0 ;
virtual HRESULT __stdcall GetPurgeOnDelete(/* out */ BOOL &fNukeOnDelete) = 0 ;
virtual HRESULT __stdcall SetPurgeOnDelete(const BOOL fNukeOnDelete) = 0 ;
};
__interface IRecycleBinManager;
typedef System::DelphiInterface<IRecycleBinManager> _di_IRecycleBinManager;
__interface INTERFACE_UUID("{5869092D-8AF9-4A6C-AE84-1F03BE2246CC}") IRecycleBinManager : public System::IInterface
{
public:
virtual HRESULT __stdcall Compact(void) = 0 ;
virtual HRESULT __stdcall GetFileData(const System::WideChar * pszPath, /* out */ tagDELETEDITEM &lpData) = 0 ;
virtual HRESULT __stdcall GetItemCount(/* out */ __int64 &lpCount) = 0 ;
virtual HRESULT __stdcall GetUsedSpace(/* out */ __int64 &lpUsedSpace) = 0 ;
virtual HRESULT __stdcall IsEmpty(void) = 0 ;
virtual HRESULT __stdcall PurgeAll(_di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall PurgeItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall SuspendUpdating(BOOL fSuspend) = 0 ;
virtual HRESULT __stdcall RecycleItem(const System::WideChar * lpstrItem, const unsigned dwAttrs, const __int64 iFileSize, /* out */ _di_IShellItem &psi) = 0 ;
virtual HRESULT __stdcall RestoreItems(const System::WideChar * lpstrItems, _di_IFileOperation pfo) = 0 ;
virtual HRESULT __stdcall IsRecycled(const System::WideChar * pszPath, PBOOL lpRecycled) = 0 ;
virtual HRESULT __stdcall EnumItems(unsigned dwFlags, /* out */ _di_IEnumRecycleItems &EnumRecycleItems) = 0 ;
virtual HRESULT __stdcall WillRecycle(const System::WideChar * pszPath) = 0 ;
virtual HRESULT __stdcall DelayCompaction(const BOOL fDelay) = 0 ;
virtual HRESULT __stdcall GetRecycleBinCount(/* out */ int &iCount) = 0 ;
virtual HRESULT __stdcall GetRecycleBinAt(const int index, const GUID &iid, /* out */ void *ppv) = 0 ;
virtual HRESULT __stdcall GetRecycleBin(const System::WideChar * pszPath, const GUID &iid, /* out */ void *ppv) = 0 ;
virtual HRESULT __stdcall Refresh(void) = 0 ;
};
//-- var, const, procedure ---------------------------------------------------
extern PACKAGE GUID IID_IEnumRecycleItems;
extern PACKAGE GUID IID_IRecycle;
extern PACKAGE GUID IID_IRecycleBin;
extern PACKAGE GUID IID_IRecycleBinManager;
extern PACKAGE GUID CLSID_RecycleBinManager;
} /* namespace Urecyclebinstuff */
#if !defined(DELPHIHEADER_NO_IMPLICIT_NAMESPACE_USE) && !defined(NO_USING_NAMESPACE_URECYCLEBINSTUFF)
using namespace Urecyclebinstuff;
#endif
#pragma pack(pop)
#pragma option pop
#pragma delphiheader end.
//-- end unit ----------------------------------------------------------------
#endif // UrecyclebinstuffHPP
If you compare the .hpp file's IRecycleBinManager to your manual IRecycleBinManager, there are some discrepancies in your code. Such as, you have the RecycleItem() and IsRecycled() methods named as PurgeItems() and RestoreItems() instead, and you have two RestoreItems() methods.