in win32/src/single_performance_counter_win32.c [24:90]
static int get_current_app_name(char* app_name, uint32_t app_name_size)
{
int result;
char executable_full_file_name[MAX_PATH + 1];
// Codes_SRS_SINGLE_PERFORMANCE_COUNTER_45_006: [ single_performance_counter_create shall call GetModuleFileNameA to get the current executable path. ]
DWORD get_module_file_name_a_result = GetModuleFileNameA(NULL, executable_full_file_name, MAX_PATH + 1);
if (get_module_file_name_a_result == MAX_PATH + 1)
{
LogLastError("failure in GetModuleFileNameA(NULL, fullExecutableFileName=%p, MAX_PATH=%d + 1), it returned %" PRIu32 " which means location did not have enough space to hold the path", executable_full_file_name, MAX_PATH, get_module_file_name_a_result);
result = MU_FAILURE;
}
else if (get_module_file_name_a_result == 0)
{
LogLastError("failure in GetModuleFileNameA(NULL, fullExecutableFileName=%p, MAX_PATH=%d + 1), it returned %" PRIu32 "", executable_full_file_name, MAX_PATH, get_module_file_name_a_result);
result = MU_FAILURE;
}
else
{
// Codes_SRS_SINGLE_PERFORMANCE_COUNTER_45_007: [ single_performance_counter_create shall call strrchr on the executable path with '\' to get the executable name. ]
const char* where_is_last_backslash = strrchr(executable_full_file_name, '\\');
if (where_is_last_backslash == NULL)
{
LogError("unexpected not to have a \\ character in fullExecutableFileName=%s", executable_full_file_name);
result = MU_FAILURE;
}
else
{
// Move past last backslash.
where_is_last_backslash++;
// Codes_SRS_SINGLE_PERFORMANCE_COUNTER_45_008: [ single_performance_counter_create shall call strrchr on the executable name with '.' to get the executable name without extension. ]
const char* where_is_last_dot = strrchr(where_is_last_backslash, '.');
if (where_is_last_dot == NULL)
{
LogError("unexpected not to have a . character in where_is_last_backslash=%s", where_is_last_backslash);
result = MU_FAILURE;
}
else if (where_is_last_dot <= where_is_last_backslash)
{
LogError("unexpected not to have a . character in where_is_last_backslash=%s", where_is_last_backslash);
result = MU_FAILURE;
}
else
{
// Codes_SRS_SINGLE_PERFORMANCE_COUNTER_45_009: [ single_performance_counter_create shall call snprintf to copy the executable name without extension. ]
int bytes_printed = snprintf(app_name, app_name_size, "%*.*s",
(int)(where_is_last_dot - where_is_last_backslash), (int)(where_is_last_dot - where_is_last_backslash), where_is_last_backslash);
if (bytes_printed < 0)
{
LogError("sprintf failure: app_name = %p, size = %" PRIu32 ", where_is_last_backslash = %s, precision = %" PRIi32 "",
app_name, app_name_size, where_is_last_backslash, (int)(where_is_last_dot - where_is_last_backslash));
result = MU_FAILURE;
}
else if ((uint32_t)bytes_printed >= app_name_size - 1)
{
LogError("given buffer for app_name, size = %" PRIu32 "is smaller than found module name where_is_last_backslash = % s", app_name_size, where_is_last_backslash);
result = MU_FAILURE;
}
else
{
result = 0;
}
}
}
}
return result;
}