Sample Code
Windows Driver Samples/ Toaster Sample Driver/ C++/ exe/ wmi/ wmitoast.cpp/
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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 | #include "wmitoast.h" // // Private methods. // bool ParseCommandLine( _In_ ULONG Argc, _In_reads_(Argc) PWSTR Argv[], _Outptr_result_maybenull_ PWSTR * ComputerName, _Outptr_result_maybenull_ PWSTR * UserId, _Outptr_result_maybenull_ PWSTR * Password, _Outptr_result_maybenull_ PWSTR * DomainName ); void DisplayUsage(); ULONG _cdecl wmain( _In_ ULONG Argc, _In_reads_(Argc) PWSTR Argv[] ) { HRESULT status = S_OK; BOOLEAN initialized = FALSE; BSTR temp = NULL; BSTR wmiRoot = NULL; BSTR userIdString = NULL; BSTR passwordString = NULL; PWSTR ComputerName = NULL; PWSTR userId = NULL; PWSTR password = NULL; PWSTR domain = NULL; IWbemLocator* wbemLocator = NULL; IWbemServices* wbemServices = NULL; // // Parse the input command line parameters. // if (!ParseCommandLine(Argc, Argv, &ComputerName, &userId, &password, &domain)) { // // Display the usage information if there was an error parsing the input // parameters or if usage information was requested. // status = E_INVALIDARG; DisplayUsage(); goto exit ; } // // Initialize COM environment for multi-threaded concurrency. // status = CoInitializeEx(NULL, COINIT_MULTITHREADED); if (FAILED(status)) { goto exit ; } initialized = TRUE; // // Initialize the security layer and set the specified values as the // security default for the process. // status = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_PKT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0); if (FAILED(status)) { goto exit ; } // // Create a single uninitialized object associated with the class id // CLSID_WbemLocator. // status = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, ( PVOID *)&wbemLocator); if (FAILED(status)) { goto exit ; } // // Construct the object path for the WMI namespace. For local access to the // WMI namespace, use a simple object path: "\\.\root\WMI". For access to // the WMI namespace on a remote computer, include the computer name in the // object path: "\\myserver\root\WMI". // if (ComputerName != NULL) { status = VarBstrCat(_bstr_t(L "\\\\" ), _bstr_t(ComputerName), &temp); if (FAILED(status)) { goto exit ; } } else { status = VarBstrCat(_bstr_t(L "\\\\" ), _bstr_t(L "." ), &temp); if (FAILED(status)) { goto exit ; } } status = VarBstrCat(temp, _bstr_t(L "\\root\\WMI" ), &wmiRoot); if (FAILED(status)) { goto exit ; } SysFreeString(temp); temp = NULL; // // Construct the user id and password strings. // if (userId != NULL) { if (domain != NULL) { status = VarBstrCat(_bstr_t(domain), _bstr_t(L "\\" ), &temp); if (FAILED(status)) { goto exit ; } status = VarBstrCat(temp, _bstr_t(userId), &userIdString); if (FAILED(status)) { goto exit ; } SysFreeString(temp); temp = NULL; } else { userIdString = SysAllocString(userId); if (userIdString == NULL) { status = E_OUTOFMEMORY; goto exit ; } } passwordString = SysAllocString(password); if (passwordString == NULL) { status = E_OUTOFMEMORY; goto exit ; } } // // Connect to the WMI server on this computer and, possibly, through it to another system. // status = wbemLocator->ConnectServer(wmiRoot, userIdString, passwordString, NULL, 0, NULL, NULL, &wbemServices); if (FAILED(status)) { if (status != WBEM_E_LOCAL_CREDENTIALS) { goto exit ; } // // Use the identity inherited from the current process. // status = wbemLocator->ConnectServer(wmiRoot, NULL, NULL, NULL, 0, NULL, NULL, &wbemServices); if (FAILED(status)) { goto exit ; } } // // Set authentication information for the interface. // status = SetInterfaceSecurity(wbemServices, userId, password, domain); if (FAILED(status)) { goto exit ; } // // Execute the methods in each instance of the desired class. // printf ( "\n1. Execute Methods in class ...\n" ); status = ExecuteMethodsInClass(wbemServices, userId, password, domain); if (FAILED(status)) { goto exit ; } // // Get and Set the property values in each instance of the desired class. // printf ( "\n2. Get/Set Property Values in class ...\n" ); status = GetAndSetValuesInClass(wbemServices, userId, password, domain); if (FAILED(status)) { goto exit ; } exit : if (temp != NULL) { SysFreeString(temp); } if (userIdString != NULL) { SysFreeString(userIdString); } if (passwordString != NULL) { SysFreeString(passwordString); } if (wmiRoot != NULL) { SysFreeString(wmiRoot); } if (wbemServices != NULL) { wbemServices->Release(); } if (wbemLocator != NULL) { wbemLocator->Release(); } if (initialized == TRUE) { CoUninitialize(); } if (FAILED(status)) { printf ( "FAILED with Status = 0x%08x\n" , status); } return status; } HRESULT SetInterfaceSecurity( _In_ IUnknown* InterfaceObj, _In_opt_ PWSTR UserId, _In_opt_ PWSTR Password, _In_opt_ PWSTR DomainName ) /*++ Routine Description: Set the interface security to allow the server to impersonate the specified user. Arguments: InterfaceObj - Pointer to interface for which the security settings need to be applied. UserId - Pointer to the user id information or NULL. Password - Pointer to password or NULL. If the user id is not specified, this parameter is ignored. DomainName - Pointer to domain name or NULL. If the user id is not specified, this parameter is ignored. Return Value: HRESULT Status code. --*/ { HRESULT hr; COAUTHIDENTITY AuthIdentity; DWORD AuthnSvc; DWORD AuthzSvc; DWORD AuthnLevel; DWORD ImpLevel; DWORD Capabilities; PWSTR pServerPrinName = NULL; RPC_AUTH_IDENTITY_HANDLE pAuthHndl = NULL; // // Get current authentication information for interface. // hr = CoQueryProxyBlanket(InterfaceObj, &AuthnSvc, &AuthzSvc, &pServerPrinName, &AuthnLevel, &ImpLevel, &pAuthHndl, &Capabilities); if (FAILED(hr)) { goto exit ; } if (UserId == NULL) { AuthIdentity.User = NULL; AuthIdentity.UserLength = 0; AuthIdentity.Password = NULL; AuthIdentity.PasswordLength = 0; AuthIdentity.Domain = NULL; AuthIdentity.DomainLength = 0; } else { AuthIdentity.User = ( USHORT *) UserId; #pragma prefast(suppress:6387, "0 length UserId is valid") AuthIdentity.UserLength = ( ULONG )wcslen(UserId); AuthIdentity.Password = ( USHORT *) Password; #pragma prefast(suppress:6387, "0 length Password is valid") AuthIdentity.PasswordLength = ( ULONG )wcslen(Password); AuthIdentity.Domain = ( USHORT *) DomainName; AuthIdentity.DomainLength = (DomainName == NULL) ? 0 : ( ULONG )wcslen(DomainName); } AuthIdentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; // // Change authentication information for interface, providing the identity // information and "echoing back" everything else. // hr = CoSetProxyBlanket(InterfaceObj, AuthnSvc, AuthzSvc, pServerPrinName, AuthnLevel, ImpLevel, &AuthIdentity, Capabilities); if (FAILED(hr)) { goto exit ; } exit : return hr; } bool ParseCommandLine( _In_ ULONG Argc, _In_reads_(Argc) PWSTR Argv[], _Outptr_result_maybenull_ PWSTR * ComputerName, _Outptr_result_maybenull_ PWSTR * UserId, _Outptr_result_maybenull_ PWSTR * Password, _Outptr_result_maybenull_ PWSTR * DomainName ) /*++ Routine Description: This routine parses the command line input and extracts the computer name, user id, password and domain name information. Arguments: Argc - Number of elements in the Agrv variable. Argv - Array of strings received as input to the wmain function. ComputerName - Pointer to the location that receives the address of the computer name. UserId - Pointer to the location that receives the address of the UserId. Password - Pointer to the location that receives the adddress of the Password string for the given user id. DomainName - Pointer to location that recieves the address of DomainName. of the user. Return Value: TRUE, if the command line could be parsed successfully. FALSE, otherwise. --*/ { ULONG i; *ComputerName = NULL; *UserId = NULL; *Password = NULL; *DomainName = NULL; // // Parameters are (when supplied): // // 0 -- Program name. Always supplied (by OS). // 1 -- System name. Optional. // 2 -- Userid. Optional but reconized only if system name supplied. // 3 -- Password. Optional but recognized only if system name and UserId supplied. // 4 -- DomainName. Optional but recognized only if system name, UserId and Password supplied. // // // Validate the number of input parameters. // if (!(Argc == 1 || Argc==2 || Argc==4 || Argc==5)) { printf ( "\nInvalid input parameters\n" ); return FALSE; } // // Parse the input parameters. // for (i = 1; i < Argc; i++) { switch (i) { case 1: // // System name. // *ComputerName = Argv[i]; break ; case 2: // // User ID. // *UserId = Argv[i]; break ; case 3: // // Password. // *Password = Argv[i]; break ; case 4: // // Domain Name. // *DomainName = Argv[i]; break ; default : printf ( "\nInvalid input parameters\n" ); return FALSE; } } // // Verify that if User Id was specified, then the Password is sepcified too. // if (((*UserId != NULL) && (*Password == NULL)) || ((*UserId == NULL) && (*Password != NULL))) { printf ( "\nIllegal UserId/Password combination\n\n" ); return FALSE; } // // Check whether the usage help was requested. // if (Argc == 2) { PWSTR HelpCmds[] = {L "help" , L "?" }; ULONG ArgLength = ( ULONG )wcslen(Argv[1]); for (i = 0; i < ARRAY_SIZE(HelpCmds); i ++) { if (_wcsnicmp(Argv[1], HelpCmds[i], ArgLength) == 0) { *ComputerName = NULL; return FALSE; } } } return TRUE; } void DisplayUsage() { printf ( "\nParameter information --\n\n\n" ); printf ( " <no parameter>\n\n" ); printf ( " Run on local system with current identity.\n\n" ); printf ( " <system name or IP address>\n\n" ); printf ( " Run on specified system with current identity.\n\n" ); printf ( " <system name or IP address> <UserId Password <DomainName>>\n\n" ); printf ( " Run on specified system with specified UserId and Password and, possibly, DomainName.\n\n" ); printf ( " help\n\n" ); printf ( " ?\n\n" ); } |
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