Created
February 14, 2022 02:22
-
-
Save bg1bgst333/b90f73e98b428e8b5c57c32ef5d4ce8e to your computer and use it in GitHub Desktop.
IWbemLocator#IWbemLocator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ヘッダファイルのインクルード | |
#define _WIN32_DCOM // DCOMの定義 | |
#include <windows.h> // 標準WindowsAPI | |
#include <tchar.h> // TCHAR対応 | |
#include <stdio.h> // C標準入出力 | |
#include <objbase.h> // COM | |
#include <wbemidl.h> // WBEM | |
// _tmain関数の定義 | |
int _tmain(int argc, TCHAR *argv[]){ // main関数のTCHAR版. | |
// 変数の宣言と初期化. | |
HRESULT hr; // COMの初期化の結果を格納するhr. | |
HRESULT hr2; // セキュリティ初期化の結果を格納するhr2. | |
HRESULT hr3; // IWbemLocatorの生成結果を格納するhr3. | |
IWbemLocator *pLocator = NULL; // IWbemLocatorインターフェースポインタpLocatorをNULLで初期化. | |
// MTA(マルチスレッドアパートメント)に属するように設定. | |
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED); // CoInitializeExにCOINIT_MULTITHREADEDを指定して初期化. | |
if (FAILED(hr)){ // FAILEDマクロで判定. | |
_tprintf(_T("FAILED!\n")); // "FAILED!"と出力. | |
return -1; // -1を返して異常終了. | |
} | |
// セキュリティ初期化. | |
hr2 = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL); // CoInitializeSecurityでセキュリティ初期化. | |
if (FAILED(hr2)){ // FAILEDマクロで判定. | |
_tprintf(_T("FAILED!\n")); // "FAILED!"と出力. | |
CoUninitialize(); // CoUninitializeで終了処理. | |
return -2; // -2を返して異常終了. | |
} | |
// IWbemLocatorインターフェースの生成. | |
hr3 = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLocator); // CoCreateInstanceで, IWbemLocatorインターフェースを生成. | |
if (FAILED(hr3)){ // FAILEDマクロで判定. | |
_tprintf(_T("FAILED!\n")); // "FAILED!"と出力. | |
CoUninitialize(); // CoUninitializeで終了処理. | |
return -3; // -3を返して異常終了. | |
} | |
// CoCreateInstanceは成功. | |
_tprintf(_T("CoCreateInstance OK!\n")); // "CoCreateInstance OK!"と出力. | |
// 取得したIWbemLocatorインターフェースポインタpLocatorを出力. | |
_tprintf(_T("pLocator = 0x%08x\n"), (unsigned long)pLocator); // pLocatorを出力. | |
// COMの終了処理. | |
pLocator->Release(); // IWbemLocator::Releaseで解放. | |
CoUninitialize(); // CoUninitializeで終了処理. | |
// プログラムの終了. | |
return 0; // 0を返して終了. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment