testtoolsconn/wintap/driver/tapinstaller.cpp
changeset 0 3da2a79470a7
equal deleted inserted replaced
-1:000000000000 0:3da2a79470a7
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <windows.h>
       
    20 #include <stdio.h>
       
    21 #include <setupapi.h>
       
    22 #include <cfgmgr32.h>
       
    23 #include <malloc.h>
       
    24 #include <newdev.h>
       
    25 
       
    26 #ifdef _UNICODE
       
    27 #define UPDATEDRIVERFORPLUGANDPLAYDEVICES "UpdateDriverForPlugAndPlayDevicesW"
       
    28 #else
       
    29 #define UPDATEDRIVERFORPLUGANDPLAYDEVICES "UpdateDriverForPlugAndPlayDevicesA"
       
    30 #endif
       
    31 
       
    32 typedef BOOL (WINAPI *PUPDATEDRIVERFORPLUGANDPLAYDEVICES)(HWND hwndParent,
       
    33                                                          LPCTSTR HardwareId,
       
    34                                                          LPCTSTR FullInfPath,
       
    35                                                          DWORD InstallFlags,
       
    36                                                          PBOOL bRebootRequired OPTIONAL
       
    37                                                          );
       
    38 
       
    39 int UpdateDevice(LPCTSTR inf, LPCTSTR hwid)
       
    40 {
       
    41     HMODULE hMod = NULL;
       
    42     int nRet = -1;
       
    43     PUPDATEDRIVERFORPLUGANDPLAYDEVICES pUpdate;
       
    44     BOOL bReboot = FALSE;
       
    45     DWORD dwFlags = 0;
       
    46     DWORD res;
       
    47     TCHAR InfPath[MAX_PATH];
       
    48 
       
    49     res = GetFullPathName(inf,MAX_PATH,InfPath,NULL);
       
    50     if((res < MAX_PATH) && (res != 0)) {
       
    51 		if(GetFileAttributes(InfPath) != (DWORD)(-1)) {
       
    52 			inf = InfPath;
       
    53 			dwFlags |= INSTALLFLAG_FORCE;
       
    54 
       
    55 			hMod = LoadLibrary(TEXT("newdev.dll"));
       
    56 			if(hMod) {
       
    57 				pUpdate = (PUPDATEDRIVERFORPLUGANDPLAYDEVICES)GetProcAddress(hMod,UPDATEDRIVERFORPLUGANDPLAYDEVICES);
       
    58 				if(pUpdate)
       
    59 				{
       
    60 					if(pUpdate(NULL,hwid,inf,dwFlags,&bReboot)) {
       
    61 						nRet = 0;
       
    62 					}
       
    63 				}
       
    64 			}
       
    65 		}
       
    66 	}
       
    67 
       
    68     if(hMod) {
       
    69         FreeLibrary(hMod);
       
    70     }
       
    71 
       
    72     return -1;
       
    73 }
       
    74 
       
    75 
       
    76 
       
    77 int install(LPCTSTR inf, LPCTSTR hwid)
       
    78 {
       
    79 	
       
    80     HDEVINFO hDeviceInfoSet = INVALID_HANDLE_VALUE;
       
    81     SP_DEVINFO_DATA DeviceInfoData;
       
    82     GUID ClassGUID;
       
    83     TCHAR ClassName[MAX_CLASS_NAME_LEN];
       
    84     TCHAR hwIdList[LINE_LEN+4];
       
    85     TCHAR InfPath[MAX_PATH];
       
    86     int nRet = -1;
       
    87     DWORD flags = 0;
       
    88 
       
    89     if(GetFullPathName(inf,MAX_PATH,InfPath,NULL) < MAX_PATH) {
       
    90 		
       
    91 		ZeroMemory(hwIdList,sizeof(hwIdList));
       
    92 		lstrcpyn(hwIdList,hwid,LINE_LEN);
       
    93 
       
    94 		if (SetupDiGetINFClass(InfPath,&ClassGUID,ClassName,sizeof(ClassName)/sizeof(ClassName[0]),0))
       
    95 		{
       
    96 
       
    97 			hDeviceInfoSet = SetupDiCreateDeviceInfoList(&ClassGUID,0);
       
    98 			if(hDeviceInfoSet != INVALID_HANDLE_VALUE)
       
    99 			{
       
   100 
       
   101 				DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
       
   102 				if (SetupDiCreateDeviceInfo(hDeviceInfoSet,
       
   103 					ClassName,
       
   104 					&ClassGUID,
       
   105 					NULL,
       
   106 					0,
       
   107 					DICD_GENERATE_ID,
       
   108 					&DeviceInfoData))
       
   109 				{
       
   110 
       
   111 					if(SetupDiSetDeviceRegistryProperty(hDeviceInfoSet,
       
   112 						&DeviceInfoData,
       
   113 						SPDRP_HARDWAREID,
       
   114 						(LPBYTE)hwIdList,
       
   115 						(lstrlen(hwIdList)+1+1)*sizeof(TCHAR)))
       
   116 					{
       
   117 
       
   118 						if (SetupDiCallClassInstaller(DIF_REGISTERDEVICE,
       
   119 							hDeviceInfoSet,
       
   120 							&DeviceInfoData))
       
   121 						{
       
   122 							// update the driver for the device we just created
       
   123 							//
       
   124 							nRet = UpdateDevice(inf,hwid);
       
   125 						}
       
   126 					}
       
   127 				}
       
   128 
       
   129 				if (hDeviceInfoSet != INVALID_HANDLE_VALUE) {
       
   130 					SetupDiDestroyDeviceInfoList(hDeviceInfoSet);
       
   131 				}
       
   132 			}
       
   133 		}
       
   134 	}
       
   135 	return nRet;
       
   136 }
       
   137 
       
   138 
       
   139 
       
   140 BOOL IsWow64()
       
   141 {
       
   142 	typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE hProcess, BOOL *Wow64Process);
       
   143 	BOOL bIsWow64 = FALSE;
       
   144 	LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle("kernel32"),"IsWow64Process");
       
   145 	if( NULL != fnIsWow64Process)
       
   146 	{
       
   147 		if( !fnIsWow64Process( GetCurrentProcess(), &bIsWow64))
       
   148 		{
       
   149 		// handle error
       
   150 		}
       
   151 	}
       
   152 	return bIsWow64;
       
   153 }
       
   154 
       
   155 void CreateChildProcessAndWaitUntilDone(const LPSTR strCmdLine) 
       
   156 {
       
   157 	PROCESS_INFORMATION piProcInfo; 
       
   158 	STARTUPINFO siStartInfo;
       
   159 
       
   160 	// Set up members of STARTUPINFO structure.
       
   161 	siStartInfo.cb = sizeof(STARTUPINFO); 
       
   162 	siStartInfo.lpReserved = NULL;
       
   163 	siStartInfo.lpReserved2 = NULL; 
       
   164 	siStartInfo.cbReserved2 = 0;
       
   165 	siStartInfo.lpDesktop = NULL; 
       
   166 	siStartInfo.dwFlags = 0;
       
   167 
       
   168 
       
   169 	// Create the child process.
       
   170 	CreateProcess(
       
   171 		NULL,
       
   172 		strCmdLine,
       
   173 		NULL, // process security attributes
       
   174 		NULL, // primary thread security attributes
       
   175 		0, // handles are inherited
       
   176 		0, // creation flags
       
   177 		NULL, // use parent's environment
       
   178 		NULL, // use parent's current directory
       
   179 		&siStartInfo, // STARTUPINFO pointer
       
   180 		&piProcInfo); // receives PROCESS_INFORMATION
       
   181 
       
   182 	// Wait for the processs to finish
       
   183 	DWORD rc = WaitForSingleObject(
       
   184 	piProcInfo.hProcess, // process handle
       
   185 	INFINITE); 
       
   186 }
       
   187 void main(int argc, LPTSTR argv[])
       
   188 {
       
   189 	int a=0;
       
   190 
       
   191 	if(IsWow64())
       
   192 	{
       
   193 		SetCurrentDirectory("x64driver");
       
   194 		CreateChildProcessAndWaitUntilDone("tapinstaller.x64.exe");
       
   195 	}
       
   196 	else 
       
   197 	{
       
   198 		OSVERSIONINFO ver;
       
   199 		ver.dwOSVersionInfoSize = sizeof(ver);
       
   200 		GetVersionEx(&ver);
       
   201 		if(ver.dwMajorVersion == 6)
       
   202 		{
       
   203 			LPTSTR inf="tap0901.inf";
       
   204 			LPTSTR hwid = "tap0901";
       
   205 			install(inf,hwid);
       
   206 		}
       
   207 		else 
       
   208 		{
       
   209 			LPTSTR inf="OemWin2k.inf";
       
   210 			LPTSTR hwid = "tap0801";
       
   211 			install(inf,hwid);
       
   212 		}
       
   213 	}
       
   214 	return;
       
   215 }