遇到一个api的接口实现问题 - C++ Builder / Windows SDK/API
我正在搞WSD编成,遇到一个接口 IWSDiscoveryProviderNotify Interface , 具体联结在这里
http://msdn.microsoft.com/en-us/library/aa386013(VS.85).aspx
现在我需要在我的程序里实现这个接口,但是不知道应该怎样实现.
下面是我的代码
- C/C++ code
// test_disco.cpp : Defines the entry point for the console application.//#pragma once#include "stdafx.h"#include "PrinterServiceTypeTypes.h"class myDiscoveryProviderNotify : public IWSDiscoveryProviderNotify{public: myDiscoveryProviderNotify();};void print_result(HRESULT hr){ if( S_OK == hr ) { _cwprintf(L"[S_OK]\r\n"); } else { _cwprintf(L"[ERROR: %x]\r\n", hr); }}int _tmain(int argc, _TCHAR* argv[]){ HRESULT hr = S_OK; HRESULT hr_IWSDXMLContext = S_OK; HRESULT hr_SetAddressFamily = S_OK; HRESULT hr_SearchByType = S_OK; HRESULT hr_ppProviderNotify = S_OK; HRESULT hr_pSink = S_OK; IWSDiscoveryProvider * pProvider = NULL; myDiscoveryProviderNotify pSink ; IWSDXMLContext * pContext = NULL ; LPCWSTR pszTag = new TCHAR[1024]; wsprintf((LPWSTR)pszTag,L"%s", "Simple Sample"); LPCWSTR pszMatchBy = NULL; WSD_NAME_LIST wsd_list = {NULL , Names_Print}; DWORD dwError = NULL; //create a interface xmlcontext //hr_IWSDXMLContext = WSDXMLCreateContext(&pContext); if( S_OK == hr_IWSDXMLContext ){ cout<<"hr_IWSDXMLContext "<<endl; print_result(hr_IWSDXMLContext); //create a interface provider hr = WSDCreateDiscoveryProvider (pContext,&pProvider); if(pProvider==NULL) cout<<"\n ppProvider null"<<endl; hr_pSink = pProvider->Attach(&pSink); if( S_OK == hr ){ cout<<"hr_WSDCreateDiscoveryProvider "<<endl; print_result(hr); //cout << WSDAPI_ADDRESSFAMILY_IPV4 << endl; hr_SetAddressFamily = pProvider->SetAddressFamily(WSDAPI_ADDRESSFAMILY_IPV4); cout<<"\n hr_SetAddressFamily "<<endl; print_result(hr_SetAddressFamily); if(S_OK == hr_SetAddressFamily){ for(int i=0 ; i<50 ;i++){ Sleep(5000); if(S_OK != hr_pSink){ DWORD dwError = GetLastError(); cout<<"\n\n hr_pSink not OK\n"<<endl; printf(" %lu \n",dwError); } hr_SearchByType = pProvider->SearchByType(&wsd_list,NULL,NULL,NULL ); //cout << "\n pszMatchBy: \n" << pszMatchBy << endl; //cout << "\n pszTag :\n" << pszTag << endl; if(S_OK == hr_SearchByType){ // cout<<"\n hr_SearchByType "<<endl; // print_result(hr_SearchByType); //hr_ppProviderNotify = pSink->SearchComplete(pszTag); dwError = GetLastError(); cout<<"\n\n hr_SearchByType OK \n"<<endl; printf(" %lu \n",dwError); } else{ dwError = GetLastError(); cout<<"\n\n hr_SearchByType not OK\n"<<endl; printf(" %lu \n",dwError); //print_result(hr_SearchByType); //hr_ppProviderNotify = pSink->SearchFailed(hr_SearchByType,NULL); dwError = GetLastError(); cout<<"\n\n hr_ppProviderNotify not OK \n"<<endl; printf(" %lu \n",dwError); } } } else cout<<"\n hr_SetAddressFamily echouer "<<endl; } else{ cout<<"hr_WSDCreateDiscoveryProvider "<<endl; print_result(hr); } } else{ print_result(hr_IWSDXMLContext); cout<<"hr_IWSDXMLContext "<<endl; } //WSDFreeLinkedMemory(ppProvider); //cout<<"WSDCreateDiscoveryProvider cleared"<<endl; return 0;}
大家帮忙看看怎么回事 , 在vs2008里编译后出错
error C2259: 'myDiscoveryProviderNotify' : cannot instantiate abstract class
[解决办法]
下面是一个借口实现例子。接口IMyInterFace只有一个方法,IUnknown有3个,都得具体实现:
- C/C++ code
DEFINE_GUID(IID_IMyInterFace, 0xD7352C6F, 0x3C8D, 0x4F93, 0x91, 0xE3, 0x19, 0x74, 0xD0, 0x7D, 0xD5, 0x64);MIDL_INTERFACE("D7352C6F-3C8D-4F93-91E3-1974D07DD564")IMyInterFace : public IUnknown{public: virtual int STDMETHODCALLTYPE IntAdd(int x) = 0;};class MyObject : public IMyInterFace{ int RefCount;public: MyObject(){ RefCount = 0; } ~MyObject() { ShowMessage("Destroy"); } virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) { if (riid == IID_IMyInterFace || riid == IID_IUnknown) { *ppvObject = this; AddRef(); return S_OK; } ppvObject = 0; return E_NOINTERFACE; } virtual ULONG STDMETHODCALLTYPE AddRef(void) { RefCount ++; return RefCount; } virtual ULONG STDMETHODCALLTYPE Release( void) { RefCount --; if (RefCount == 0) delete this; return RefCount; } virtual int STDMETHODCALLTYPE IntAdd(int x) { return x * x; }};