📄 client2b.cpp
字号:
#include <iostream>
#define INITGUID
#include "Utility.h"
#include "IFortune.h"
int main()
{
HRESULT hResult;
IFortuneTeller *pIFortuneTeller;
BSTR bstrFortune;
LPSTR pszFortune;
cout << "Fortune2 - Simple COM Fortune Client Program\n";
// Prepare COM for use
VerboseMsg("Calling COM API CoInitialize.\n");
hResult = CoInitialize(NULL);
if (FAILED(hResult)) {
ReportError("Could not initialize COM.", hResult);
return 1;
}
// Create a new FortuneTeller object and return an IFortuneTeller
// interface pointer
VerboseMsg("Creating new FortuneTeller object instance.\n");
hResult = CoCreateInstance(CLSID_FortuneTeller2,
NULL, CLSCTX_INPROC_SERVER,
IID_IFortuneTeller,
(PPVOID) &pIFortuneTeller);
if (FAILED(hResult)) {
ReportError("Could not create a new FortuneTeller object.", hResult);
return 1;
}
// Get our fortune, in BSTR form
VerboseMsg("Getting fortune from FortuneTeller object.\n");
pIFortuneTeller->GetFortune(&bstrFortune);
// Convert the fortune into a regular string
VerboseMsg("Translating fortune from Unicode to ANSI text.\n");
UnicodeToAnsi(bstrFortune, &pszFortune);
// Show the user the fortune
cout << "\"" << pszFortune << "\"" << endl;
// Release the memory for both the ANSI string and the BSTR
VerboseMsg("Releasing fortune and BSTR strings.\n");
CoTaskMemFree(pszFortune);
SysFreeString(bstrFortune);
// We're done with the FortuneTeller object
VerboseMsg("Releasing FortuneTeller object.\n");
pIFortuneTeller->Release();
pIFortuneTeller = NULL;
// We're all done with COM
CoUninitialize();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -