Sample Code

Windows Driver Samples/ Toaster Sample Driver/ C++/ classinstaller/ classInst.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//+---------------------------------------------------------------------------
//
//  Copyright (c) Microsoft Corporation.  All rights reserved.
//
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
//  PURPOSE.
//
//  File:       ClassINST.C
//
//  Contents:   class-installer for toaster.
//
//  Notes:      This is a sample class installer. Class installer is not required
//              for every class of device. I have included here to provide
//              an icon for the toaster class and to demonstrate how one
//              can provide a customer property sheet in the device manager
//              in response to DIF_ADDPROPERTYPAGE_ADVANCED.
//              The property sheet has a check box. If you enable the box
//              and press OK, it will restart the device. This is required
//              if the user changes the device properties, and it needs to
//              be restarted to apply the new attributes.
//              For a complete description of ClassInstallers, please see the
//              Microsoft Windows 2000 DDK Documentation.
//
// 
//  Revision: Added property page - Nov 14, 2000
// 
//
//----------------------------------------------------------------------------
 
//
// Annotation to indicate to prefast that this is nondriver user-mode code.
//
#include <DriverSpecs.h>
__user_code 
 
#include <windows.h>
#include <strsafe.h>
#include <setupapi.h>
#include <stdio.h>
#include "resource.h"
#include <dontuse.h>
//+---------------------------------------------------------------------------
//
// WARNING!
//
// Installer must not generate any popup to the user.
//    It should provide appropriate defaults.
//
//  OutputDebugString should be fine...
//
#if DBG
#define DbgOut(Text) OutputDebugString(TEXT("ClassInstaller: " Text "\n"))
#else
#define DbgOut(Text)
#endif
 
typedef struct _TOASTER_PROP_PARAMS
{
 
   HDEVINFO                     DeviceInfoSet;
   PSP_DEVINFO_DATA             DeviceInfoData;
   BOOL                         Restart;
    
} TOASTER_PROP_PARAMS, *PTOASTER_PROP_PARAMS;
 
 
INT_PTR
PropPageDlgProc(_In_ HWND   hDlg,
               _In_ UINT   uMessage,
               _In_ WPARAM wParam,
               _In_ LPARAM lParam);
 
UINT
CALLBACK
PropPageDlgCallback(HWND hwnd,
                    UINT uMsg,
                    LPPROPSHEETPAGE ppsp);
DWORD
PropPageProvider(
    _In_  HDEVINFO            DeviceInfoSet,
    _In_  PSP_DEVINFO_DATA    DeviceInfoData OPTIONAL
);
 
BOOL
OnNotify(
    HWND    ParentHwnd,
    LPNMHDR NmHdr,
    PTOASTER_PROP_PARAMS Params
    );
     
HMODULE ModuleInstance;
 
BOOL WINAPI
DllMain(
    HINSTANCE DllInstance,
    DWORD Reason,
    PVOID Reserved
    )
{
 
    UNREFERENCED_PARAMETER( Reserved );
 
    switch(Reason) {
 
        case DLL_PROCESS_ATTACH: {
 
            ModuleInstance = DllInstance;
            DisableThreadLibraryCalls(DllInstance);
            InitCommonControls();
            break;
        }
 
        case DLL_PROCESS_DETACH: {
            ModuleInstance = NULL;
            break;
        }
 
        default: {
            break;
        }
    }
 
    return TRUE;
}
 
DWORD CALLBACK
ToasterClassInstaller(
    _In_  DI_FUNCTION         InstallFunction,
    _In_  HDEVINFO            DeviceInfoSet,
    _In_  PSP_DEVINFO_DATA    DeviceInfoData OPTIONAL
    )
