bool log::alloc_console_private()
{
	char image_path[MAX_STRING_SIZE] = {0};
	bool is_ok = true;
	PLOADED_IMAGE loaded_image_ptr = NULL;

	GetModuleFileNameA(NULL, image_path, sizeof(image_path));
	loaded_image_ptr = ImageLoad(image_path, NULL);

	switch(loaded_image_ptr->FileHeader->OptionalHeader.Subsystem){
	   case IMAGE_SUBSYSTEM_UNKNOWN:
			is_ok = false;
		   break;
	   case IMAGE_SUBSYSTEM_NATIVE:
	   case IMAGE_SUBSYSTEM_WINDOWS_GUI:
	   case IMAGE_SUBSYSTEM_OS2_CUI:
	   case IMAGE_SUBSYSTEM_NATIVE_WINDOWS:
	   case IMAGE_SUBSYSTEM_WINDOWS_CE_GUI:
		   if(AllocConsole() == FALSE) is_ok = false;
		   break;
	   case IMAGE_SUBSYSTEM_WINDOWS_CUI:
	   case IMAGE_SUBSYSTEM_POSIX_CUI:
		   break;
	}

	ImageUnload(loaded_image_ptr);
	std_output_handler_ = GetStdHandle(STD_OUTPUT_HANDLE);
	__matrix_setlocale(LC_ALL, __TEXT("chs"));
	return is_ok;
}
Example #2
0
u32 CHookFunction_i::GetExportFunctionInfo(CArray<EXPORT_FUN_INFO, EXPORT_FUN_INFO&>& InfoArray, 
                                           const char* pszDllFile)
{
    if(!pszDllFile)
    {
        return 0;
    }
	  
    PLOADED_IMAGE pImage = ::ImageLoad((char*)pszDllFile, NULL);
    if(!pImage)
    {
        return 0;
    }
	
    DWORD dwBase = (DWORD)pImage->MappedAddress;
    PIMAGE_SECTION_HEADER pSections    = (PIMAGE_SECTION_HEADER)((DWORD)pImage->FileHeader + sizeof(IMAGE_NT_HEADERS));
    PIMAGE_EXPORT_DIRECTORY pExportDir = (PIMAGE_EXPORT_DIRECTORY)(RVA2RawAdr(pSections, pImage->FileHeader->OptionalHeader.DataDirectory[0].VirtualAddress) + dwBase);
	
    InfoArray.RemoveAll();
	  
    EXPORT_FUN_INFO info;
	
    DWORD* pFunOffsetAdr = (DWORD*)(RVA2RawAdr(pSections, pExportDir->AddressOfFunctions) + dwBase);
    for(DWORD i = 0; i < pExportDir->NumberOfFunctions; i++)
    {
        info.dwOffset = 0;
        info.strFunName = "";
        info.dwOrdinal = 0;
        
        info.dwOffset = *(pFunOffsetAdr + i);
        info.dwOrdinal =  i + 1;
        InfoArray.Add(info);
    }
	
    DWORD* pFunNamesAdr       = (DWORD*)(RVA2RawAdr(pSections, pExportDir->AddressOfNames) + dwBase);
    WORD* pFunNamesOrdinalAdr = (WORD*)(RVA2RawAdr(pSections, pExportDir->AddressOfNameOrdinals) + dwBase);
	  
    char* pName = NULL;
    for(i = 0; i < pExportDir->NumberOfNames; i++)
    {
        pName = (char*)(RVA2RawAdr(pSections, *(pFunNamesAdr+i)) + dwBase);
        int idx = *(pFunNamesOrdinalAdr+i);
        if(idx < InfoArray.GetSize())
        {
          EXPORT_FUN_INFO& tmpInfo = InfoArray.ElementAt(idx);
          tmpInfo.strFunName = pName;
        }
    }
	  
    ImageUnload(pImage);
    
    return InfoArray.GetSize();
}
Example #3
0
BOOL CShadowSSDTMgr::GetShadowSSDTNativeAddrBySymbol()
{
	BOOL			boRetn;
	PDWORD			pW32pServiceTable = 0;
	DWORD			dwload = 0;
	PLOADED_IMAGE	ploadImage = {0};
	char			imgPath[MAX_PATH];

	// 初始化
	if ( !InitSymbolsHandler() )
	{
		return FALSE;
	}

	// 获取系统目录"C:\WINDOWS\system32"
	GetSystemDirectory( imgPath, MAX_PATH );
	// 加载模块
	ploadImage = ImageLoad( "win32k.sys", imgPath );

	// 加载符号"C:\WINDOWS\system32\win32k.sys"
	strcat( imgPath, "\\win32k.sys" );
	dwload=SymLoadModule ( g_ixerhProc, ploadImage->hFile,
									imgPath, "win32k.pdb",
									0, ploadImage->SizeOfImage );

	boRetn=SymEnumSymbols( g_ixerhProc, dwload, NULL, (PSYM_ENUMERATESYMBOLS_CALLBACK)EnumSymRoutine, NULL );
	if (g_W32pServiceTable)
	{
		pW32pServiceTable = (PDWORD)( g_W32pServiceTable - dwload + (DWORD)ploadImage->MappedAddress );
		boRetn=SymEnumSymbols( g_ixerhProc, dwload, NULL, (PSYM_ENUMERATESYMBOLS_CALLBACK)EnumSymRoutine, pW32pServiceTable );
	}

	ImageUnload(ploadImage);
	SymCleanup(g_ixerhProc);
	return boRetn;
}
Example #4
0
INT_PTR CALLBACK CDialogPackage::SelectPluginDlgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_INITDIALOG:
    {
        EnableThemeDialogTexture(hWnd, ETDT_ENABLETAB);
        c_Dialog->SetDialogFont(hWnd);

        auto plugins = (std::pair<std::wstring, std::wstring>*)lParam;
        SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)plugins);
    }
    break;

    case WM_COMMAND:
        switch (LOWORD(wParam))
        {
        case IDC_PACKAGESELECTPLUGIN_32BITBROWSE_BUTTON:
        case IDC_PACKAGESELECTPLUGIN_64BITBROWSE_BUTTON:
        {
            WCHAR buffer[MAX_PATH];
            buffer[0] = L'\0';

            OPENFILENAME ofn = { sizeof(OPENFILENAME) };
            ofn.Flags = OFN_FILEMUSTEXIST;
            ofn.lpstrFilter = L"Plugins (.dll)\0*.dll";
            ofn.lpstrTitle = L"Select plugin file";
            ofn.lpstrDefExt = L"dll";
            ofn.nFilterIndex = 0;
            ofn.lpstrFile = buffer;
            ofn.nMaxFile = _countof(buffer);
            ofn.hwndOwner = c_Dialog->GetWindow();

            if (!GetOpenFileName(&ofn))
            {
                break;
            }

            bool x32 = LOWORD(wParam) == IDC_PACKAGESELECTPLUGIN_32BITBROWSE_BUTTON;

            LOADED_IMAGE* loadedImage = ImageLoad(StringUtil::NarrowUTF8(buffer).c_str(), NULL);
            if (loadedImage)
            {
                WORD machine = loadedImage->FileHeader->FileHeader.Machine;
                ImageUnload(loadedImage);

                if ((x32 && machine == IMAGE_FILE_MACHINE_I386) || (!x32 && machine == IMAGE_FILE_MACHINE_AMD64))
                {
                    // Check if same name as other DLL
                    auto plugins = (std::pair<std::wstring, std::wstring>*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
                    const WCHAR* otherName = PathFindFileName(x32 ? plugins->second.c_str() : plugins->first.c_str());
                    if (*otherName && _wcsicmp(otherName, PathFindFileName(buffer)) != 0)
                    {
                        MessageBox(hWnd, L"Plugins must have same name.", L"Rainmeter Skin Packager", MB_OK | MB_TOPMOST);
                        break;
                    }

                    PathSetDlgItemPath(hWnd, x32 ? IDC_PACKAGESELECTPLUGIN_32BIT_EDIT : IDC_PACKAGESELECTPLUGIN_64BIT_EDIT, buffer);

                    (x32 ? plugins->first : plugins->second) = buffer;

                    if (!plugins->first.empty() && !plugins->second.empty())
                    {
                        // Enable Add button if both plugins have been selected
                        EnableWindow(GetDlgItem(hWnd, IDOK), TRUE);
                    }
                    break;
                }
            }

            MessageBox(hWnd, L"Invalid plugin.", L"Rainmeter Skin Packager", MB_OK | MB_TOPMOST);
        }
        break;

        case IDOK:
            EndDialog(hWnd, 1);
            break;
        }
        break;

    case WM_CLOSE:
        EndDialog(hWnd, 0);
        break;

    default:
        return FALSE;
    }

    return TRUE;
}
Example #5
0
void DialogAbout::TabPlugins::Initialize()
{
	// Add columns to the list view
	HWND item = GetControl(Id_ItemsListView);

	LVCOLUMN lvc;
	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvc.fmt = LVCFMT_LEFT;  // left-aligned column
	lvc.iSubItem = 0;
	lvc.cx = 140;
	lvc.pszText = GetString(ID_STR_NAME);
	ListView_InsertColumn(item, 0, &lvc);
	lvc.iSubItem = 1;
	lvc.cx = 80;
	lvc.pszText = GetString(ID_STR_VERSION);
	ListView_InsertColumn(item, 1, &lvc);
	lvc.iSubItem = 2;

	// Start 3rd column at max width
	RECT rect;
	lvc.cx = GetWindowRect(item, &rect) ? (rect.right - rect.left - 193) : 290;
	lvc.pszText = GetString(ID_STR_AUTHOR);
	ListView_InsertColumn(item, 2, &lvc);

	LVITEM vitem;
	vitem.mask = LVIF_TEXT;
	vitem.iItem = 0;
	vitem.iSubItem = 0;

	auto findPlugins = [&](const std::wstring& path)
	{
		std::wstring filter = path + L"*.dll";

		WIN32_FIND_DATA fd;
		HANDLE hSearch = FindFirstFile(filter.c_str(), &fd);
		if (hSearch == INVALID_HANDLE_VALUE)
		{
			return;
		}

		int index = 0;
		do
		{
			// Try to get the version and author
			std::wstring tmpSz = path + fd.cFileName;
			const WCHAR* path = tmpSz.c_str();

			LOADED_IMAGE* loadedImage = ImageLoad(StringUtil::Narrow(path).c_str(), nullptr);
			if (!loadedImage)
			{
				LogErrorF(L"About Dialog - Unable to load plugin: %s", fd.cFileName);
				continue;
			}

			const WORD imageBitness = loadedImage->FileHeader->FileHeader.Machine;
			ImageUnload(loadedImage);

#ifdef _WIN64
			const WORD rainmeterBitness = IMAGE_FILE_MACHINE_AMD64;
#elif _WIN32
			const WORD rainmeterBitness = IMAGE_FILE_MACHINE_I386;
#endif

			if (rainmeterBitness != imageBitness)
			{
				LogErrorF(L"About Dialog - Incorrect bitness of plugin: %s", fd.cFileName);
				continue;
			}

			vitem.iItem = index;
			vitem.pszText = fd.cFileName;

			// Try to get version and author from file resources first
			DWORD handle;
			DWORD versionSize = GetFileVersionInfoSize(path, &handle);
			if (versionSize)
			{
				bool found = false;
				void* data = new BYTE[versionSize];
				if (GetFileVersionInfo(path, 0, versionSize, data))
				{
					UINT len;
					struct LANGCODEPAGE
					{
						WORD wLanguage;
						WORD wCodePage;
					} *lcp;

					if (VerQueryValue(data, L"\\VarFileInfo\\Translation", (LPVOID*)&lcp, &len))
					{
						WCHAR key[64];
						LPWSTR value;

						_snwprintf_s(key, _TRUNCATE, L"\\StringFileInfo\\%04x%04x\\ProductName", lcp[0].wLanguage, lcp[0].wCodePage);
						if (VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&value, &len) &&
							wcscmp(value, L"Rainmeter") == 0)
						{
							ListView_InsertItem(item, &vitem);
							++index;
							found = true;

							_snwprintf_s(key, _TRUNCATE, L"\\StringFileInfo\\%04x%04x\\FileVersion", lcp[0].wLanguage, lcp[0].wCodePage);
							if (VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&value, &len))
							{
								ListView_SetItemText(item, vitem.iItem, 1, value);
							}

							_snwprintf_s(key, _TRUNCATE, L"\\StringFileInfo\\%04x%04x\\LegalCopyright", lcp[0].wLanguage, lcp[0].wCodePage);
							if (VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&value, &len))
							{
								ListView_SetItemText(item, vitem.iItem, 2, value);
							}
						}
					}
				}

				delete [] data;
				if (found) continue;
			}

			// Try old calling GetPluginVersion/GetPluginAuthor for backwards compatibility
			DWORD err = 0;
			HMODULE dll = System::RmLoadLibrary(path, &err);
			if (dll)
			{
				ListView_InsertItem(item, &vitem);
				++index;

				GETPLUGINVERSION GetVersionFunc = (GETPLUGINVERSION)GetProcAddress(dll, "GetPluginVersion");
				if (GetVersionFunc)
				{
					UINT version = GetVersionFunc();
					WCHAR buffer[64];
					_snwprintf_s(buffer, _TRUNCATE, L"%u.%u", version / 1000, version % 1000);
					ListView_SetItemText(item, vitem.iItem, 1, buffer);
				}

				GETPLUGINAUTHOR GetAuthorFunc = (GETPLUGINAUTHOR)GetProcAddress(dll, "GetPluginAuthor");
				if (GetAuthorFunc)
				{
					LPCTSTR author = GetAuthorFunc();
					if (author && *author)
					{
						ListView_SetItemText(item, vitem.iItem, 2, (LPWSTR)author);
					}
				}

				FreeLibrary(dll);
			}
			else
			{
				LogErrorF(L"About Dialog - Unable to load plugin: %s (%u)", tmpSz.c_str(), err);
			}
		}
		while (FindNextFile(hSearch, &fd));
		FindClose(hSearch);
	};

	findPlugins(GetRainmeter().GetPluginPath());
	if (GetRainmeter().HasUserPluginPath())
	{
		findPlugins(GetRainmeter().GetUserPluginPath());
	}

	m_Initialized = true;
}