InstallDriver.cpp
上传用户:looem2003
上传日期:2014-07-20
资源大小:13733k
文件大小:7k
- // InstallDriver.cpp : Defines the entry point for the console application.
- //
- #pragma once
- // Modify the following defines if you have to target a platform prior to the ones specified below.
- // Refer to MSDN for the latest info on corresponding values for different platforms.
- #ifndef WINVER // Allow use of features specific to Windows XP or later.
- #define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows.
- #endif
- #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
- #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
- #endif
- #ifndef _WIN32_WINDOWS // Allow use of features specific to Windows 98 or later.
- #define _WIN32_WINDOWS 0x0410 // Change this to the appropriate value to target Windows Me or later.
- #endif
- #ifndef _WIN32_IE // Allow use of features specific to IE 6.0 or later.
- #define _WIN32_IE 0x0600 // Change this to the appropriate value to target other versions of IE.
- #endif
- //#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
- #define _CRT_SECURE_NO_DEPRECATE
- // Windows Header Files:
- #include <windows.h>
- // C RunTime Header Files
- #include <stdlib.h>
- #include <tchar.h>
- #pragma comment(lib,"Winspool.lib")
- enum WINDOWS_VERSIONS {_UNKNOWN = 0,_W95 = 1,_W98 = 2,_WME = 3,_WNT = 4,_W2000 = 5,_WXP = 6,_W2003 = 7,_WVISTA = 8};
- WINDOWS_VERSIONS __cdecl _get_windows_version();
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- MONITOR_INFO_2 mi2;
- HANDLE hPrinter = NULL;
- WCHAR portName[] = L"Popfax: ";
- DWORD dwReturn = 0, dwStatus = 0;
- DWORD cbNeeded = 0;
- WCHAR txt[100] = L"";
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- TCHAR remPrinter[MAX_PATH] = L"rundll32 printui.dll,PrintUIEntry /dl /n "PopfaxPrinter" /q";
- TCHAR instPrinter[MAX_PATH] = L"rundll32 printui.dll,PrintUIEntry /if /b "PopfaxPrinter" /f imgprinter.inf /r "ImagePrinter Port" /m "PopfaxPrinter"";
- TCHAR pnputilVista[MAX_PATH] = L"install.bat";
- TCHAR instXP[MAX_PATH] = L"install_xp.bat";
-
- LPTSTR instPrn = instPrinter;
- DWORD ver = _get_windows_version();
- if( _W2000 < ver )
- {
- mi2.pDLLName = L"PopFaxLocalMon.dll";
- mi2.pEnvironment = L"Windows NT x86";
- mi2.pName = L"Popfax Local Port";
- if( AddMonitor( NULL, 2, (LPBYTE)&mi2 ) )
- {
- HKEY hKey = HKEY_LOCAL_MACHINE;
- ShellExecute( NULL, L"open", L"net.exe", L"stop Spooler", NULL, SW_HIDE );
- ShellExecute( NULL, L"open", L"sc.exe", L"config Spooler start= auto", NULL, SW_HIDE );
- Sleep( 2000 );
- if( ERROR_SUCCESS == RegCreateKey( hKey, L"SYSTEM\CurrentControlSet\Control\Print\Monitors\Popfax Local Port\Ports", &hKey ) )
- {
- ZeroMemory( txt, sizeof(txt) );
- RegSetValueEx( hKey, L"Popfax:", 0, REG_SZ, (const LPBYTE)txt, (DWORD)1 );
- RegCloseKey( hKey );
- }
- ShellExecute( NULL, L"open", L"net.exe", L"start Spooler", NULL, SW_HIDE );
- }
- }
- else
- {
- HKEY hKey = HKEY_LOCAL_MACHINE;
- if( ERROR_SUCCESS == RegCreateKeyEx( hKey, _T("SYSTEM\CurrentControlSet\Control\Print\Monitors\ImagePrinter Port"), NULL, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, NULL ) )
- {
- TCHAR path[4*MAX_PATH] = _T("");
- RegSetValueEx( hKey, _T("Driver"), NULL, REG_SZ, (const BYTE*)_T("imgport.dll"), (_tcslen(_T("imgport.dll"))+1)*sizeof(TCHAR) );
- GetTempPath( ARRAYSIZE(path), path );
- if( path[_tcslen(path)-1] != _T('\') )
- _tcscat_s( path, ARRAYSIZE(path)-_tcslen(path), _T("\") );
- RegSetValueEx( hKey, _T("path"), NULL, REG_SZ, (const BYTE*)path, (_tcslen(path)+1)*sizeof(TCHAR) );
- RegSetValueEx( hKey, _T("name"), NULL, REG_SZ, (const BYTE*)_T("ImagePrinter Port"), (_tcslen(_T("ImagePrinter Port"))+1)*sizeof(TCHAR) );
- RegSetValueEx( hKey, _T("format"), NULL, REG_SZ, (const BYTE*)_T("tif"), (_tcslen(_T("tif"))+1)*sizeof(TCHAR) );
- RegSetValueEx( hKey, _T("format_ext"), NULL, REG_SZ, (const BYTE*)_T("G3"), (_tcslen(_T("G3"))+1)*sizeof(TCHAR) );
- RegCloseKey( hKey );
- }
- }
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- ZeroMemory( &pi, sizeof(pi) );
- if( CreateProcess( NULL, remPrinter, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
- {
- // Wait until child process exits.
- WaitForSingleObject( pi.hProcess, INFINITE );
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }
- ZeroMemory( &si, sizeof(si) );
- si.cb = sizeof(si);
- si.dwFlags = STARTF_USESHOWWINDOW;
- si.wShowWindow = SW_HIDE;
- ZeroMemory( &pi, sizeof(pi) );
- if( _W2000 < ver )//礤
- {
- if( _WVISTA == ver )
- {
- if( CreateProcess( NULL, pnputilVista, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
- {
- // Wait until child process exits.
- WaitForSingleObject( pi.hProcess, INFINITE );
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }
- }
- if( CreateProcess( NULL, instXP, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
- {
- // Wait until child process exits.
- WaitForSingleObject( pi.hProcess, INFINITE );
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }
- return 0;
- }
- if( CreateProcess( NULL, instPrinter, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) )
- {
- // Wait until child process exits.
- WaitForSingleObject( pi.hProcess, INFINITE );
- // Close process and thread handles.
- CloseHandle( pi.hProcess );
- CloseHandle( pi.hThread );
- }
- return 0;
- }
- WINDOWS_VERSIONS __cdecl _get_windows_version()
- {
- OSVERSIONINFO verInfo;
- verInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- GetVersionEx(&verInfo);
- switch( verInfo.dwPlatformId )
- {
- case VER_PLATFORM_WIN32_WINDOWS:
- if( verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 0 )
- return _W95;
- if( verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 10 )
- return _W98;
- if( verInfo.dwMajorVersion == 4 && verInfo.dwMinorVersion == 90 )
- return _WME;
- case VER_PLATFORM_WIN32_NT:
- if ( verInfo.dwMajorVersion <= 4 )
- return _WNT;
- if ( verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 0 )
- return _W2000;
- if ( verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 1 )
- return _WXP;
- if ( verInfo.dwMajorVersion == 5 && verInfo.dwMinorVersion == 2 )
- return _W2003;
- if ( verInfo.dwMajorVersion == 6 && verInfo.dwMinorVersion == 0 )
- return _WVISTA;
- }
- return _UNKNOWN;
- }