OLE接口用法

转载请标明是引用于 http://blog.csdn.net/chenyujing1234

 例子代码:(编译工具:VS2005)

http://www.rayfile.com/zh-cn/files/4468c85c-7a4e-11e1-8329-0015c55db73d/

 

// 第一步得初始化COM环境
	HRESULT hr = CoInitialize(NULL);
	if (FAILED(hr)) 
	{
		return -3;
	}

// 第二步是获得接口 IDccMan
	hr = CoCreateInstance(CLSID_DccMan, NULL, CLSCTX_SERVER, IID_IDccMan, (LPVOID*)&g_pDccMan);
	if (FAILED(hr)) 
	{
		
		return -1;
	}

 


 

	// Create new notification object.
	g_pDanSink = new DanDccSink(hWnd, g_pDccMan);
	if (g_pDanSink)
	{
		// 第三步: 查询IDccManSink的接口
		g_pDanSink->QueryInterface(IID_IDccManSink, (void **)&pdms);

		// 这里调用后会自动调用OnLogActive函数
		g_pDccMan->Advise(pdms, &g_Context);
	}


 

g_pDanSink->QueryInterface(IID_IDccManSink, (void **)&pdms);,

1、会调用DanDccSink::QueryInterface,

STDMETHODIMP DanDccSink::QueryInterface(REFIID riid, LPVOID * ppvObj) 
{
	if (IID_IUnknown == riid || IID_IDccManSink == riid)
	{
		*ppvObj = (IDccManSink*)this;
	}
	else 
	{
		*ppvObj = NULL;
		return E_NOINTERFACE;
	}
	AddRef();

	return NO_ERROR;
}

它返回了(IDccManSink*)this;

 

 

// 第三步:这里调用后会自动调用OnLogActive函数
g_pDccMan->Advise(pdms, &g_Context);

1、它会先调用

STDMETHODIMP_(ULONG) DanDccSink::AddRef(THIS) 
{
	return (ULONG)InterlockedIncrement(&m_lRef);
}



 

2、然后又调用了一次QueryInterface,不过是返回了空接口.

3、调用调用

//
// The connection manager calls OnLogActive to indicate that the connection
// between the PC and the device is up and fully operational.
//
STDMETHODIMP DanDccSink::OnLogActiveC()
{

	return NO_ERROR;
}


4、然后又调用了一次QueryInterface,不过是返回了空接口.
5、然后调用

//
// When a connection is established, the connection manager calls OnLogIpAddr
// to notify you of the IP address of the connected device.
//
STDMETHODIMP DanDccSink::OnLogIpAddr(DWORD dwIpAddr) 
{
	TCHAR szIP[30];
	_stprintf_s(szIP, 30, TEXT("OnLogIpAddr: %02d.%02d.%02d.%02d"),
		(dwIpAddr & 0x000000ff),     (dwIpAddr & 0x0000ff00)>>8, 
		(dwIpAddr & 0x00ff0000)>>16, dwIpAddr>>24);

	//SendMessage(g_hwndStatusBar, SB_SETTEXT, (WPARAM)0, (LPARAM)szIP);

	return NO_ERROR;
}


下午很累了,具体原理下次补充.

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* cole - A free C OLE library. cole_extract - Extract a file from a filesystem. Copyright 1998, 1999 Roberto Arturo Tena Sanchez This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* Arturo Tena */ #include /* To compile this file outside cole source tree, you must include here instead */ #include "cole.h" #define PRGNAME "iOLE" #define BUFFER_SIZE 128 int main(int argc, char **argv) { COLEFS *cfs; COLEFILE *cf; COLERRNO colerrno; char buffer[BUFFER_SIZE]; size_t char_read; if (argc != 3) { fprintf(stderr, "cole_extract. Extract a file from a " "filesystem to the standard output.\n" "Usage: "PRGNAME" \n" " FILE - File with the filesystem.\n" " INFILE - Filename of the file to extract.\n"); exit(1); } printf("%s",argv[1]); cfs = cole_mount(argv[1], &colerrno); if (cfs == NULL) { cole_perror(PRGNAME, colerrno); exit(1); } cf = cole_fopen(cfs, argv[2], &colerrno); if (cf == NULL) { cole_perror(PRGNAME, colerrno); cole_umount(cfs, NULL); exit(1); } while ((char_read = cole_fread(cf, buffer, BUFFER_SIZE, &colerrno))) { if (fwrite(buffer, 1, char_read, stdout) != char_read) { break; } } if (!cole_feof(cf)) { cole_perror(PRGNAME, colerrno); cole_umount(cfs, NULL); exit(1); } if (cole_fclose(

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值