Пример #1
0
////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////// PUBLIC FUNCTIONS /////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
int GetFolderPath(FolderType folder,wchar_t *path,unsigned int pathsize)
{
	int iReturnCode = RETURN_OK;

	switch(folder)
	{
	case FOLDER_SYSTEM32:
		if(0==GetSystemDirectory(path,pathsize))
		{
			LOG(L" --> ERROR - GetFolderPath - System32 folder not found (LastError=%d)\n", GetLastError());
			iReturnCode = RETURN_ERR_INTERNAL;
		}
		break;
	case FOLDER_WOWSYS64:
#ifdef WIN64
		if(0==GetSystemWow64Directory(path,pathsize))
		{
			LOG(L" --> ERROR - GetFolderPath - SystemWow64 folder not found (LastError=%d)\n", GetLastError());
			iReturnCode = RETURN_ERR_INTERNAL;
		}
#else
		if(IsWow64Proc())
		{
			if(0==GetSystemWow64Directory(path,pathsize))
			{
				LOG(L" --> ERROR - GetFolderPath - SystemWow64 folder not found (LastError=%d)\n", GetLastError());
				iReturnCode = RETURN_ERR_INTERNAL;
			}
		}
		else
		{
			iReturnCode = RETURN_SKIP_FOLDER;
		}
#endif
		break;
	case FOLDER_APP:
		if(-1==swprintf_s(path,pathsize,L"C:\\Program Files\\Belgium Identity Card"))
		{			
			LOG(L" --> ERROR - GetFolderPath - Buffer too small\n");
			iReturnCode = RETURN_ERR_INTERNAL;
		}
		break;
	default:
		if(0==GetTempPath(pathsize,path))
		{
			LOG(L" --> ERROR - GetFolderPath - Temp folder not found (LastError=%d)\n", GetLastError());
			iReturnCode = RETURN_ERR_INTERNAL;
		}
		break;
	}

	return iReturnCode;
}
Пример #2
0
static bool RegisterShellExtension(bool unregister)
{
	TCHAR szSystem32[260] = { 0 };
	TCHAR szSysWow64[260] = { 0 };
	GetSystemDirectory(szSystem32, sizeof(szSystem32) / sizeof(szSystem32[0]));
	GetSystemWow64Directory(szSysWow64, sizeof(szSysWow64) / sizeof(szSysWow64[0]));

	String progpath = env::GetProgPath();
	String regsvr32 = paths::ConcatPath(szSystem32, _T("regsvr32.exe"));
	String args;
	SHELLEXECUTEINFO sei = { sizeof(sei) };
	sei.lpVerb = _T("runas");
	if (szSysWow64[0])
	{
		args = (unregister ? _T("/s /u \"") : _T("/s \"")) + paths::ConcatPath(progpath, _T("ShellExtensionX64.dll")) + _T("\"");
		sei.lpFile = regsvr32.c_str();
		sei.lpParameters = args.c_str();
		ShellExecuteEx(&sei);

		regsvr32 = paths::ConcatPath(szSysWow64, _T("regsvr32.exe"));
		args = (unregister ? _T("/s /u \"") : _T("/s \"")) + paths::ConcatPath(progpath, _T("ShellExtensionU.dll")) + _T("\"");
		sei.lpFile = regsvr32.c_str();
		sei.lpParameters = args.c_str();
		return !!ShellExecuteEx(&sei);
	}
	else
	{
		args = (unregister ? _T("/s /u \"") : _T("/s \"")) + paths::ConcatPath(progpath, _T("ShellExtensionU.dll")) + _T("\"");
		sei.lpFile = regsvr32.c_str();
		sei.lpParameters = args.c_str();
		return !!ShellExecuteEx(&sei);
	}
}
Пример #3
0
/*
  Get the relative address of the 32-bit LoadLibraryW function from 64-bit code.
  This was originally done via executing a helper program (errout-LLW.exe), but
  I never liked doing that, so now I do it the "hard" way - load the 32-bit
  kernel32.dll directly and search the exports.
*/
BOOL get_LLW32( void )
{
  HMODULE kernel32;
  TCHAR   buf[MAX_PATH];
  UINT	  len;
  PIMAGE_NT_HEADERS32	  pNTHeader;
  PIMAGE_EXPORT_DIRECTORY pExportDir;
  PDWORD  fun_table, name_table;
  PWORD   ord_table;
  PDWORD  pLLW;

  len = GetSystemWow64Directory( buf, MAX_PATH );
  wcscpy( buf + len, L"\\kernel32.dll" );
  // MinGW-w64 had a typo, calling it LINRARY.
  kernel32 = LoadLibraryEx( buf, NULL, 0x20/*LOAD_LIBRARY_AS_IMAGE_RESOURCE*/ );
  if (kernel32 == NULL)
    return FALSE;

  // The handle uses low bits as flags, so strip 'em off.
  pDosHeader = (PIMAGE_DOS_HEADER)((DWORD_PTR)kernel32 & ~0xFFFF);
  pNTHeader  = MakeVA( PIMAGE_NT_HEADERS32, pDosHeader->e_lfanew );
  pExportDir = MakeVA( PIMAGE_EXPORT_DIRECTORY,
		       pNTHeader->OptionalHeader.
			DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].
			 VirtualAddress );

  fun_table  = MakeVA( PDWORD, pExportDir->AddressOfFunctions );
  name_table = MakeVA( PDWORD, pExportDir->AddressOfNames );
  ord_table  = MakeVA( PWORD,  pExportDir->AddressOfNameOrdinals );

  pLLW = bsearch( "LoadLibraryW", name_table, pExportDir->NumberOfNames,
		  sizeof(DWORD), export_cmp );
  if (pLLW == NULL)
  {
    FreeLibrary( kernel32 );
    return FALSE;
  }
  LLW32 = fun_table[ord_table[pLLW - name_table]];

  FreeLibrary( kernel32 );
  return TRUE;
}
Пример #4
0
HRESULT Uninstall( )
{
	HRESULT hr = S_OK;

	TCHAR szCurrentDllPath[MAX_PATH << 1];
	TCHAR szTemp[MAX_PATH << 1];

	LPTSTR lpszFileToDelete = szCurrentDllPath;
	LPTSTR lpszTempAppend = szTemp + GetModuleFileName(g_hModThisDll, szTemp, countof(szTemp));

    StringCbCopy(szCurrentDllPath, sizeof(szCurrentDllPath), szTemp);

#ifdef _WIN64
    // If this 64-bit dll was installed to the default location,
    // uninstall the 32-bit dll if it exists in its default location

    TCHAR lpszDefInstallPath[MAX_PATH + 0x20];
    UINT uSize = GetSystemDirectory(lpszDefInstallPath, MAX_PATH);

    if (uSize && uSize < MAX_PATH)
    {
        LPTSTR lpszPathAppend = lpszDefInstallPath + uSize;

        if (*(lpszPathAppend - 1) != TEXT('\\'))
            *lpszPathAppend++ = TEXT('\\');

        static const TCHAR szFolderAndFilename[] = TEXT("ShellExt") TEXT("\\") TEXT(HASHCHECK_FILENAME_STR);
        SSStaticCpy(lpszPathAppend, szFolderAndFilename);

        // If this 64-bit dll was installed to the default location
        if (StrCmpI(szCurrentDllPath, lpszDefInstallPath) == 0)
        {
            TCHAR lpszSystemWow64[MAX_PATH + 0x20];
            uSize = GetSystemWow64Directory(lpszSystemWow64, MAX_PATH);

            if (uSize && uSize < MAX_PATH)
            {
                LPTSTR lpszSystemWow64Append = lpszSystemWow64 + uSize;

                if (*(lpszSystemWow64Append - 1) != TEXT('\\'))
                    SSCpy2Ch(lpszSystemWow64Append++, TEXT('\\'), 0);

                StringCbCopyEx(lpszDefInstallPath, sizeof(lpszDefInstallPath), lpszSystemWow64, &lpszPathAppend, NULL, 0);

                SSStaticCpy(lpszPathAppend, szFolderAndFilename);

                // If the 32-bit dll exists in its default location
                if (PathFileExists(lpszDefInstallPath))
                {
                    static const TCHAR szRegsvr32[] = TEXT("regsvr32.exe");
                    SSStaticCpy(lpszSystemWow64Append, szRegsvr32);
                    // the lpszSystemWow64 buffer now contains the full regsvr32.exe path

                    TCHAR lpszCommandLine[MAX_PATH + 0x20];
                    LPTSTR lpszCommandLineAppend;
                    
                    static const TCHAR szCommandOpts[] = TEXT("regsvr32.exe /u /i /n /s ");
                    lpszCommandLineAppend = SSStaticCpy(lpszCommandLine, szCommandOpts) - 1;

                    StringCbCopy(lpszCommandLineAppend, sizeof(lpszCommandLine)-sizeof(szCommandOpts), lpszDefInstallPath);

                    STARTUPINFO si;
                    memset(&si, 0, sizeof(si));
                    si.cb = sizeof(si);

                    PROCESS_INFORMATION pi;
                    memset(&pi, 0, sizeof(pi));

                    if (!CreateProcess(lpszSystemWow64, lpszCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
                        return E_FAIL;

                    DWORD dwExit;
                    WaitForSingleObject(pi.hProcess, INFINITE);
                    GetExitCodeProcess(pi.hProcess, &dwExit);
                    CloseHandle(pi.hThread);
                    CloseHandle(pi.hProcess);

                    if (dwExit != 0)
                        return E_FAIL;
                }
            }
        }
    }
#endif

	// Rename the DLL prior to scheduling it for deletion
	*lpszTempAppend++ = TEXT('.');
	SSCpy2Ch(lpszTempAppend, 0, 0);

	for (TCHAR ch = TEXT('0'); ch <= TEXT('9'); ++ch)
	{
		*lpszTempAppend = ch;

		if (MoveFileEx(szCurrentDllPath, szTemp, MOVEFILE_REPLACE_EXISTING))
		{
			lpszFileToDelete = szTemp;
			break;
		}
	}

	// Schedule the DLL to be deleted at shutdown/reboot
	if (!MoveFileEx(lpszFileToDelete, NULL, MOVEFILE_DELAY_UNTIL_REBOOT)) hr = E_FAIL;

	// Unregister
	if (DllUnregisterServer() != S_OK) hr = E_FAIL;

	// Disassociate file extensions; see the comment in DllUnregisterServer for
	// why this step is skipped for Wow64 processes
	if (!Wow64CheckProcess())
	{
		for (UINT i = 0; i < countof(g_szHashExtsTab); ++i)
		{
			HKEY hKey;

			if (hKey = RegOpen(HKEY_CLASSES_ROOT, g_szHashExtsTab[i], NULL, FALSE))
			{
                RegGetSZ(hKey, NULL, szTemp, sizeof(szTemp));
                if (_tcscmp(szTemp, PROGID_STR_HashCheck) == 0)
                    RegDeleteValue(hKey, NULL);
                RegCloseKey(hKey);
			}
		}
	}

	// We don't need the uninstall strings any more...
	RegDelete(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\%s"), CLSNAME_STR_HashCheck);

	return(hr);
}
Пример #5
0
void APILoader::loadLibrary(ApiLibrary apiLibrary, LoadMode mode) {

    if (apiLibrary == LIBRARY_NONE) {
        return;
    }

    std::string libraryName = getLibraryName(apiLibrary);

    if (m_LoadedLibraries.find(libraryName) == m_LoadedLibraries.end()) {

#ifdef _WIN32
        if (mode == LoadMode::LAZY && !GetModuleHandle(libraryName.c_str())) {
            //Will load this later. This is used to avoid calling LoadLibrary() too early in Win.
            return;
        }
#else
        DGL_UNUSED(mode);
#endif

        std::vector<std::string> libSearchPath;


        libSearchPath.push_back("");
#ifdef _WIN32
        char buffer[1000];
#ifndef _WIN64
        if (GetSystemWow64Directory(buffer, sizeof(buffer)) > 0) {
            // we are running 32bit app on 64 bit windows
            libSearchPath.push_back(buffer + std::string("\\"));
        }
#endif
        if (GetSystemDirectory(buffer, sizeof(buffer)) > 0) {
            // we are running on native system (32 on 32 or 64 on 64)
            libSearchPath.push_back(buffer + std::string("\\"));
        }

#ifndef _WIN64
        libSearchPath.push_back("C:\\Windows\\SysWOW64\\");
#endif
        libSearchPath.push_back("C:\\Windows\\System32\\");
        libSearchPath.push_back(".");
#endif

        std::shared_ptr<DynamicLibrary> openGLLibraryHandle = nullptr;

        for (size_t i = 0; i < libSearchPath.size() && !openGLLibraryHandle;
             i++) {
            openGLLibraryHandle = EarlyGlobalState::getDynLoader().getLibrary(
                    (libSearchPath[i] + libraryName).c_str());
        }

#if DGL_HAVE_WA(ANDROID_MISSING_LIBGL)
        if (!openGLLibraryHandle && apiLibrary == LIBRARY_GL) {
            //Some Android devices support 'big' OpenGL, but libGL.so is not provided.
            //Instead fake the load process with *eglGetProcAddress

            openGLLibraryHandle = std::make_shared<FakeDynamicLibrary>(this);
        }
#endif

        if (!openGLLibraryHandle) {
            std::string msg = std::string("Cannot load ") + libraryName +
                              "  system library";
            Os::fatal(msg.c_str());
        } else {
            m_LoadedLibraries[libraryName] = openGLLibraryHandle;
        }
    }

    DynamicLibrary* library = m_LoadedLibraries[libraryName].get();

#ifdef _WIN32
    HookSession hookSession;
#endif

    // g_DirectPointers is now filled with opengl32.dll pointers
    // we will now detour (hook) them all, so g_DirectPointers will still lead
    // to original opengl32.dll, but
    // application will always call us.
    for (int i = 0; i < Entrypoints_NUM; i++) {
        if (!(g_DirectPointers[i].libraryMask & apiLibrary)) {
            // Do not load - entrypoint does not belong to currently loaded API
            continue;
        }

        if (m_LoadedApiLibraries & g_DirectPointers[i].libraryMask) {
            // Do not load - entrypoint belongs to already loaded API
            continue;
        }

        // this sets g_DirectPointers[i].ptr
        setPointer(i, getGLPointer(*library, i));

        if (g_DirectPointers[i].ptr) {
// this entrypoint was loaded from OpenGL32.dll, detour it!
#ifdef _WIN32
            dgl_func_ptr hookPtr = getWrapperPointer(i);
            if (!hookSession.hook(&g_DirectPointers[i].ptr, hookPtr)) {
                Os::fatal("Cannot hook %s() function.", GetEntryPointName(i));
            }
#endif
        }
    }
    if (apiLibrary == LIBRARY_EGL || apiLibrary == LIBRARY_WGL ||
        apiLibrary == LIBRARY_GLX)
        m_GlueLibrary = apiLibrary;

    m_LoadedApiLibraries |= apiLibrary;
}
Пример #6
0
DWORD GetProcRVA( LPCTSTR module, LPCSTR func )
#endif
{
    HMODULE hMod;
    TCHAR   buf[MAX_PATH];
    UINT	  len;
    PIMAGE_NT_HEADERS	  pNTHeader;
    PIMAGE_EXPORT_DIRECTORY pExportDir;
    PDWORD  fun_table, name_table;
    PWORD   ord_table;
    PDWORD  pFunc;
    DWORD   rva;

#ifdef _WIN64
    if (bits == 32)
        len = GetSystemWow64Directory( buf, MAX_PATH );
    else
#endif
        len = GetSystemDirectory( buf, MAX_PATH );
    buf[len++] = '\\';
    wcscpy( buf + len, module );
    hMod = LoadLibraryEx( buf, NULL, LOAD_LIBRARY_AS_IMAGE_RESOURCE );
    if (hMod == NULL)
    {
#ifdef _WIN64
        DEBUGSTR( 1, L"Unable to load %d-bit %s (%lu)!",
                  bits, module, GetLastError() );
#else
        DEBUGSTR( 1, L"Unable to load %s (%lu)!", module, GetLastError() );
#endif
        return 0;
    }
    // The handle uses low bits as flags, so strip 'em off.
    pDosHeader = (PIMAGE_DOS_HEADER)((DWORD_PTR)hMod & ~0xFFFF);
    pNTHeader  = MakeVA( PIMAGE_NT_HEADERS, pDosHeader->e_lfanew );
#ifdef _WIN64
    if (bits == 32)
        pExportDir	= MakeVA( PIMAGE_EXPORT_DIRECTORY,
                              ((PIMAGE_NT_HEADERS32)pNTHeader)->EXPORTDIR.VirtualAddress );
    else
#endif
        pExportDir = MakeVA( PIMAGE_EXPORT_DIRECTORY,
                             pNTHeader->EXPORTDIR.VirtualAddress );
    fun_table  = MakeVA( PDWORD, pExportDir->AddressOfFunctions );
    name_table = MakeVA( PDWORD, pExportDir->AddressOfNames );
    ord_table  = MakeVA( PWORD,  pExportDir->AddressOfNameOrdinals );

    pFunc = bsearch( func, name_table, pExportDir->NumberOfNames,
                     sizeof(DWORD), export_cmp );
    if (pFunc == NULL)
    {
#ifdef _WIN64
        DEBUGSTR( 1, L"Could not find %d-bit %s!", bits, func );
#else
        DEBUGSTR( 1, L"Could not find %s!", func );
#endif
        rva = 0;
    }
    else
    {
        rva = fun_table[ord_table[pFunc - name_table]];
    }
    FreeLibrary( hMod );
    return rva;
}