Ejemplo n.º 1
0
TEST(UtilTest, detectVMDirectory)
{
#if defined(WIN32)
	void *token = NULL;
	ASSERT_EQ(OMR_ERROR_NONE, OMR_Glue_GetVMDirectoryToken(&token));

	wchar_t path[2048];
	size_t pathMax = 2048;
	wchar_t *pathEnd = NULL;
	ASSERT_EQ(OMR_ERROR_NONE, detectVMDirectory(path, pathMax, &pathEnd));
	ASSERT_TRUE((NULL == pathEnd) || (L'\0' == *pathEnd));
	wprintf(L"VM Directory: '%s'\n", path);

	if (NULL != pathEnd) {
		size_t length = pathMax - wcslen(path) - 2;
		_snwprintf(pathEnd, length, L"\\abc");
		wprintf(L"Mangled VM Directory: '%s'\n", path);
	}
#endif /* defined(WIN32) */
}
Ejemplo n.º 2
0
omr_error_t
detectVMDirectory(wchar_t *vmDirectory, size_t vmDirectoryLength, wchar_t **vmDirectoryEnd)
{
	omr_error_t rc = OMR_ERROR_NOT_AVAILABLE;
	LPCSTR moduleName = NULL;
	HANDLE moduleHandle = NULL;
	DWORD pathLength = 0;

	if (OMR_ERROR_NONE == OMR_Glue_GetVMDirectoryToken((void *)&moduleName)) {
		moduleHandle = GetModuleHandle(moduleName);
		if (NULL != moduleHandle) {
			pathLength = GetModuleFileNameW(moduleHandle, vmDirectory, (DWORD)vmDirectoryLength);
			if ((0 != pathLength) && (pathLength < (DWORD)vmDirectoryLength)) {
				/* remove the module name from the path */
				*vmDirectoryEnd = wcsrchr(vmDirectory, '\\');
				if (NULL != *vmDirectoryEnd) {
					**vmDirectoryEnd = L'\0';
				}
				rc = OMR_ERROR_NONE;
			}
		}
	}
	return rc;
}