/*++
 
Routine Description:
 
    Responds to Class-installer messages
    
Arguments:
 
     InstallFunction   [in]
     DeviceInfoSet     [in]
     DeviceInfoData    [in]
 
Return Value:
 
Returns:    NO_ERROR, ERROR_DI_POSTPROCESSING_REQUIRED, or an error code.
 
--*/
{
    switch (InstallFunction)
    {
        case DIF_INSTALLDEVICE:
            //
            // Sent twice: once before installing the device and once
            // after installing device, if you have returned
            // ERROR_DI_POSTPROCESSING_REQUIRED during the first pass.
            //
            DbgOut("DIF_INSTALLDEVICE");
            break;
             
        case DIF_ADDPROPERTYPAGE_ADVANCED:
            //
            // Sent when you check the properties of the device in the
            // device manager.
            //
            DbgOut("DIF_ADDPROPERTYPAGE_ADVANCED");
            return PropPageProvider(DeviceInfoSet, DeviceInfoData);          
             
        case DIF_POWERMESSAGEWAKE:
            //
            // Sent when you check the power management tab
            //
            DbgOut("DIF_POWERMESSAGEWAKE");
            break;
 
        case DIF_PROPERTYCHANGE:
            //
            // Sent when you change the property of the device using
            // SetupDiSetDeviceInstallParams. (Enable/Disable/Restart)
            //
            DbgOut("DIF_PROPERTYCHANGE");
            break;
        case DIF_REMOVE:
             //
             // Sent when you uninstall the device.
             //
             DbgOut("DIF_REMOVE");
             break;
              
        case DIF_NEWDEVICEWIZARD_FINISHINSTALL:
            //
            // Sent near the end of installation to allow
            // an installer to supply wizard page(s) to the user.
            // These wizard pages are different from the device manager
            // property sheet.There are popped only once during install.
            //
            DbgOut("DIF_NEWDEVICEWIZARD_FINISHINSTALL");
            break;
             
        case DIF_SELECTDEVICE:
            DbgOut("DIF_SELECTDEVICE");
            break;
        case DIF_DESTROYPRIVATEDATA:
            //
            // Sent when Setup destroys a device information set
            // or an SP_DEVINFO_DATA element, or when Setup discards
            // its list of co-installers and class installer for a device
            //
            DbgOut("DIF_DESTROYPRIVATEDATA");
            break;
        case DIF_INSTALLDEVICEFILES:
            DbgOut("DIF_INSTALLDEVICEFILES");
            break;
        case DIF_ALLOW_INSTALL:
            //
            // Sent to confirm whether the installer wants to allow
            // the installation of device.
            //
            DbgOut("DIF_ALLOW_INSTALL");
            break;
        case DIF_SELECTBESTCOMPATDRV:
            DbgOut("DIF_SELECTBESTCOMPATDRV");
            break;
 
        case DIF_INSTALLINTERFACES:
            DbgOut("DIF_INSTALLINTERFACES");
            break;
        case DIF_REGISTER_COINSTALLERS:
            DbgOut("DIF_REGISTER_COINSTALLERS");
            break;
        default:
            DbgOut("DIF_???");
            break;
    }  
    return ERROR_DI_DO_DEFAULT;   
}
 
DWORD
PropPageProvider(
    _In_  HDEVINFO            DeviceInfoSet,
    _In_  PSP_DEVINFO_DATA    DeviceInfoData OPTIONAL
)
 
/*++
 
Routine Description:
 
    Entry-point for adding additional device manager property
    sheet pages. 
Arguments:
 
Return Value:
 
Returns:    NO_ERROR, ERROR_DI_DO_DEFAULT, or an error code.
 
--*/
{
    HPROPSHEETPAGE  pageHandle;
    PROPSHEETPAGE   page;
    PTOASTER_PROP_PARAMS      params = NULL;
    SP_ADDPROPERTYPAGE_DATA AddPropertyPageData = {0};
 
    //
    // DeviceInfoSet is NULL if setup is requesting property pages for
    // the device setup class. We don't want to do anything in this
    // case.
    //
    if (DeviceInfoData==NULL) {
        return ERROR_DI_DO_DEFAULT;
    }
 
    AddPropertyPageData.ClassInstallHeader.cbSize =
         sizeof(SP_CLASSINSTALL_HEADER);
 
    //
    // Get the current class install parameters for the device
    //
 
    if (SetupDiGetClassInstallParams(DeviceInfoSet, DeviceInfoData,
         (PSP_CLASSINSTALL_HEADER)&AddPropertyPageData,
         sizeof(SP_ADDPROPERTYPAGE_DATA), NULL ))
    {
 
        //
        // Ensure that the maximum number of dynamic pages for the
        // device has not yet been met
        //
        if(AddPropertyPageData.NumDynamicPages >= MAX_INSTALLWIZARD_DYNAPAGES){
            return NO_ERROR;
        }
        params = HeapAlloc(GetProcessHeap(), 0, sizeof(TOASTER_PROP_PARAMS));
        if (params)
        {
            //
            // Save DeviceInfoSet and DeviceInfoData
            //
            params->DeviceInfoSet     = DeviceInfoSet;
            params->DeviceInfoData    = DeviceInfoData;
            params->Restart           = FALSE;
             
            //
            // Create custom property sheet page
            //
            memset(&page, 0, sizeof(PROPSHEETPAGE));
 
            page.dwSize = sizeof(PROPSHEETPAGE);
            page.dwFlags = PSP_USECALLBACK;
            page.hInstance = ModuleInstance;
            page.pszTemplate = MAKEINTRESOURCE(DLG_TOASTER_PORTSETTINGS);
            page.pfnDlgProc = (DLGPROC) PropPageDlgProc;
            page.pfnCallback = PropPageDlgCallback;
 
            page.lParam = (LPARAM) params;
 
            pageHandle = CreatePropertySheetPage(&page);
            if(!pageHandle)
            {
                HeapFree(GetProcessHeap(), 0, params);
                return NO_ERROR;
            }
 
            //
            // Add the new page to the list of dynamic property
            // sheets
            //
            AddPropertyPageData.DynamicPages[
                AddPropertyPageData.NumDynamicPages++]=pageHandle;
 
            SetupDiSetClassInstallParams(DeviceInfoSet,
                        DeviceInfoData,
                        (PSP_CLASSINSTALL_HEADER)&AddPropertyPageData,
                        sizeof(SP_ADDPROPERTYPAGE_DATA));
        }
    }
    return NO_ERROR;
}
 
