client2b.cpp
来自「VC++串口通信设。本书详细说明讲解了在VC++环境下编写串口通信得过程。值得一」· C++ 代码 · 共 60 行
CPP
60 行
#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 + =
减小字号Ctrl + -
显示快捷键?