Skip to content

Commit 5e44b1f

Browse files
committed
CreateEmbedLangTransform: replace the usage of unique_ptr<char[]> with strings
* using higher-level constructs is easier to understand * switch to C++17 for the mutable string::data() overload git-svn-id: https://poroz/svnad/wfl_mr/branches/dev/trunk/src/tools/CreateEmbedLangTransform@13663 0d26eecb-4107-0410-bdd6-a17e8e2aa929
1 parent 07cf6c1 commit 5e44b1f

2 files changed

Lines changed: 17 additions & 12 deletions

File tree

CreateEmbedLangTransform/CreateEmbedLangTransform.vcxproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
<ClCompile>
5353
<WarningLevel>Level3</WarningLevel>
5454
<Optimization>Disabled</Optimization>
55-
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
55+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
56+
<LanguageStandard>stdcpp17</LanguageStandard>
5657
</ClCompile>
5758
<Link>
5859
<SubSystem>Console</SubSystem>
@@ -65,7 +66,8 @@
6566
<Optimization>MaxSpeed</Optimization>
6667
<FunctionLevelLinking>true</FunctionLevelLinking>
6768
<IntrinsicFunctions>true</IntrinsicFunctions>
68-
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
69+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
70+
<LanguageStandard>stdcpp17</LanguageStandard>
6971
</ClCompile>
7072
<Link>
7173
<SubSystem>Console</SubSystem>

CreateEmbedLangTransform/main.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <tchar.h>
33
#include <MsiQuery.h>
44
#include <iostream>
5-
#include <memory>
5+
#include <string>
66

77
#pragma comment(lib, "msi.lib")
88

@@ -11,6 +11,7 @@
1111
#else
1212
#define tcerr cerr
1313
#endif // _UNICODE
14+
using tstring = std::basic_string<TCHAR>;
1415

1516
void HandleWindowsError(LPCTSTR func) {
1617
DWORD error{GetLastError()};
@@ -27,9 +28,10 @@ void HandleMsiError(UINT result, LPCTSTR func) {
2728
DWORD length{0u};
2829
auto status{MsiFormatRecord(0u, error, TEXT(""), &length)}; // get the length of the error message
2930
if (status == ERROR_MORE_DATA) {
31+
tstring errorMessage;
32+
errorMessage.resize(length);
3033
length++; // for the '\0' terminator
31-
auto errorMessage{std::make_unique<TCHAR[]>(length)};
32-
status = MsiFormatRecord(0u, error, errorMessage.get(), &length);
34+
status = MsiFormatRecord(0u, error, errorMessage.data(), &length);
3335
if (status == ERROR_SUCCESS) {
3436
std::tcerr << TEXT("MSI error at ") << func << TEXT(": ") << errorMessage << TEXT(" (") << result << TEXT(")") << std::endl;
3537
}
@@ -125,18 +127,19 @@ int _tmain(int argc, TCHAR **argv) {
125127
HandleMsiError(uResult, TEXT("MsiSummaryInfoGetProperty length"));
126128
return 1;
127129
}
128-
length++; // for the '\0' terminator
129130
{
130-
auto stringValue{std::make_unique<TCHAR[]>(length + _tcslen(argv[3]) + 1)}; // allocate a buffer long enough for the new value (including the separator)
131-
uResult = MsiSummaryInfoGetProperty(summaryInfo, PIDSI_TEMPLATE, &dataType, nullptr, nullptr, stringValue.get(), &length);
131+
tstring stringValue;
132+
stringValue.resize(length); // allocate space for the value to be filled (note: reserve would lead to UB!)
133+
length++; // for the '\0' terminator
134+
uResult = MsiSummaryInfoGetProperty(summaryInfo, PIDSI_TEMPLATE, &dataType, nullptr, nullptr, stringValue.data(), &length);
132135
if (uResult != ERROR_SUCCESS) {
133136
HandleMsiError(uResult, TEXT("MsiSummaryInfoGetProperty value"));
134137
return 1;
135138
}
136-
if (_tcsstr(stringValue.get(), argv[3]) == nullptr) { // check if the required value is already included, only add it if not
137-
stringValue[length] = TEXT(',');
138-
_tcscpy(stringValue.get() + length + 1, argv[3]);
139-
uResult = MsiSummaryInfoSetProperty(summaryInfo, PIDSI_TEMPLATE, dataType, 0, nullptr, stringValue.get());
139+
if (stringValue.find(argv[3]) == tstring::npos) { // check if the required value is already included, only add it if not
140+
stringValue.append(TEXT(","));
141+
stringValue.append(argv[3]);
142+
uResult = MsiSummaryInfoSetProperty(summaryInfo, PIDSI_TEMPLATE, dataType, 0, nullptr, stringValue.data());
140143
if (uResult != ERROR_SUCCESS) {
141144
HandleMsiError(uResult, TEXT("MsiSummaryInfoSetProperty"));
142145
return 1;

0 commit comments

Comments
 (0)