source: nscp/include/com_helpers.hpp @ d05c3f0

0.4.00.4.10.4.2
Last change on this file since d05c3f0 was d05c3f0, checked in by Michael Medin <michael@…>, 4 years ago

Next major "milestone" we now have one working plugin (CheckHelpers) which loads and works both on Linux and Windows.
This is (as always) a broken build which many many features disabled and non functional...

  • Property mode set to 100644
File size: 1.3 KB
Line 
1#pragma once
2
3#define _WIN32_DCOM
4#define _WIN32_WINNT 0x0400
5
6#include <error.hpp>
7#include <windows.h>
8#include <objbase.h>
9#include <atlbase.h>
10#include <atlsafe.h>
11
12namespace com_helper {
13
14        class com_exception {
15                std::wstring error_;
16                HRESULT result_;
17        public:
18                com_exception(std::wstring error) : error_(error) {}
19                com_exception(std::wstring error, HRESULT result) : error_(error), result_(result) {
20                        error_ += error::format::from_system(result);
21                }
22                std::wstring getMessage() {
23                        return error_;
24                }
25
26        };
27
28        class initialize_com {
29                bool isInitialized_;
30        public:
31                initialize_com() : isInitialized_(false) {}
32                ~initialize_com() {
33                        unInitialize();
34                }
35
36                void initialize() {
37                        HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
38                        if (FAILED(hRes))
39                                throw com_exception(_T("CoInitialize failed: "), hRes);
40                        isInitialized_ = true;
41                        hRes = CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_PKT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL);
42                        if (FAILED(hRes))
43                                throw com_exception(_T("CoInitializeSecurity failed: "), hRes);
44                }
45                void unInitialize() {
46                        if (!isInitialized_)
47                                return;
48                        CoUninitialize();
49                        isInitialized_ = false;
50                }
51                bool isInitialized() const {
52                        return isInitialized_;
53                }
54
55        };
56
57};
Note: See TracBrowser for help on using the repository browser.