-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhelpers.h
102 lines (88 loc) · 4.02 KB
/
helpers.h
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//---------------------------------------------------------------------------
// Helpers.h
//---------------------------------------------------------------------------
// General OLE Automation helper functions.
//---------------------------------------------------------------------------
/* Copyright (C) Microsoft Corporation, 1999. All rights reserved.
This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation. See these other
materials for detailed information regarding Microsoft code samples.
*/
#pragma once
//---------------------------------------------------------------------------
// To make the OLE-related code easier to read
//---------------------------------------------------------------------------
#define CHECK(x) { hr = (x); if (hr) { FAIL("Bad hr"); goto CleanUp; } }
#define CHECKSZ(x,sz) { hr = (x); if (hr) { FAIL(sz); goto CleanUp; } }
#define CHECKCL(x) { hr = (x); if (hr) { FAIL("Bad hr"); goto CheckLoop; } }
#define ARGSZ(f,sz) { if (f) { FAIL(sz); return E_INVALIDARG; } }
#define CHECKOS(x) { if (ERROR_SUCCESS!=(x)) { FAIL("Bad"); hr=E_FAIL; goto CleanUp; } }
#define RELEASEPTRTYPE(p,typ) \
{ \
if (p) \
{ \
typ *__punk = (p); \
(p) = NULL; \
__punk->Release(); \
} \
}
#define RELEASEPTR(p) RELEASEPTRTYPE(p,IUnknown)
//---------------------------------------------------------------------------
// Allocates a temporary buffer that will disappear when it goes out of scope
// NOTE: Be careful of that-- make sure you use the string in the same or
// nested scope in which you created this buffer. People should not use this
// class directly; use the macro(s) below.
//---------------------------------------------------------------------------
class TempBuffer
{
public:
TempBuffer(const ULONG cb)
{
m_fAlloc = (cb > 256);
if (m_fAlloc)
m_pbBuf = new char[cb];
else
m_pbBuf = m_szBufT;
}
~TempBuffer()
{ if (m_pbBuf && m_fAlloc) delete [] m_pbBuf; }
void *GetBuffer() const
{ return m_pbBuf; }
private:
void *m_pbBuf;
char m_szBufT[256]; // We'll use this temp buffer for small cases.
bool m_fAlloc;
};
//---------------------------------------------------------------------------
// String helpers.
//---------------------------------------------------------------------------
// Given and ANSI String, copy it into a wide buffer.
// NOTE: Be careful about scoping when using this macro!
//
// How to use the below two macros:
//
// ...
// LPSTR pszA;
// pszA = MyGetpszAnsiingRoutine();
// MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
// MyUseWideStringRoutine(pwsz);
// ...
//
// Similarily for MAKE_ANSIPTR_FROMWIDE(). Note that the first param does
// not have to be declared, and no clean up must be done.
//---------------------------------------------------------------------------
#define UNICODE_FROM_ANSI(pwszUnicode, pszAnsi, cb) \
MultiByteToWideChar(CP_ACP, 0, pszAnsi, -1, pwszUnicode, cb);
#define MAKE_WIDEPTR_FROMANSI(ptrname, pszAnsi) \
char *__psz##ptrname = pszAnsi?pszAnsi:""; \
long __l##ptrname = (lstrlen(__psz##ptrname) + 1) * sizeof(WCHAR); \
TempBuffer __TempBuffer##ptrname(__l##ptrname); \
MultiByteToWideChar(CP_ACP, 0, __psz##ptrname, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
#define MAKE_ANSIPTR_FROMWIDE(ptrname, pwszUnicode) \
WCHAR *__pwsz##ptrname = pwszUnicode?pwszUnicode:L""; \
long __l##ptrname = (lstrlenW(__pwsz##ptrname) + 1) * sizeof(char); \
TempBuffer __TempBuffer##ptrname(__l##ptrname); \
WideCharToMultiByte(CP_ACP, 0, __pwsz##ptrname, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); \
LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
//--- EOF -------------------------------------------------------------------