HRESULT FreeRegistryInformation(_Inout_ PSCANINFO pScanInfo, _In_ PWIASANE_Context pContext) { HANDLE hHeap; if (!pScanInfo || !pContext) return E_INVALIDARG; hHeap = pScanInfo->DeviceIOHandles[1]; if (pContext->pszHost) HeapSafeFree(hHeap, 0, pContext->pszHost); if (pContext->pszName) HeapSafeFree(hHeap, 0, pContext->pszName); if (pContext->pszUsername) HeapSafeFree(hHeap, 0, pContext->pszUsername); if (pContext->pszPassword) HeapSafeFree(hHeap, 0, pContext->pszPassword); HeapSafeFree(hHeap, 0, pContext); return S_OK; }
DWORD DriverInstall(_In_ HANDLE hHeap, _In_ LPTSTR lpInfPath, _In_ int nCmdShow) { INSTALLERINFO installerInfo; DWORD dwFlags; BOOL needReboot; LPVOID lpData; DWORD res; Trace(TEXT("DriverInstall(%s, %d)"), lpInfPath, nCmdShow); res = CreateInstallInfo(hHeap, &installerInfo, &lpData); if (res == ERROR_SUCCESS) { if (nCmdShow == SW_HIDE) dwFlags = DRIVER_PACKAGE_SILENT; else dwFlags = 0; res = DriverPackageInstall(lpInfPath, dwFlags, &installerInfo, &needReboot); if (res != ERROR_SUCCESS && res != ERROR_NO_SUCH_DEVINST) Trace(TEXT("DriverPackageInstall failed: %08X"), res); if (lpData) HeapSafeFree(hHeap, 0, lpData); } return res; }
LPSTR WINAPI StringConvWToA(_In_ HANDLE hHeap, _In_ LPWSTR lpszString) { LPSTR lpszCopy; int iLength; if (!lpszString) return NULL; iLength = WideCharToMultiByte(CP_ACP, 0, lpszString, -1, NULL, 0, NULL, NULL); if (!iLength) return NULL; if ((iLength+1) <= iLength) return NULL; iLength += 1; lpszCopy = (LPSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, iLength); if (!lpszCopy) return NULL; iLength = WideCharToMultiByte(CP_ACP, 0, lpszString, -1, lpszCopy, iLength, NULL, NULL); if (!iLength) { HeapSafeFree(hHeap, 0, lpszCopy); return NULL; } return lpszCopy; }
LPWSTR WINAPI StringDupW(_In_ HANDLE hHeap, _In_ LPCWSTR lpszString) { LPWSTR lpszCopy; size_t cbCopy; HRESULT hr; if (!lpszString) return NULL; hr = StringCbLengthW(lpszString, (STRSAFE_MAX_CCH-1) * sizeof(WCHAR), &cbCopy); if (FAILED(hr)) return NULL; if ((cbCopy + sizeof(WCHAR)) <= cbCopy) return NULL; cbCopy += sizeof(WCHAR); lpszCopy = (LPWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cbCopy); if (!lpszCopy) return NULL; hr = StringCbCopyNW(lpszCopy, cbCopy, lpszString, cbCopy); if (FAILED(hr)) { HeapSafeFree(hHeap, 0, lpszCopy); return NULL; } return lpszCopy; }
HRESULT StringCchAVPrintfW(_In_ HANDLE hHeap, _Outptr_result_nullonfailure_ LPWSTR *plpszDest, _Out_opt_ size_t *pcchDest, _In_ LPCWSTR lpszFormat, _In_ va_list argList) { LPWSTR lpszDest; int cchDest; if (!hHeap || !plpszDest || !lpszFormat) return STRSAFE_E_INVALID_PARAMETER; *plpszDest = NULL; if (pcchDest) *pcchDest = 0; cchDest = _vscwprintf(lpszFormat, argList); if (cchDest < 0 || cchDest > STRSAFE_MAX_CCH) return STRSAFE_E_INVALID_PARAMETER; if ((cchDest+1) <= cchDest) return STRSAFE_E_INVALID_PARAMETER; cchDest += 1; lpszDest = (LPWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cchDest * sizeof(WCHAR)); if (lpszDest) { cchDest = vswprintf_s(lpszDest, cchDest, lpszFormat, argList); if (cchDest >= 0) { *plpszDest = lpszDest; if (pcchDest) *pcchDest = cchDest; return S_OK; } else { HeapSafeFree(hHeap, 0, lpszDest); } } return STRSAFE_E_INSUFFICIENT_BUFFER; }
LPWSTR WINAPI StringConvAToW(_In_ HANDLE hHeap, _In_ LPSTR lpszString) { LPWSTR lpszCopy; int iLength; if (!lpszString) return NULL; iLength = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, NULL, 0); if (!iLength) return NULL; if ((iLength+1) <= iLength) return NULL; iLength += 1; lpszCopy = (LPWSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, iLength * sizeof(TCHAR)); if (!lpszCopy) return NULL; iLength = MultiByteToWideChar(CP_ACP, 0, lpszString, -1, lpszCopy, iLength); if (!iLength) { HeapSafeFree(hHeap, 0, lpszCopy); return NULL; } return lpszCopy; }
int WINAPI MessageBoxR(_In_ HANDLE hHeap, _In_opt_ HINSTANCE hInstance, _In_ HWND hWnd, _In_ UINT uText, _In_ UINT uCaption, _In_ UINT uType) { LPTSTR lpszText, lpszCaption; int msgBoxID; msgBoxID = 0; if (LoadAString(hHeap, hInstance, uText, &lpszText, NULL)) { if (LoadAString(hHeap, hInstance, uCaption, &lpszCaption, NULL)) { msgBoxID = MessageBox(hWnd, lpszText, lpszCaption, uType); HeapSafeFree(hHeap, 0, (LPVOID) lpszCaption); } HeapSafeFree(hHeap, 0, (LPVOID) lpszText); } return msgBoxID; }
DWORD DriverUninstall(_In_ HANDLE hHeap, _In_ LPTSTR lpInfPath, _In_ int nCmdShow) { INSTALLERINFO installerInfo; LPTSTR lpDsInfPath; DWORD cbDsInfPath, dwFlags; BOOL needReboot; LPVOID lpData; DWORD res; Trace(TEXT("DriverUninstall(%s, %d)"), lpInfPath, nCmdShow); cbDsInfPath = 0; res = DriverPackageGetPath(lpInfPath, NULL, &cbDsInfPath); if (res == ERROR_INSUFFICIENT_BUFFER) { lpDsInfPath = (LPTSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, cbDsInfPath * sizeof(TCHAR)); if (lpDsInfPath) { res = DriverPackageGetPath(lpInfPath, lpDsInfPath, &cbDsInfPath); if (res == ERROR_SUCCESS) { Trace(TEXT("DsInfPath: %s"), lpDsInfPath); res = CreateInstallInfo(hHeap, &installerInfo, &lpData); if (res == ERROR_SUCCESS) { if (nCmdShow == SW_HIDE) dwFlags = DRIVER_PACKAGE_SILENT; else dwFlags = 0; res = DriverPackageUninstall(lpDsInfPath, dwFlags, &installerInfo, &needReboot); if (res != ERROR_SUCCESS) Trace(TEXT("DriverPackageUninstall failed: %08X"), res); if (lpData) HeapSafeFree(hHeap, 0, lpData); } } else Trace(TEXT("DriverPackageGetPath 2 failed: %08X"), res); HeapSafeFree(hHeap, 0, lpDsInfPath); } else Trace(TEXT("HeapAlloc failed")); } else Trace(TEXT("DriverPackageGetPath 1 failed: %08X"), res); return res; }
BOOL WINAPI SetDlgItemTextR(_In_ HANDLE hHeap, _In_opt_ HINSTANCE hInstance, _In_ HWND hDlg, _In_ int nIDDlgItem, _In_ UINT uText) { LPTSTR lpszText; BOOL res; if (LoadAString(hHeap, hInstance, uText, &lpszText, NULL)) { res = SetDlgItemText(hDlg, nIDDlgItem, lpszText); HeapSafeFree(hHeap, 0, (LPVOID) lpszText); } else { res = SetDlgItemText(hDlg, nIDDlgItem, TEXT("")); } return res; }
BOOL WINAPI LoadAString(_In_ HANDLE hHeap, _In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Outptr_result_nullonfailure_ LPTSTR *plpszString, _Out_opt_ int *piLength) { LPTSTR lpszString; int iLength; if (!plpszString) return FALSE; *plpszString = NULL; if (piLength) *piLength = 0; iLength = LoadString(hInstance, uID, (LPTSTR) &lpszString, 0); if (iLength <= 0) return FALSE; if ((iLength+1) <= iLength) return FALSE; iLength += 1; lpszString = (LPTSTR) HeapAlloc(hHeap, HEAP_ZERO_MEMORY, iLength * sizeof(TCHAR)); if (!lpszString) return FALSE; iLength = LoadString(hInstance, uID, lpszString, iLength); if (iLength <= 0) { HeapSafeFree(hHeap, 0, lpszString); return FALSE; } *plpszString = lpszString; if (piLength) *piLength = iLength; return TRUE; }
HRESULT FreeScannerDefaults(_Inout_ PSCANINFO pScanInfo, _Inout_ PWIASANE_Context pContext) { if (!pScanInfo || !pContext) return E_INVALIDARG; if (!pContext->pValues) return S_OK; if (pContext->pValues->pszModeThreshold) { HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues->pszModeThreshold); pContext->pValues->pszModeThreshold = NULL; } if (pContext->pValues->pszModeGrayscale) { HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues->pszModeGrayscale); pContext->pValues->pszModeGrayscale = NULL; } if (pContext->pValues->pszModeColor) { HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues->pszModeColor); pContext->pValues->pszModeColor = NULL; } if (pContext->pValues->pszSourceFlatbed) { HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues->pszSourceFlatbed); pContext->pValues->pszSourceFlatbed = NULL; } if (pContext->pValues->pszSourceADF) { HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues->pszSourceADF); pContext->pValues->pszSourceADF = NULL; } if (pContext->pValues->pszSourceDuplex) { HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues->pszSourceDuplex); pContext->pValues->pszSourceDuplex = NULL; } HeapSafeFree(pScanInfo->DeviceIOHandles[1], 0, pContext->pValues); pContext->pValues = NULL; return S_OK; }
HRESULT ReadRegistryInformation(_Inout_ PSCANINFO pScanInfo, _Inout_ PWIASANE_Context pContext) { HKEY hKey, hOpenKey; LPTSTR lpszValue; DWORD dwValue; HANDLE hHeap; LSTATUS st; if (!pScanInfo || !pContext) return E_INVALIDARG; hHeap = pScanInfo->DeviceIOHandles[1]; hKey = (HKEY) pScanInfo->DeviceIOHandles[2]; hOpenKey = NULL; // // Open DeviceData section to read driver specific information // st = RegOpenKeyEx(hKey, TEXT("DeviceData"), 0, KEY_QUERY_VALUE|KEY_READ, &hOpenKey); if (st != ERROR_SUCCESS) return HRESULT_FROM_WIN32(st); pContext->usPort = WINSANE_DEFAULT_PORT; st = ReadRegistryLong(hHeap, hOpenKey, TEXT("Port"), &dwValue); if (st != ERROR_SUCCESS) { RegCloseKey(hOpenKey); return HRESULT_FROM_WIN32(st); } pContext->usPort = (USHORT) dwValue; st = ReadRegistryString(hHeap, hOpenKey, TEXT("Host"), &lpszValue, NULL); if (st != ERROR_SUCCESS) { RegCloseKey(hOpenKey); return HRESULT_FROM_WIN32(st); } if (pContext->pszHost) HeapSafeFree(hHeap, 0, pContext->pszHost); pContext->pszHost = lpszValue; st = ReadRegistryString(hHeap, hOpenKey, TEXT("Name"), &lpszValue, NULL); if (st != ERROR_SUCCESS) { RegCloseKey(hOpenKey); return HRESULT_FROM_WIN32(st); } if (pContext->pszName) HeapSafeFree(hHeap, 0, pContext->pszName); pContext->pszName = lpszValue; st = ReadRegistryString(hHeap, hOpenKey, TEXT("Username"), &lpszValue, NULL); if (st != ERROR_SUCCESS && st != ERROR_FILE_NOT_FOUND) { RegCloseKey(hOpenKey); return HRESULT_FROM_WIN32(st); } if (pContext->pszUsername) HeapSafeFree(hHeap, 0, pContext->pszUsername); pContext->pszUsername = lpszValue; st = ReadRegistryString(hHeap, hOpenKey, TEXT("Password"), &lpszValue, NULL); if (st != ERROR_SUCCESS && st != ERROR_FILE_NOT_FOUND) { RegCloseKey(hOpenKey); return HRESULT_FROM_WIN32(st); } if (pContext->pszPassword) HeapSafeFree(hHeap, 0, pContext->pszPassword); pContext->pszPassword = lpszValue; RegCloseKey(hOpenKey); return S_OK; }
DWORD CreateInstallInfo(_In_ HANDLE hHeap, _Out_ PINSTALLERINFO pInstallerInfo, _Outptr_result_maybenull_ LPVOID *plpData) { TCHAR szModuleFileName[MAX_PATH + 1]; LPTSTR lpszSubBlock, lpszValue; LPWORD lpwLanguage; DWORD dwLen, dwHandle; size_t cbSubBlock; HMODULE hModule; UINT cbSize; HRESULT hr; BOOL res; if (!pInstallerInfo || !plpData) return ERROR_INVALID_PARAMETER; ZeroMemory(pInstallerInfo, sizeof(INSTALLERINFO)); *plpData = NULL; hModule = GetModuleInstance(); if (!hModule) return ERROR_OUTOFMEMORY; dwLen = GetModuleFileName(hModule, szModuleFileName, MAX_PATH); if (!dwLen) return GetLastError(); dwLen = GetFileVersionInfoSize(szModuleFileName, &dwHandle); if (!dwLen) return GetLastError(); *plpData = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, dwLen); if (!*plpData) return ERROR_OUTOFMEMORY; LoadString(hModule, IDS_APPLICATION_ID, (LPWSTR) &pInstallerInfo->pApplicationId, 0); res = GetFileVersionInfo(szModuleFileName, dwHandle, dwLen, *plpData); if (res) { res = VerQueryValue(*plpData, TEXT("\\VarFileInfo\\Translation"), (LPVOID*) &lpwLanguage, &cbSize); if (res) { Trace(TEXT("Translation: %04x%04x (%d)"), lpwLanguage[0], lpwLanguage[1], cbSize); hr = StringCbAPrintf(hHeap, &lpszSubBlock, &cbSubBlock, TEXT("\\StringFileInfo\\%04x%04x\\ProductName"), lpwLanguage[0], lpwLanguage[1]); if (SUCCEEDED(hr)) { Trace(TEXT("SubBlock: %s (%d)"), lpszSubBlock, cbSubBlock); res = VerQueryValue(*plpData, lpszSubBlock, (LPVOID*) &lpszValue, &cbSize); if (res) { pInstallerInfo->pDisplayName = lpszValue; pInstallerInfo->pProductName = lpszValue; } else Trace(TEXT("VerQueryValue 2 failed: %08X"), GetLastError()); HeapSafeFree(hHeap, 0, lpszSubBlock); } else Trace(TEXT("StringCbAPrintf 1 failed: %08X"), hr); hr = StringCbAPrintf(hHeap, &lpszSubBlock, &cbSubBlock, TEXT("\\StringFileInfo\\%04x%04x\\CompanyName"), lpwLanguage[0], lpwLanguage[1]); if (SUCCEEDED(hr)) { Trace(TEXT("SubBlock: %s (%d)"), lpszSubBlock, cbSubBlock); res = VerQueryValue(*plpData, lpszSubBlock, (LPVOID*) &lpszValue, &cbSize); if (res) { pInstallerInfo->pMfgName = lpszValue; } else Trace(TEXT("VerQueryValue 3 failed: %08X"), GetLastError()); HeapSafeFree(hHeap, 0, lpszSubBlock); } else Trace(TEXT("StringCbAPrintf 2 failed: %08X"), hr); } else Trace(TEXT("VerQueryValue 1 failed: %08X"), GetLastError()); } else Trace(TEXT("GetFileVersionInfo failed: %08X"), GetLastError()); Trace(TEXT("ApplicationID: %s"), pInstallerInfo->pApplicationId); Trace(TEXT("DisplayName: %s"), pInstallerInfo->pDisplayName); Trace(TEXT("ProductName: %s"), pInstallerInfo->pProductName); Trace(TEXT("MfgName: %s"), pInstallerInfo->pMfgName); return ERROR_SUCCESS; }