Skip to content

Commit

Permalink
improve error handling and link msi.lib via pragma instead of project…
Browse files Browse the repository at this point in the history
… setting

git-svn-id: https://poroz/svnad/wfl_mr/branches/dev/jmairboe/trunk/src/tools/CreateEmbedLangTransform@8475 0d26eecb-4107-0410-bdd6-a17e8e2aa929
  • Loading branch information
jmairboeck committed May 22, 2023
1 parent af67e25 commit aea85ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 0 additions & 2 deletions CreateEmbedLangTransform/CreateEmbedLangTransform.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>msi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -75,7 +74,6 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>msi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
21 changes: 13 additions & 8 deletions CreateEmbedLangTransform/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
#include <MsiQuery.h>
#include <iostream>

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

#ifdef _UNICODE
#define tcerr wcerr
#else
#define tcerr cerr
#endif // _UNICODE

void HandleWindowsError(LPCTSTR func) {
DWORD error = GetLastError();
LPTSTR errorMessage;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&errorMessage), 0, NULL);
std::tcerr << TEXT("error at ") << func << TEXT(": ") << errorMessage << std::endl;
LocalFree(errorMessage);
}

void HandleMsiError(LPCTSTR func) {
PMSIHANDLE error = MsiGetLastErrorRecord();
if (error) {
Expand All @@ -24,14 +34,9 @@ void HandleMsiError(LPCTSTR func) {
delete[] errorMessage;
}
}
}

void HandleWindowsError(LPCTSTR func) {
DWORD error = GetLastError();
LPTSTR errorMessage;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPTSTR>(&errorMessage), 0, NULL);
std::tcerr << TEXT("error at ") << func << TEXT(": ") << errorMessage << std::endl;
LocalFree(errorMessage);
else {
HandleWindowsError(func);
}
}

int _tmain(int argc, TCHAR **argv) {
Expand Down

0 comments on commit aea85ed

Please sign in to comment.