aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/inc/dutil.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/dutil/inc/dutil.h')
-rw-r--r--src/dutil/inc/dutil.h160
1 files changed, 160 insertions, 0 deletions
diff --git a/src/dutil/inc/dutil.h b/src/dutil/inc/dutil.h
new file mode 100644
index 00000000..3791dd5a
--- /dev/null
+++ b/src/dutil/inc/dutil.h
@@ -0,0 +1,160 @@
1#pragma once
2// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
3
4
5#define DAPI __stdcall
6#define DAPIV __cdecl // used only for functions taking variable length arguments
7
8#define DAPI_(type) EXTERN_C type DAPI
9#define DAPIV_(type) EXTERN_C type DAPIV
10
11
12// enums
13typedef enum REPORT_LEVEL
14{
15 REPORT_NONE, // turns off report (only valid for XXXSetLevel())
16 REPORT_WARNING, // written if want only warnings or reporting is on in general
17 REPORT_STANDARD, // written if reporting is on
18 REPORT_VERBOSE, // written only if verbose reporting is on
19 REPORT_DEBUG, // reporting useful when debugging code
20 REPORT_ERROR, // always gets reported, but can never be specified
21} REPORT_LEVEL;
22
23// asserts and traces
24typedef BOOL (DAPI *DUTIL_ASSERTDISPLAYFUNCTION)(__in_z LPCSTR sz);
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30void DAPI Dutil_SetAssertModule(__in HMODULE hAssertModule);
31void DAPI Dutil_SetAssertDisplayFunction(__in DUTIL_ASSERTDISPLAYFUNCTION pfn);
32void DAPI Dutil_Assert(__in_z LPCSTR szFile, __in int iLine);
33void DAPI Dutil_AssertSz(__in_z LPCSTR szFile, __in int iLine, __in_z LPCSTR szMessage);
34
35void DAPI Dutil_TraceSetLevel(__in REPORT_LEVEL ll, __in BOOL fTraceFilenames);
36REPORT_LEVEL DAPI Dutil_TraceGetLevel();
37void __cdecl Dutil_Trace(__in_z LPCSTR szFile, __in int iLine, __in REPORT_LEVEL rl, __in_z __format_string LPCSTR szMessage, ...);
38void __cdecl Dutil_TraceError(__in_z LPCSTR szFile, __in int iLine, __in REPORT_LEVEL rl, __in HRESULT hr, __in_z __format_string LPCSTR szMessage, ...);
39void DAPI Dutil_RootFailure(__in_z LPCSTR szFile, __in int iLine, __in HRESULT hrError);
40
41#ifdef __cplusplus
42}
43#endif
44
45
46#ifdef DEBUG
47
48#define AssertSetModule(m) (void)Dutil_SetAssertModule(m)
49#define AssertSetDisplayFunction(pfn) (void)Dutil_SetAssertDisplayFunction(pfn)
50#define Assert(f) ((f) ? (void)0 : (void)Dutil_Assert(__FILE__, __LINE__))
51#define AssertSz(f, sz) ((f) ? (void)0 : (void)Dutil_AssertSz(__FILE__, __LINE__, sz))
52
53#define TraceSetLevel(l, f) (void)Dutil_TraceSetLevel(l, f)
54#define TraceGetLevel() (REPORT_LEVEL)Dutil_TraceGetLevel()
55#define Trace(l, f, ...) (void)Dutil_Trace(__FILE__, __LINE__, l, f, __VA_ARGS__)
56#define TraceError(x, f, ...) (void)Dutil_TraceError(__FILE__, __LINE__, REPORT_ERROR, x, f, __VA_ARGS__)
57#define TraceErrorDebug(x, f, ...) (void)Dutil_TraceError(__FILE__, __LINE__, REPORT_DEBUG, x, f, __VA_ARGS__)
58
59#else // !DEBUG
60
61#define AssertSetModule(m)
62#define AssertSetDisplayFunction(pfn)
63#define Assert(f)
64#define AssertSz(f, sz)
65
66#define TraceSetLevel(l, f)
67#define Trace(l, f, ...)
68#define TraceError(x, f, ...)
69#define TraceErrorDebug(x, f, ...)
70
71#endif // DEBUG
72
73// ExitTrace can be overriden
74#ifndef ExitTrace
75#define ExitTrace TraceError
76#endif
77
78// Exit macros
79#define ExitFunction() { goto LExit; }
80#define ExitFunction1(x) { x; goto LExit; }
81
82#define ExitFunctionWithLastError(x) { x = HRESULT_FROM_WIN32(::GetLastError()); goto LExit; }
83
84#define ExitOnLastError(x, s, ...) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (FAILED(x)) { Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; } }
85#define ExitOnLastErrorDebugTrace(x, s, ...) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (FAILED(x)) { Dutil_RootFailure(__FILE__, __LINE__, x); TraceErrorDebug(x, s, __VA_ARGS__); goto LExit; } }
86#define ExitWithLastError(x, s, ...) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (!FAILED(x)) { x = E_FAIL; } Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; }
87#define ExitOnFailure(x, s, ...) if (FAILED(x)) { ExitTrace(x, s, __VA_ARGS__); goto LExit; }
88#define ExitOnRootFailure(x, s, ...) if (FAILED(x)) { Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; }
89#define ExitOnFailureDebugTrace(x, s, ...) if (FAILED(x)) { TraceErrorDebug(x, s, __VA_ARGS__); goto LExit; }
90#define ExitOnNull(p, x, e, s, ...) if (NULL == p) { x = e; Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; }
91#define ExitOnNullWithLastError(p, x, s, ...) if (NULL == p) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (!FAILED(x)) { x = E_FAIL; } Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; }
92#define ExitOnNullDebugTrace(p, x, e, s, ...) if (NULL == p) { x = e; Dutil_RootFailure(__FILE__, __LINE__, x); TraceErrorDebug(x, s, __VA_ARGS__); goto LExit; }
93#define ExitOnInvalidHandleWithLastError(p, x, s, ...) if (INVALID_HANDLE_VALUE == p) { DWORD Dutil_er = ::GetLastError(); x = HRESULT_FROM_WIN32(Dutil_er); if (!FAILED(x)) { x = E_FAIL; } Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; }
94#define ExitOnWin32Error(e, x, s, ...) if (ERROR_SUCCESS != e) { x = HRESULT_FROM_WIN32(e); if (!FAILED(x)) { x = E_FAIL; } Dutil_RootFailure(__FILE__, __LINE__, x); ExitTrace(x, s, __VA_ARGS__); goto LExit; }
95
96// release macros
97#define ReleaseObject(x) if (x) { x->Release(); }
98#define ReleaseObjectArray(prg, cel) if (prg) { for (DWORD Dutil_ReleaseObjectArrayIndex = 0; Dutil_ReleaseObjectArrayIndex < cel; ++Dutil_ReleaseObjectArrayIndex) { ReleaseObject(prg[Dutil_ReleaseObjectArrayIndex]); } ReleaseMem(prg); }
99#define ReleaseVariant(x) { ::VariantClear(&x); }
100#define ReleaseNullObject(x) if (x) { (x)->Release(); x = NULL; }
101#define ReleaseCertificate(x) if (x) { ::CertFreeCertificateContext(x); x=NULL; }
102#define ReleaseHandle(x) if (x) { ::CloseHandle(x); x = NULL; }
103
104
105// useful defines and macros
106#define Unused(x) ((void)x)
107
108#ifndef countof
109#if 1
110#define countof(ary) (sizeof(ary) / sizeof(ary[0]))
111#else
112#ifndef __cplusplus
113#define countof(ary) (sizeof(ary) / sizeof(ary[0]))
114#else
115template<typename T> static char countofVerify(void const *, T) throw() { return 0; }
116template<typename T> static void countofVerify(T *const, T *const *) throw() {};
117#define countof(arr) (sizeof(countofVerify(arr,&(arr))) * sizeof(arr)/sizeof(*(arr)))
118#endif
119#endif
120#endif
121
122#define roundup(x, n) roundup_typed(x, n, DWORD)
123#define roundup_typed(x, n, t) (((t)(x) + ((t)(n) - 1)) & ~((t)(n) - 1))
124
125#define HRESULT_FROM_RPC(x) ((HRESULT) ((x) | FACILITY_RPC))
126
127#ifndef MAXSIZE_T
128#define MAXSIZE_T ((SIZE_T)~((SIZE_T)0))
129#endif
130
131typedef const BYTE* LPCBYTE;
132
133#define E_FILENOTFOUND HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)
134#define E_PATHNOTFOUND HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND)
135#define E_INVALIDDATA HRESULT_FROM_WIN32(ERROR_INVALID_DATA)
136#define E_INVALIDSTATE HRESULT_FROM_WIN32(ERROR_INVALID_STATE)
137#define E_INSUFFICIENT_BUFFER HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)
138#define E_MOREDATA HRESULT_FROM_WIN32(ERROR_MORE_DATA)
139#define E_NOMOREITEMS HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS)
140#define E_NOTFOUND HRESULT_FROM_WIN32(ERROR_NOT_FOUND)
141#define E_MODNOTFOUND HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND)
142#define E_BADCONFIGURATION HRESULT_FROM_WIN32(ERROR_BAD_CONFIGURATION)
143
144#define AddRefAndRelease(x) { x->AddRef(); x->Release(); }
145
146#define MAKEDWORD(lo, hi) ((DWORD)MAKELONG(lo, hi))
147#define MAKEQWORDVERSION(mj, mi, b, r) (((DWORD64)MAKELONG(r, b)) | (((DWORD64)MAKELONG(mi, mj)) << 32))
148
149
150#ifdef __cplusplus
151extern "C" {
152#endif
153
154// other functions
155HRESULT DAPI LoadSystemLibrary(__in_z LPCWSTR wzModuleName, __out HMODULE *phModule);
156HRESULT DAPI LoadSystemLibraryWithPath(__in_z LPCWSTR wzModuleName, __out HMODULE *phModule, __deref_out_z_opt LPWSTR* psczPath);
157
158#ifdef __cplusplus
159}
160#endif