Sample Code
Windows Driver Samples/ Toaster Sample Driver/ C++/ coinstaller/ coinst.c/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | // 45678901234567890123456789012345678901234567890123456789012345678901234567890 //+--------------------------------------------------------------------------- // // Microsoft Windows // Copyright (C) Microsoft Corporation, 1999. // // File: COINST.C // // Contents: co-installer hook. // // Notes: For a complete description of CoInstallers, please see the // Microsoft Windows 2000 DDK Documentation // // // Revision History: // Added FriendlyName interface // // Modified to create a friendlyname in the first callback to // to install device rather than in post-processing. // - July 28, 2000 // //---------------------------------------------------------------------------- // // Annotation to indicate to prefast that this is nondriver user-mode code. // #include <DriverSpecs.h> __user_code #include <windows.h> #include <setupapi.h> #include <stdio.h> #include <strsafe.h> //+--------------------------------------------------------------------------- // // WARNING! // // A Coinstaller must not generate any popup to the user. // it should provide appropriate defaults. // // OutputDebugString should be fine... // #if DBG #define DbgOut(Text) OutputDebugString(TEXT("CoInstaller: " Text "\n")) #else #define DbgOut(Text) #endif //+--------------------------------------------------------------------------- // // Function: MyOpenInfFile // // Purpose: Will open the handle to the INF file being installed // // Arguments: // DeviceInfoSet [in] // DeviceInfoData [in] // ErrorLine [out] // Optional, See SetupOpenInfFile // // Returns: HINF Handle of INF file // HINF MyOpenInfFile ( _In_ HDEVINFO DeviceInfoSet, _In_ PSP_DEVINFO_DATA DeviceInfoData, _Out_opt_ PUINT ErrorLine ) { SP_DRVINFO_DATA DriverInfoData; SP_DRVINFO_DETAIL_DATA DriverInfoDetailData; DWORD Status; HINF FileHandle; if (NULL != ErrorLine) { *ErrorLine = 0; } DriverInfoData.cbSize = sizeof (SP_DRVINFO_DATA); if (!SetupDiGetSelectedDriver( DeviceInfoSet, DeviceInfoData, &DriverInfoData)) { DbgOut( "Fail: SetupDiGetSelectedDriver" ); return INVALID_HANDLE_VALUE; } DriverInfoDetailData.cbSize = sizeof (SP_DRVINFO_DETAIL_DATA); if (!SetupDiGetDriverInfoDetail(DeviceInfoSet, DeviceInfoData, &DriverInfoData, &DriverInfoDetailData, sizeof (SP_DRVINFO_DETAIL_DATA), NULL)) { if ((Status = GetLastError()) == ERROR_INSUFFICIENT_BUFFER) { // We don't need the extended information. Ignore. } else { DbgOut( "Fail: SetupDiGetDriverInfoDetail" ); return INVALID_HANDLE_VALUE; } } if (INVALID_HANDLE_VALUE == (FileHandle = SetupOpenInfFile( DriverInfoDetailData.InfFileName, NULL, INF_STYLE_WIN4, ErrorLine))) { DbgOut( "Fail: SetupOpenInfFile" ); } return FileHandle; } //+--------------------------------------------------------------------------- // // Function: ToasterCoInstaller // // Purpose: Responds to co-installer messages // // Arguments: // InstallFunction [in] // DeviceInfoSet [in] // DeviceInfoData [in] // Context [inout] // // Returns: NO_ERROR, ERROR_DI_POSTPROCESSING_REQUIRED, or an error code. // DWORD CALLBACK ToasterCoInstaller ( _In_ DI_FUNCTION InstallFunction, _In_ HDEVINFO DeviceInfoSet, _In_ PSP_DEVINFO_DATA DeviceInfoData, OPTIONAL _Inout_ PCOINSTALLER_CONTEXT_DATA Context ) { switch (InstallFunction) { case DIF_INSTALLDEVICE: if (!Context->PostProcessing) { TCHAR FriendlyName[MAX_PATH]; BOOL fSuccess=FALSE; DWORD dwRegType, UINumber; size_t len; DbgOut( "DIF_INSTALLDEVICE" ); // // We wil create here a friendly name for this device // based on it's serial number. // The bus driver returns the serial No. in the UINumber. // field of the device capabiliities structure. // So let us get that first . // fSuccess = SetupDiGetDeviceRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_UI_NUMBER, &dwRegType, ( BYTE *) &UINumber, sizeof (UINumber), NULL); if (fSuccess) { // // Cook a FriendlyName and add it to the registry // if (SUCCEEDED(StringCbPrintf(FriendlyName, sizeof (FriendlyName), TEXT( "ToasterDevice%02u" ), UINumber))) { if (SUCCEEDED(StringCbLength(FriendlyName, sizeof (FriendlyName), &len))) { fSuccess = SetupDiSetDeviceRegistryProperty(DeviceInfoSet, DeviceInfoData, SPDRP_FRIENDLYNAME, ( BYTE *)FriendlyName, ( DWORD )len ); if (!fSuccess) { DbgOut( "SetupDiSetDeviceRegistryProperty failed!" ); } } } } // You can use PrivateData to pass Data needed for PostProcessing // Context->PrivateData = Something; return ERROR_DI_POSTPROCESSING_REQUIRED; //Set for PostProcessing } else // post processing { INFCONTEXT InfContext; HINF InfFile; // // Sample code to show how you can process a custom section // in your INF file. // DbgOut( "DIF_INSTALLDEVICE PostProcessing" ); if (INVALID_HANDLE_VALUE == (InfFile = MyOpenInfFile(DeviceInfoSet,DeviceInfoData,NULL))) { return GetLastError(); } if (SetupFindFirstLine(InfFile, // InfHandle TEXT( "MySection" ), TEXT( "MySpecialFlag" ), &InfContext)) { DbgOut( "DIF_INSTALLDEVICE MySpecicalFlag, Do something here!" ); } } break ; case DIF_REMOVE: DbgOut( "DIF_REMOVE" ); break ; case DIF_SELECTDEVICE: DbgOut( "DIF_SELECTDEVICE" ); break ; case DIF_ASSIGNRESOURCES: DbgOut( "DIF_ASSIGNRESOURCES" ); break ; case DIF_PROPERTIES: DbgOut( "DIF_PROPERTIES" ); break ; case DIF_FIRSTTIMESETUP: DbgOut( "DIF_FIRSTTIMESETUP" ); break ; case DIF_FOUNDDEVICE: DbgOut( "DIF_FOUNDDEVICE" ); break ; case DIF_SELECTCLASSDRIVERS: DbgOut( "DIF_SELECTCLASSDRIVERS" ); break ; case DIF_VALIDATECLASSDRIVERS: DbgOut( "DIF_VALIDATECLASSDRIVERS" ); break ; case DIF_INSTALLCLASSDRIVERS: DbgOut( "DIF_INSTALLCLASSDRIVERS" ); break ; case DIF_CALCDISKSPACE: DbgOut( "DIF_CALCDISKSPACE" ); break ; case DIF_DESTROYPRIVATEDATA: DbgOut( "DIF_DESTROYPRIVATEDATA" ); break ; case DIF_VALIDATEDRIVER: DbgOut( "DIF_VALIDATEDRIVER" ); break ; case DIF_MOVEDEVICE: DbgOut( "DIF_MOVEDEVICE" ); break ; case DIF_DETECT: DbgOut( "DIF_DETECT" ); break ; case DIF_INSTALLWIZARD: DbgOut( "DIF_INSTALLWIZARD" ); break ; case DIF_DESTROYWIZARDDATA: DbgOut( "DIF_DESTROYWIZARDDATA" ); break ; case DIF_PROPERTYCHANGE: DbgOut( "DIF_PROPERTYCHANGE" ); break ; case DIF_ENABLECLASS: DbgOut( "DIF_ENABLECLASS" ); break ; case DIF_DETECTVERIFY: DbgOut( "DIF_DETECTVERIFY" ); break ; case DIF_INSTALLDEVICEFILES: DbgOut( "DIF_INSTALLDEVICEFILES" ); break ; case DIF_ALLOW_INSTALL: DbgOut( "DIF_ALLOW_INSTALL" ); break ; case DIF_SELECTBESTCOMPATDRV: DbgOut( "DIF_SELECTBESTCOMPATDRV" ); break ; case DIF_REGISTERDEVICE: DbgOut( "DIF_REGISTERDEVICE" ); break ; case DIF_NEWDEVICEWIZARD_PRESELECT: DbgOut( "DIF_NEWDEVICEWIZARD_PRESELECT" ); break ; case DIF_NEWDEVICEWIZARD_SELECT: DbgOut( "DIF_NEWDEVICEWIZARD_SELECT" ); break ; case DIF_NEWDEVICEWIZARD_PREANALYZE: DbgOut( "DIF_NEWDEVICEWIZARD_PREANALYZE" ); break ; case DIF_NEWDEVICEWIZARD_POSTANALYZE: DbgOut( "DIF_NEWDEVICEWIZARD_POSTANALYZE" ); break ; case DIF_NEWDEVICEWIZARD_FINISHINSTALL: DbgOut( "DIF_NEWDEVICEWIZARD_FINISHINSTALL" ); break ; case DIF_INSTALLINTERFACES: DbgOut( "DIF_INSTALLINTERFACES" ); break ; case DIF_DETECTCANCEL: DbgOut( "DIF_DETECTCANCEL" ); break ; case DIF_REGISTER_COINSTALLERS: DbgOut( "DIF_REGISTER_COINSTALLERS" ); break ; case DIF_ADDPROPERTYPAGE_ADVANCED: DbgOut( "DIF_ADDPROPERTYPAGE_ADVANCED" ); break ; case DIF_ADDPROPERTYPAGE_BASIC: DbgOut( "DIF_ADDPROPERTYPAGE_BASIC" ); break ; case DIF_TROUBLESHOOTER: DbgOut( "DIF_TROUBLESHOOTER" ); break ; case DIF_POWERMESSAGEWAKE: DbgOut( "DIF_POWERMESSAGEWAKE" ); break ; default : DbgOut( "?????" ); break ; } return NO_ERROR; } |
Our Services
-
What our customers say about us?
Read our customer testimonials to find out why our clients keep returning for their projects.
View Testimonials