Repository navigation
Expand file tree
/
Copy pathDllMain.cpp
More file actions
80 lines (67 loc) · 2.12 KB
/
Copy pathDllMain.cpp
File metadata and controls
80 lines (67 loc) · 2.12 KB
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
#include "pch.h"
#include "PluginModule.h"
#include <string>
#include <cstdio>
namespace
{
HINSTANCE g_plugin_module = nullptr;
}
HINSTANCE GetPluginModuleHandle()
{
return g_plugin_module;
}
const wchar_t* GetPluginVersionString()
{
// Read the version from this DLL's embedded VS_VERSION_INFO "FileVersion"
// resource so it is never a hard-coded value and always matches the version
// declared in OpenCodeGoUsage.rc.
static std::wstring version_cache; // thread-unsafe init, but read once at startup
if (!version_cache.empty())
return version_cache.c_str();
wchar_t module_path[MAX_PATH];
DWORD len = GetModuleFileNameW(g_plugin_module, module_path, MAX_PATH);
if (len == 0 || len >= MAX_PATH)
return L"";
DWORD handle = 0;
DWORD size = GetFileVersionInfoSizeW(module_path, &handle);
if (size == 0)
return L"";
std::string buffer(size, '\0');
if (!GetFileVersionInfoW(module_path, 0, size, &buffer[0]))
return L"";
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
};
LANGANDCODEPAGE* lang = nullptr;
UINT lang_len = 0;
if (!VerQueryValueW(&buffer[0], L"\\VarFileInfo\\Translation",
reinterpret_cast<void**>(&lang), &lang_len) || lang_len < sizeof(LANGANDCODEPAGE))
{
return L"";
}
wchar_t subblock[64];
swprintf_s(subblock, L"\\StringFileInfo\\%04x%04x\\FileVersion", lang[0].wLanguage, lang[0].wCodePage);
void* value = nullptr;
UINT value_len = 0;
if (!VerQueryValueW(&buffer[0], subblock, &value, &value_len) || value == nullptr || value_len == 0)
return L"";
version_cache = static_cast<const wchar_t*>(value);
return version_cache.c_str();
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
g_plugin_module = static_cast<HINSTANCE>(hModule);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
#pragma comment(lib, "version.lib")