Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | // MyWindows.cpp |
michael@0 | 2 | |
michael@0 | 3 | #include "StdAfx.h" |
michael@0 | 4 | |
michael@0 | 5 | #ifndef _WIN32 |
michael@0 | 6 | |
michael@0 | 7 | #include "MyWindows.h" |
michael@0 | 8 | #include "Types.h" |
michael@0 | 9 | #include <malloc.h> |
michael@0 | 10 | |
michael@0 | 11 | static inline void *AllocateForBSTR(size_t cb) { return ::malloc(cb); } |
michael@0 | 12 | static inline void FreeForBSTR(void *pv) { ::free(pv);} |
michael@0 | 13 | |
michael@0 | 14 | static UINT MyStringLen(const wchar_t *s) |
michael@0 | 15 | { |
michael@0 | 16 | UINT i; |
michael@0 | 17 | for (i = 0; s[i] != '\0'; i++); |
michael@0 | 18 | return i; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | BSTR SysAllocStringByteLen(LPCSTR psz, UINT len) |
michael@0 | 22 | { |
michael@0 | 23 | int realLen = len + sizeof(UINT) + sizeof(OLECHAR) + sizeof(OLECHAR); |
michael@0 | 24 | void *p = AllocateForBSTR(realLen); |
michael@0 | 25 | if (p == 0) |
michael@0 | 26 | return 0; |
michael@0 | 27 | *(UINT *)p = len; |
michael@0 | 28 | BSTR bstr = (BSTR)((UINT *)p + 1); |
michael@0 | 29 | memmove(bstr, psz, len); |
michael@0 | 30 | Byte *pb = ((Byte *)bstr) + len; |
michael@0 | 31 | for (int i = 0; i < sizeof(OLECHAR) * 2; i++) |
michael@0 | 32 | pb[i] = 0; |
michael@0 | 33 | return bstr; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | BSTR SysAllocString(const OLECHAR *sz) |
michael@0 | 37 | { |
michael@0 | 38 | if (sz == 0) |
michael@0 | 39 | return 0; |
michael@0 | 40 | UINT strLen = MyStringLen(sz); |
michael@0 | 41 | UINT len = (strLen + 1) * sizeof(OLECHAR); |
michael@0 | 42 | void *p = AllocateForBSTR(len + sizeof(UINT)); |
michael@0 | 43 | if (p == 0) |
michael@0 | 44 | return 0; |
michael@0 | 45 | *(UINT *)p = strLen; |
michael@0 | 46 | BSTR bstr = (BSTR)((UINT *)p + 1); |
michael@0 | 47 | memmove(bstr, sz, len); |
michael@0 | 48 | return bstr; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | void SysFreeString(BSTR bstr) |
michael@0 | 52 | { |
michael@0 | 53 | if (bstr != 0) |
michael@0 | 54 | FreeForBSTR((UINT *)bstr - 1); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | UINT SysStringByteLen(BSTR bstr) |
michael@0 | 58 | { |
michael@0 | 59 | if (bstr == 0) |
michael@0 | 60 | return 0; |
michael@0 | 61 | return *((UINT *)bstr - 1); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | UINT SysStringLen(BSTR bstr) |
michael@0 | 65 | { |
michael@0 | 66 | return SysStringByteLen(bstr) / sizeof(OLECHAR); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | HRESULT VariantClear(VARIANTARG *prop) |
michael@0 | 70 | { |
michael@0 | 71 | if (prop->vt == VT_BSTR) |
michael@0 | 72 | SysFreeString(prop->bstrVal); |
michael@0 | 73 | prop->vt = VT_EMPTY; |
michael@0 | 74 | return S_OK; |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | HRESULT VariantCopy(VARIANTARG *dest, VARIANTARG *src) |
michael@0 | 78 | { |
michael@0 | 79 | HRESULT res = ::VariantClear(dest); |
michael@0 | 80 | if (res != S_OK) |
michael@0 | 81 | return res; |
michael@0 | 82 | if (src->vt == VT_BSTR) |
michael@0 | 83 | { |
michael@0 | 84 | dest->bstrVal = SysAllocStringByteLen((LPCSTR)src->bstrVal, |
michael@0 | 85 | SysStringByteLen(src->bstrVal)); |
michael@0 | 86 | if (dest->bstrVal == 0) |
michael@0 | 87 | return E_OUTOFMEMORY; |
michael@0 | 88 | dest->vt = VT_BSTR; |
michael@0 | 89 | } |
michael@0 | 90 | else |
michael@0 | 91 | *dest = *src; |
michael@0 | 92 | return S_OK; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | LONG CompareFileTime(const FILETIME* ft1, const FILETIME* ft2) |
michael@0 | 96 | { |
michael@0 | 97 | if(ft1->dwHighDateTime < ft2->dwHighDateTime) |
michael@0 | 98 | return -1; |
michael@0 | 99 | if(ft1->dwHighDateTime > ft2->dwHighDateTime) |
michael@0 | 100 | return 1; |
michael@0 | 101 | if(ft1->dwLowDateTime < ft2->dwLowDateTime) |
michael@0 | 102 | return -1; |
michael@0 | 103 | if(ft1->dwLowDateTime > ft2->dwLowDateTime) |
michael@0 | 104 | return 1; |
michael@0 | 105 | return 0; |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | DWORD GetLastError() |
michael@0 | 109 | { |
michael@0 | 110 | return 0; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | #endif |