-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
70 lines (52 loc) · 1.4 KB
/
utils.cpp
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
/*
* ArgX - Better command line processing for Windows.
*
* Copyright (c) 2019 Alastair J. Houghton.
*
*/
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include "utils.hpp"
// N.B. The string converters need to use LocalAlloc()/LocalFree() because
// of the API contract for ArgxFindExecutableA().
LPWSTR argx_utils::ConvertMultiByteToWideString(LPCSTR psz) {
int ret = MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0);
if (!ret)
return NULL;
LPWSTR pwsz = (LPWSTR)LocalAlloc(LMEM_FIXED, ret * sizeof(WCHAR));
if (!pwsz)
return NULL;
ret = MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, ret);
if (!ret) {
DWORD dwErr = GetLastError();
LocalFree((HLOCAL)pwsz);
SetLastError(dwErr);
return NULL;
}
return pwsz;
}
void argx_utils::FreeConvertedString(LPWSTR pwsz) {
if (pwsz)
LocalFree((HLOCAL)pwsz);
}
LPSTR argx_utils::ConvertWideToMultiByteString(LPCWSTR pwsz) {
int ret = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
pwsz, -1,
NULL, 0, NULL, NULL);
if (!ret)
return NULL;
LPSTR psz = (LPSTR)LocalAlloc(LMEM_FIXED, ret);
if (!psz)
return NULL;
ret = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS,
pwsz, -1,
psz, ret,
NULL, NULL);
if (!ret)
return NULL;
return psz;
}
void argx_utils::FreeConvertedString(LPSTR psz) {
if (psz)
LocalFree((HLOCAL)psz);
}