URLMoniker用法

// ConsoleApplication7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#pragma comment (lib, "urlmon.lib")


class CMarshaller2 : public IBindStatusCallback
{


	LONG _ref_count;
	

public:

	CMarshaller2() : _ref_count(1)
	{

	}
	~CMarshaller2() {}


	STDMETHOD(OnStartBinding)(
		DWORD dwReserved,
		IBinding __RPC_FAR *pib)
	{
		return S_OK;;
	}

	STDMETHOD(GetPriority)(
		LONG __RPC_FAR *pnPriority)
	{
		return S_OK;;
	}

	STDMETHOD(OnLowResource)(
		DWORD reserved)
	{
		return S_OK;;
	}

	STDMETHOD(OnProgress)(
		ULONG ulProgress,
		ULONG ulProgressMax,
		ULONG ulStatusCode,
		LPCWSTR wszStatusText)
	{
		return S_OK;;
	}
	STDMETHOD(OnStopBinding)(
		HRESULT hresult,
		LPCWSTR szError)
	{
		return S_OK;;
	}

	STDMETHOD(GetBindInfo)(
		DWORD __RPC_FAR *pgrfBINDF,
		BINDINFO __RPC_FAR *pbindInfo)
	{
		//*pgrfBINDF = BINDF_ASYNCHRONOUS | BINDF_ASYNCSTORAGE | BINDF_GETNEWESTVERSION; // bind asynchronously
		//pbindInfo->cbSize = sizeof(BINDINFO);
		//pbindInfo->szExtraInfo = NULL;
		//memset(&pbindInfo->stgmedData, 0, sizeof(STGMEDIUM));
		//pbindInfo->grfBindInfoF = 0;
		//pbindInfo->dwBindVerb = BINDVERB_GET;
		//pbindInfo->szCustomVerb = NULL;
		return S_OK;
	}

	STDMETHOD(OnDataAvailable)(
		DWORD grfBSCF,
		DWORD dwSize,
		FORMATETC __RPC_FAR *pformatetc,
		STGMEDIUM __RPC_FAR *pstgmed)
	{
		return S_OK;;
	}

	STDMETHOD(OnObjectAvailable)(
		REFIID riid,
		IUnknown __RPC_FAR *punk)
	{
		return S_OK;;
	}

	STDMETHOD_(ULONG, AddRef)()
	{
		return InterlockedIncrement(&_ref_count);
	}

	STDMETHOD_(ULONG, Release)()
	{
		printf("Release: %d\n", _ref_count);
		ULONG ret = InterlockedDecrement(&_ref_count);
		if (ret == 0)
		{
			printf("Release object %p\n", this);
			delete this;
		}
		return ret;
	}

	STDMETHOD(QueryInterface)(
		REFIID riid,
		void __RPC_FAR *__RPC_FAR *ppv)
	{
		*ppv = NULL;
		if (riid == IID_IUnknown || riid == IID_IBindStatusCallback)
		{
			*ppv = this;
			AddRef();
			return S_OK;
		}
		

		return E_NOINTERFACE;
	}


};

int wmain(int argc, wchar_t** argv)
{

	IMoniker* pMoniker;

	CMarshaller2* pBSC = new CMarshaller2();

	
	CLSID iid = IID_IStream;
	IStream* pStream = nullptr;

	HRESULT hResult;
	IBindCtx* pNewBindCtx=nullptr;




	

	hResult = CreateURLMoniker(NULL, L"http://localhost:50001/Index.html", &pMoniker);
	if (FAILED(hResult))
	{
		return(hResult);
	}

	hResult = CreateBindCtx(0, &pNewBindCtx);
	if (FAILED(hResult))
	{
		return(hResult);
	}
	//向绑定环境注册一个回调接口IBindStatusCallback,CCuteMoniker派生自接口IBindStatusCallback。
	hResult = RegisterBindStatusCallback(pNewBindCtx, static_cast<IBindStatusCallback*>(pBSC), 0, 0L);
	
	if (FAILED(hResult))
	{
		return(hResult);
	}

	hResult = pMoniker->BindToStorage(pNewBindCtx, 0, IID_IStream, (void**)&pStream);
	if (FAILED(hResult))
	{
		return(hResult);
	}

	return hResult;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值