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
|
/*++
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.
Module Name:
toastva.c
Abstract:
The TOASTVA application illustrates installation techniques that can be
used to seamlessly integrate PnP device installation with installation of
value-added software, regardless of whether the software installation
preceeded the hardware installation, or vice versa.
--*/
#include "precomp.h"
#pragma hdrstop
//
// Constants
//
#if defined(_IA64_)
#define TOASTVA_PLATFORM_SUBDIRECTORY L"ia64"
#elif defined(_X86_)
#define TOASTVA_PLATFORM_SUBDIRECTORY L"i386"
#elif defined(_AMD64_)
#define TOASTVA_PLATFORM_SUBDIRECTORY L"amd64"
#elif defined(_ARM_)
#define TOASTVA_PLATFORM_SUBDIRECTORY L"arm"
#elif defined(_ARM64_)
#define TOASTVA_PLATFORM_SUBDIRECTORY L"arm64"
#else
#error Unsupported platform
#endif
#define TOASTVA_PLATFORM_SUBDIRECTORY_SIZE (sizeof(TOASTVA_PLATFORM_SUBDIRECTORY) / sizeof(WCHAR))
//
// Globals
//
HINSTANCE g_hInstance;
//
// Implementation
//
INT
WINAPI
WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd
)
{
LPWSTR *ArgList;
INT NumArgs;
WCHAR MediaRootDirectory[MAX_PATH];
LPWSTR FileNamePart;
DWORD DirPathLength;
g_hInstance = hInstance;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nShowCmd);
//
// Windows 2000 doesn't suppress auto-run applications when media (e.g.,
// a CD) is inserted while a "Found New Hardware" popup is onscreen. This
// means that, by default, inserting a CD in order to supply PnP with the
// necessary INF and driver files will result in the autorun app launching,
// and obscuring the wizard, causing user confusion, etc.
//
// To avoid this, we retrieve an entrypoint to a Windows 2000 (and later)
// Configuration Manager (CM) API that allows us to detect when a device
// installation is in-progress, and suppress our own application from
// starting.
//
if(IsDeviceInstallInProgress()) {
//
// We don't want to startup right now. Don't worry--the value-added
// software part of the device installation will be invoked (if
// necessary) by our device co-installer during finish-install
// processing.
//
return 0;
}
//
// Retrieve the full directory path from which our setup program was
// invoked.
//
ArgList = CommandLineToArgvW(GetCommandLine(), &NumArgs);
if(ArgList && (NumArgs >= 1)) {
DirPathLength = GetFullPathName(ArgList[0],
MAX_PATH,
MediaRootDirectory,
&FileNamePart
);
if(DirPathLength >= MAX_PATH) {
//
// The directory is too large for our buffer. Set our directory
// path length to zero so we'll simply bail out in this rare case.
//
DirPathLength = 0;
}
if(DirPathLength) {
//
// Strip the filename off the path.
//
*FileNamePart = L'\0';
DirPathLength = (DWORD)(FileNamePart - MediaRootDirectory);
}
} else {
//
// For some reason, we couldn't get the command line arguments that
// were used when invoking our setup app. Assume current directory
// instead.
//
DirPathLength = GetCurrentDirectory(MAX_PATH, MediaRootDirectory);
if(DirPathLength >= MAX_PATH) {
//
// The current directory is too large for our buffer. Set our
// directory path length to zero so we'll simply bail out in this
// rare case.
//
DirPathLength = 0;
}
if(DirPathLength) {
//
// Ensure that path ends in a path separator character.
//
if((MediaRootDirectory[DirPathLength-1] != L'\\') &&
(MediaRootDirectory[DirPathLength-1] != L'/'))
{
MediaRootDirectory[DirPathLength++] = L'\\';
if(DirPathLength < MAX_PATH) {
MediaRootDirectory[DirPathLength] = L'\0';
} else {
//
// Not enough room in buffer to add path separator char
//
DirPathLength = 0;
}
}
}
}
if(ArgList) {
GlobalFree(ArgList);
}
if(!DirPathLength) {
//
// Couldn't figure out what the root directory of our installation
// media was. Bail out.
//
return 0;
}
//
// If we're being invoked from a platform-specific subdirectory (i.e.,
// \i386 or \ia64), then strip off that subdirectory to get the true media
// root path.
//
if(DirPathLength > TOASTVA_PLATFORM_SUBDIRECTORY_SIZE) {
//
// We know that the last character in our MediaRootDirectory string is
// a path separator character. Check to see if the preceding
// characters match our platform-specific subdirectory.
//
if(!_wcsnicmp(&(MediaRootDirectory[DirPathLength - TOASTVA_PLATFORM_SUBDIRECTORY_SIZE]),
TOASTVA_PLATFORM_SUBDIRECTORY,
TOASTVA_PLATFORM_SUBDIRECTORY_SIZE - 1)) {
//
// Platform-specific part matches, just make sure preceding char
// is a path separator char.
//
if((MediaRootDirectory[DirPathLength - TOASTVA_PLATFORM_SUBDIRECTORY_SIZE - 1] == L'\\') ||
(MediaRootDirectory[DirPathLength - TOASTVA_PLATFORM_SUBDIRECTORY_SIZE - 1] == L'/')) {
MediaRootDirectory[DirPathLength - TOASTVA_PLATFORM_SUBDIRECTORY_SIZE] = L'\0';
}
}
}
DoValueAddWizard(MediaRootDirectory);
return 0;
}
|