-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler_string.h
More file actions
64 lines (52 loc) · 2.09 KB
/
compiler_string.h
File metadata and controls
64 lines (52 loc) · 2.09 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
#pragma once
#include "common.h"
/*
full compiler descriptor
*/
static const wchar_t const compiler_string_[] =
{ L"MSVC " WSTRNGFY(_MSC_FULL_VER) L"-" WSTRNGFY(_MSC_BUILD) };
/*
string_p_ must be pre-allocated memory block of length string_len_
*/
__declspec(dllexport) int compiler_string(wchar_t* const cs_string_p_, int string_len_)
{
ASSERT(cs_string_p_ != NULL); // remember: this is C code, not C++, hence the 'NULL'
ASSERT(wcslen(compiler_string_) < string_len_);
/*
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/sprintf-s-sprintf-s-l-swprintf-s-swprintf-s-l?view=msvc-160
*/
// (w)sprintf_s guarantees that the buffer will be null-terminated unless the buffer size is zero.
// at this point string_len_ > compiler_string_len_
// that matters because L'\0' will have to be appended to the cs_str_
int rezult = swprintf_s(cs_string_p_, string_len_, L"%s", compiler_string_);
return rezult ;
}
/*
if first arg is null returns the required result string size in the second arg
* C# declared as
[DllImport(valstat_dll_location, EntryPoint = "this_name", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
static extern bool this_name([MarshalAs(UnmanagedType.LPWStr)] string name_, out Int32 string_length_);
*/
__declspec(dllexport) BOOL this_name(wchar_t* const cs_string_p_, int* string_len_)
{
ASSERT(string_len_ != NULL); // remember: this is C code, not C++ hence the 'NULL'
const wchar_t const this_name_[0xFF] = { L'\0' };
if (0 == GetModuleFileNameW(handle_store(NULL), this_name_, 0xFF))
{
SetLastError(GetLastError()); // signal to the "managed" caller
return FALSE;
}
const size_t this_name_len_ = wcslen(this_name_);
if ((cs_string_p_ == NULL) || (cs_string_p_[0] == L'\0'))
{ // caller just wanted the size
*string_len_ = (1 + this_name_len_);
return TRUE;
}
#ifdef _DEBUG
size_t len_control = wcslen(cs_string_p_);
ASSERT(len_control == *string_len_);
#endif // DEBUG
ASSERT(this_name_len_ < *string_len_);
int rezult = swprintf_s(cs_string_p_, *string_len_, L"%s", this_name_);
return rezult > 0; // used as true or false by the callers
}