C++ com 组件的使用
// CommonTest.cpp : This file contains the ‘main‘ function. Program execution begins and ends there.
//
#include <iostream>
#include <atlbase.h>
#include "mbnapi.h"
int main()
{
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
{
return -1;
}
CComPtr<IMbnInterfaceManager> mbnInterfaceMgr = NULL;
hr = CoCreateInstance(CLSID_MbnInterfaceManager,NULL,CLSCTX_ALL,IID_IMbnInterfaceManager,(void**)&mbnInterfaceMgr);
if (FAILED(hr))
{
return -1;
}
SAFEARRAY* psa = NULL;
hr = mbnInterfaceMgr->GetInterfaces(&psa);
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
LONG lLower;
hr = SafeArrayGetLBound(psa, 1, &lLower);
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
LONG lUpper;
hr = SafeArrayGetUBound(psa, 1, &lUpper);
if (FAILED(hr))
{
CoUninitialize();
return -1;
}
CComPtr<IMbnInterface> pInf = NULL;
for (LONG l = lLower; l <= lUpper; l++)
{
hr = SafeArrayGetElement(psa, &l, (void*)(&pInf));
if (SUCCEEDED(hr))
{
CComPtr<IMbnSubscriberInformation> subscriberInf = NULL;
hr = pInf->GetSubscriberInformation(&subscriberInf);
if (SUCCEEDED(hr)) {
WCHAR tszID[256] = { L‘\0‘ };
hr = subscriberInf->get_SubscriberID((BSTR*)&tszID);
if (SUCCEEDED(hr)) {
std::wcout << L"SubscriberID: " << tszID <<std::endl;
}
}
}
}
SafeArrayDestroy(psa);
CoUninitialize();
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file