summaryrefslogtreecommitdiff
path: root/shared/com_error.h
blob: ab3659777089fcbbcc0763935ec038d335c65eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2010 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************
//
#ifndef COM_ERROR_HEADER
#define COM_ERROR_HEADER

#include <string>
#include <cstdio>
#include <comdef.h>

namespace util
{
std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr);


class ComException
{
public:
    ComException(const std::wstring& message, HRESULT hr = NO_ERROR)
        : message_(message), hr_(hr) {}

    std::wstring show() const
    {
        return hr_ != NO_ERROR ? generateErrorMsg(message_, hr_) : message_;
    }

private:
    const std::wstring message_;
    const HRESULT hr_;
};
 




















//################# implementation #####################
inline
std::wstring numberToHexString(long number)
{
    wchar_t result[100];
    swprintf(result, 100, L"0x%08x", number);
    return std::wstring(result);
}


inline
std::wstring generateErrorMsg(const std::wstring& input, HRESULT hr)
{
    std::wstring output(input);
    output += L" (";
    output += numberToHexString(hr);
    output += L": ";
    output += _com_error(hr).ErrorMessage();
    output += L")";
    return output;
}
}
#endif //COM_ERROR_HEADER
bgstack15