INT_PTR
PropPageDlgProc(_In_ HWND   hDlg,
                   _In_ UINT   uMessage,
                   _In_ WPARAM wParam,
                   _In_ LPARAM lParam)
/*++
 
Routine Description: PropPageDlgProc
 
    The windows control function for the custom property page window
 
Arguments:
 
    hDlg, uMessage, wParam, lParam: standard windows DlgProc parameters
 
Return Value:
 
    BOOL: FALSE if function fails, TRUE if function passes
 
--*/
{
    PTOASTER_PROP_PARAMS params;
 
    UNREFERENCED_PARAMETER( wParam );
 
    params = (PTOASTER_PROP_PARAMS) GetWindowLongPtr(hDlg, DWLP_USER);
 
    switch(uMessage) {
    case WM_COMMAND:
        break;
 
    case WM_CONTEXTMENU:
        break;
 
    case WM_HELP:
        break;
 
    case WM_INITDIALOG:
 
        //
        // on WM_INITDIALOG call, lParam points to the property
        // sheet page.
        //
        // The lParam field in the property sheet page struct is set by the
        // caller. This was set when we created the property sheet.
        // Save this in the user window long so that we can access it on later
        // on later messages.
        //
 
        params = (PTOASTER_PROP_PARAMS) ((LPPROPSHEETPAGE)lParam)->lParam;
        SetWindowLongPtr(hDlg, DWLP_USER, (LONG_PTR) params);
        break;
 
 
    case WM_NOTIFY:
        OnNotify(hDlg, (NMHDR *)lParam, params);
        break;
 
    default:
        return FALSE;
    }
 
    return TRUE;
}
 
 
UINT
CALLBACK
PropPageDlgCallback(HWND hwnd,
                   UINT uMsg,
                   LPPROPSHEETPAGE ppsp)
{
    PTOASTER_PROP_PARAMS params;
 
    UNREFERENCED_PARAMETER( hwnd );
 
    switch (uMsg) {
 
    case PSPCB_CREATE:
        //
        // Called when the property sheet is first displayed
        //
        return TRUE;    // return TRUE to continue with creation of page
 
    case PSPCB_RELEASE:
        //
        // Called when property page is destroyed, even if the page
        // was never displayed. This is the correct way to release data.
        //
        params = (PTOASTER_PROP_PARAMS) ppsp->lParam;
        LocalFree(params);
        return 0;       // return value ignored
    default:
        break;
    }
 
    return TRUE;
}
 
 
BOOL
OnNotify(
    HWND    ParentHwnd,
    LPNMHDR NmHdr,
    PTOASTER_PROP_PARAMS Params
    )
{
    SP_DEVINSTALL_PARAMS spDevInstall = {0};
    TCHAR                friendlyName[LINE_LEN] ={0};
    size_t  charCount;
    BOOL    fSuccess;
 
    switch (NmHdr->code) {
    case PSN_APPLY:
        //
        // Sent when the user clicks on Apply OR OK !!
        //
 
        GetDlgItemText(ParentHwnd, IDC_FRIENDLYNAME, friendlyName,
                                        LINE_LEN-1 );
        friendlyName[LINE_LEN-1] = UNICODE_NULL;
        if(friendlyName[0]) {
 
            if (FAILED(StringCchLength(friendlyName,
                         STRSAFE_MAX_CCH,
                         &charCount))) {
                DbgOut("StringCchLength failed!");                  
                break;
            }
            fSuccess = SetupDiSetDeviceRegistryProperty(Params->DeviceInfoSet,
                         Params->DeviceInfoData,
                         SPDRP_FRIENDLYNAME,
                         (BYTE *)friendlyName,
                         (DWORD)((charCount + 1) * sizeof(TCHAR))
                         );
            if(!fSuccess) {
                DbgOut("SetupDiSetDeviceRegistryProperty failed!");                  
                break;
            }
 
            //
            // Inform setup about property change so that it can
            // restart the device.
            //
 
            spDevInstall.cbSize = sizeof(SP_DEVINSTALL_PARAMS);
      
            if (Params && SetupDiGetDeviceInstallParams(Params->DeviceInfoSet,
                                              Params->DeviceInfoData,
                                              &spDevInstall)) {
                //
                // If your device requires a reboot to restart, you can
                // specify that by setting DI_NEEDREBOOT as shown below
                //
                // if(NeedReboot) {
                //    spDevInstall.Flags |= DI_PROPERTIES_CHANGE | DI_NEEDREBOOT;
                // }
                //
                spDevInstall.FlagsEx |= DI_FLAGSEX_PROPCHANGE_PENDING;
                 
                SetupDiSetDeviceInstallParams(Params->DeviceInfoSet,
                                              Params->DeviceInfoData,
                                              &spDevInstall);
            }
         
        }
        return TRUE;
 
    default:
        return FALSE;
    }
    return FALSE;  
}

Our Services

  • What our customers say about us?

© 2011-2025 All Rights Reserved. Joya Systems. 4425 South Mopac Building II Suite 101 Austin, TX 78735 Tel: 800-DEV-KERNEL

Privacy Policy. Terms of use. Valid XHTML & CSS