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
103
104
105
106
107
108
109
|
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//----------------------------------------------------------
// SPMIUtil.cpp - General utility functions
//----------------------------------------------------------
#include "standardpch.h"
#include "logging.h"
#include "spmiutil.h"
bool breakOnDebugBreakorAV = false;
void DebugBreakorAV(int val)
{
if (IsDebuggerPresent())
{
if (val == 0)
__debugbreak();
if (breakOnDebugBreakorAV)
__debugbreak();
}
int exception_code = EXCEPTIONCODE_DebugBreakorAV + val;
// assert((EXCEPTIONCODE_DebugBreakorAV <= exception_code) && (exception_code < EXCEPTIONCODE_DebugBreakorAV_MAX))
LogException(exception_code, "DebugBreak or AV Exception %d", val);
}
char* GetEnvironmentVariableWithDefaultA(const char* envVarName, const char* defaultValue)
{
char* retString = nullptr;
// Figure out how much space we need to allocate
DWORD dwRetVal = ::GetEnvironmentVariableA(envVarName, nullptr, 0);
if (dwRetVal != 0)
{
retString = new char[dwRetVal];
dwRetVal = ::GetEnvironmentVariableA(envVarName, retString, dwRetVal);
}
else
{
if (defaultValue != nullptr)
{
dwRetVal = (DWORD)strlen(defaultValue) + 1; // add one for null terminator
retString = new char[dwRetVal];
memcpy_s(retString, dwRetVal, defaultValue, dwRetVal);
}
}
return retString;
}
WCHAR* GetEnvironmentVariableWithDefaultW(const WCHAR* envVarName, const WCHAR* defaultValue)
{
WCHAR* retString = nullptr;
// Figure out how much space we need to allocate
DWORD dwRetVal = ::GetEnvironmentVariableW(envVarName, nullptr, 0);
if (dwRetVal != 0)
{
retString = new WCHAR[dwRetVal];
dwRetVal = ::GetEnvironmentVariableW(envVarName, retString, dwRetVal);
}
else
{
if (defaultValue != nullptr)
{
dwRetVal = (DWORD)wcslen(defaultValue) + 1; // add one for null terminator
retString = new WCHAR[dwRetVal];
memcpy_s(retString, dwRetVal * sizeof(WCHAR), defaultValue, dwRetVal * sizeof(WCHAR));
}
}
return retString;
}
#ifdef FEATURE_PAL
// For some reason, the PAL doesn't have GetCommandLineA(). So write it.
LPSTR GetCommandLineA()
{
LPSTR pCmdLine = nullptr;
LPWSTR pwCmdLine = GetCommandLineW();
if (pwCmdLine != nullptr)
{
// Convert to ASCII
int n = WideCharToMultiByte(CP_ACP, 0, pwCmdLine, -1, nullptr, 0, nullptr, nullptr);
if (n == 0)
{
LogError("MultiByteToWideChar failed %d", GetLastError());
return nullptr;
}
pCmdLine = new char[n];
int n2 = WideCharToMultiByte(CP_ACP, 0, pwCmdLine, -1, pCmdLine, n, nullptr, nullptr);
if ((n2 == 0) || (n2 != n))
{
LogError("MultiByteToWideChar failed %d", GetLastError());
return nullptr;
}
}
return pCmdLine;
}
#endif // FEATURE_PAL
|