3 #include "stringutils.h"
5 // HANDLE g_hHeap = GetProcessHeap();
6 #define g_hHeap GetProcessHeap()
10 MemInit(IN HANDLE Heap
)
12 /* Save the heap handle */
18 MemFree(IN PVOID lpMem
)
20 /* Free memory back into the heap */
21 return HeapFree(g_hHeap
, 0, lpMem
);
25 MemAlloc(IN DWORD dwFlags
,
28 /* Allocate memory from the heap */
29 return HeapAlloc(g_hHeap
, dwFlags
, dwBytes
);
33 FormatDateTime(IN LPSYSTEMTIME pDateTime
)
35 LPWSTR lpszDateTime
= NULL
;
39 int iDateBufSize
= 0, iTimeBufSize
= 0;
41 iDateBufSize
= GetDateFormatW(LOCALE_USER_DEFAULT
,
42 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE
,
47 iTimeBufSize
= GetTimeFormatW(LOCALE_USER_DEFAULT
,
54 if ( (iDateBufSize
> 0) && (iTimeBufSize
> 0) )
56 lpszDateTime
= (LPWSTR
)MemAlloc(0, (iDateBufSize
+ iTimeBufSize
) * sizeof(TCHAR
));
58 GetDateFormatW(LOCALE_USER_DEFAULT
,
59 /* Only for Windows 7 : DATE_AUTOLAYOUT | */ DATE_SHORTDATE
,
64 if (iDateBufSize
> 0) lpszDateTime
[iDateBufSize
-1] = L
' ';
65 GetTimeFormatW(LOCALE_USER_DEFAULT
,
69 lpszDateTime
+ iDateBufSize
,
78 FreeDateTime(IN LPWSTR lpszDateTime
)
81 MemFree(lpszDateTime
);
85 LoadResourceStringEx(IN HINSTANCE hInstance
,
87 OUT
size_t* pSize OPTIONAL
)
89 LPWSTR lpszDestBuf
= NULL
, lpszResourceString
= NULL
;
92 // When passing a zero-length buffer size, LoadString(...) returns a
93 // read-only pointer buffer to the program's resource string.
94 iStrSize
= LoadString(hInstance
, uID
, (LPTSTR
)&lpszResourceString
, 0);
96 if ( lpszResourceString
&& ( (lpszDestBuf
= (LPWSTR
)MemAlloc(0, (iStrSize
+ 1) * sizeof(WCHAR
))) != NULL
) )
98 _tcsncpy(lpszDestBuf
, lpszResourceString
, iStrSize
);
99 lpszDestBuf
[iStrSize
] = L
'\0'; // NULL-terminate the string
102 *pSize
= iStrSize
+ 1;
114 LoadConditionalResourceStringEx(IN HINSTANCE hInstance
,
118 IN
size_t* pSize OPTIONAL
)
120 return LoadResourceStringEx(hInstance
,
121 (bCondition
? uIDifTrue
: uIDifFalse
),
126 //////////////////// The following comes from MSDN samples ///////////////////
127 // https://msdn.microsoft.com/en-us/library/windows/desktop/dd162826(v=vs.85).aspx
131 // ClipOrCenterRectToMonitor
133 // The most common problem apps have when running on a
134 // multimonitor system is that they "clip" or "pin" windows
135 // based on the SM_CXSCREEN and SM_CYSCREEN system metrics.
136 // Because of app compatibility reasons these system metrics
137 // return the size of the primary monitor.
139 // This shows how you use the multi-monitor functions
140 // to do the same thing.
142 VOID
ClipOrCenterRectToMonitor(LPRECT prc
, UINT flags
)
147 int w
= prc
->right
- prc
->left
;
148 int h
= prc
->bottom
- prc
->top
;
151 // Get the nearest monitor to the passed rect.
153 hMonitor
= MonitorFromRect(prc
, MONITOR_DEFAULTTONEAREST
);
156 // Get the work area or entire monitor rect.
158 mi
.cbSize
= sizeof(mi
);
159 GetMonitorInfo(hMonitor
, &mi
);
161 if (flags
& MONITOR_WORKAREA
)
167 // Center or clip the passed rect to the monitor rect.
169 if (flags
& MONITOR_CENTER
)
171 prc
->left
= rc
.left
+ (rc
.right
- rc
.left
- w
) / 2;
172 prc
->top
= rc
.top
+ (rc
.bottom
- rc
.top
- h
) / 2;
173 prc
->right
= prc
->left
+ w
;
174 prc
->bottom
= prc
->top
+ h
;
178 prc
->left
= max(rc
.left
, min(rc
.right
-w
, prc
->left
));
179 prc
->top
= max(rc
.top
, min(rc
.bottom
-h
, prc
->top
));
180 prc
->right
= prc
->left
+ w
;
181 prc
->bottom
= prc
->top
+ h
;
185 VOID
ClipOrCenterWindowToMonitor(HWND hWnd
, UINT flags
)
188 GetWindowRect(hWnd
, &rc
);
189 ClipOrCenterRectToMonitor(&rc
, flags
);
190 SetWindowPos(hWnd
, NULL
, rc
.left
, rc
.top
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
);
192 ////////////////////////////////////////////////////////////////////////////////
196 GetExecutableVendor(IN LPCWSTR lpszFilename
)
198 LPWSTR lpszVendor
= NULL
;
204 LPVOID pvData
= NULL
;
206 WORD wCodePage
= 0, wLangID
= 0;
207 LPTSTR lpszStrFileInfo
= NULL
;
209 LPWSTR lpszData
= NULL
;
211 if (lpszFilename
== NULL
) return NULL
;
213 dwLen
= GetFileVersionInfoSizeW(lpszFilename
, &dwHandle
);
214 if (dwLen
== 0) return NULL
;
216 lpData
= MemAlloc(0, dwLen
);
217 if (!lpData
) return NULL
;
219 GetFileVersionInfoW(lpszFilename
, dwHandle
, dwLen
, lpData
);
221 if (VerQueryValueW(lpData
, L
"\\VarFileInfo\\Translation", &pvData
, &BufLen
))
223 wCodePage
= LOWORD(*(DWORD
*)pvData
);
224 wLangID
= HIWORD(*(DWORD
*)pvData
);
226 lpszStrFileInfo
= FormatString(L
"StringFileInfo\\%04X%04X\\CompanyName",
231 VerQueryValueW(lpData
, lpszStrFileInfo
, (LPVOID
*)&lpszData
, &BufLen
);
234 lpszVendor
= (LPWSTR
)MemAlloc(0, BufLen
* sizeof(WCHAR
));
236 wcscpy(lpszVendor
, lpszData
);
243 MemFree(lpszStrFileInfo
);