Changeset 70798 in vbox for trunk/src/VBox/HostDrivers/VBoxNetFlt
- Timestamp:
- Jan 29, 2018 7:38:48 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/HostDrivers/VBoxNetFlt/win/nobj/VBoxNetFltNobj.cpp
r69500 r70798 31 31 #include <VBoxNetFltNobjT_i.c> 32 32 33 #include <Olectl.h> 34 33 35 //# define VBOXNETFLTNOTIFY_DEBUG_BIND 34 36 … … 584 586 } 585 587 588 /* 589 * ATL::CComModule does not suport server registration/unregistration methods, 590 * so we need to do it manually. Since this is the only place we do registraton 591 * manually, we do it the quick-and-dirty way. 592 */ 593 594 /* Someday we may want to log errors. */ 595 class AdHocRegError 596 { 597 public: 598 AdHocRegError(LSTATUS rc) { RT_NOREF1(rc); }; 599 }; 600 601 /* A simple wrapper on Windows registry functions. */ 602 class AdHocRegKey 603 { 604 public: 605 AdHocRegKey(HKEY hKey) : m_hKey(hKey) {}; 606 AdHocRegKey(LPCWSTR pcwszName, HKEY hParent = HKEY_CLASSES_ROOT); 607 ~AdHocRegKey() { RegCloseKey(m_hKey); }; 608 609 AdHocRegKey *create(LPCWSTR pcwszSubkey, LPCWSTR pcwszDefaultValue = NULL); 610 void remove(LPCWSTR pcwszSubkey); 611 void setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue); 612 HKEY getKey(void) { return m_hKey; }; 613 private: 614 HKEY m_hKey; 615 }; 616 617 AdHocRegKey::AdHocRegKey(LPCWSTR pcwszName, HKEY hParent) 618 { 619 LSTATUS rc = RegOpenKeyExW(hParent, pcwszName, 0, KEY_ALL_ACCESS, &m_hKey); 620 if (rc != ERROR_SUCCESS) 621 throw AdHocRegError(rc); 622 } 623 624 void AdHocRegKey::remove(LPCWSTR pcwszSubkey) 625 { 626 LSTATUS rc; 627 WCHAR wszName[256]; 628 DWORD dwName; 629 630 /* Remove all subkeys of subkey first */ 631 AdHocRegKey *subkey = new AdHocRegKey(pcwszSubkey, m_hKey); 632 for (;;) 633 { 634 /* Always ask for the first subkey, because we remove it before calling RegEnumKeyEx again */ 635 dwName = 255; 636 rc = RegEnumKeyExW(subkey->getKey(), 0, wszName, &dwName, NULL, NULL, NULL, NULL); 637 if (rc != ERROR_SUCCESS) 638 break; 639 subkey->remove(wszName); 640 } 641 delete subkey; 642 643 /* Remove the subkey itself */ 644 rc = RegDeleteKeyW(m_hKey, pcwszSubkey); 645 if (rc != ERROR_SUCCESS) 646 throw AdHocRegError(rc); 647 } 648 649 AdHocRegKey *AdHocRegKey::create(LPCWSTR pcwszSubkey, LPCWSTR pcwszDefaultValue) 650 { 651 HKEY hSubkey; 652 LSTATUS rc = RegCreateKeyExW(m_hKey, pcwszSubkey, 653 0 /*Reserved*/, NULL /*pszClass*/, 0 /*fOptions*/, 654 KEY_ALL_ACCESS, NULL /*pSecAttr*/, &hSubkey, NULL /*pdwDisposition*/); 655 if (rc != ERROR_SUCCESS) 656 throw AdHocRegError(rc); 657 AdHocRegKey *pSubkey = new AdHocRegKey(hSubkey); 658 if (pcwszDefaultValue) 659 pSubkey->setValue(NULL, pcwszDefaultValue); 660 return pSubkey; 661 } 662 663 void AdHocRegKey::setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue) 664 { 665 LSTATUS rc = RegSetValueExW(m_hKey, pcwszName, 0, REG_SZ, (const BYTE *)pcwszValue, 666 (wcslen(pcwszValue) + 1) * sizeof(WCHAR)); 667 if (rc != ERROR_SUCCESS) 668 throw AdHocRegError(rc); 669 } 670 671 /* 672 * Auxiliary class that facilitates automatic destruction of AdHocRegKey objects 673 * allocated in heap. No reference counting here! 674 */ 675 class AdHocRegKeyPtr 676 { 677 public: 678 AdHocRegKeyPtr(AdHocRegKey *pKey) : m_pKey(pKey) {}; 679 ~AdHocRegKeyPtr() { delete m_pKey; }; 680 681 AdHocRegKey *create(LPCWSTR pcwszSubkey, LPCWSTR pcwszDefaultValue = NULL) 682 { return m_pKey->create(pcwszSubkey, pcwszDefaultValue); }; 683 void remove(LPCWSTR pcwszSubkey) 684 { return m_pKey->remove(pcwszSubkey); }; 685 void setValue(LPCWSTR pcwszName, LPCWSTR pcwszValue) 686 { return m_pKey->setValue(pcwszName, pcwszValue); }; 687 private: 688 AdHocRegKey *m_pKey; 689 /* Prevent copying, since we do not support reference counting */ 690 AdHocRegKeyPtr(const AdHocRegKeyPtr&); 691 AdHocRegKeyPtr& operator=(const AdHocRegKeyPtr&); 692 }; 693 694 586 695 STDAPI DllRegisterServer() 587 696 { 588 // this is a "just in case" conditional, which is not defined 589 #ifdef VBOX_FORCE_REGISTER_SERVER 590 return _Module.RegisterServer(TRUE); 591 #else 697 WCHAR wszModule[MAX_PATH + 1]; 698 if (GetModuleFileNameW(GetModuleHandleW(L"VBoxNetFltNobj"), wszModule, MAX_PATH) == 0) 699 return SELFREG_E_CLASS; 700 701 try { 702 AdHocRegKey keyCLSID(L"CLSID"); 703 AdHocRegKeyPtr pkeyNobjClass(keyCLSID.create(L"{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}", 704 L"VirtualBox Bridged Networking Driver Notify Object v1.1")); 705 AdHocRegKeyPtr pkeyNobjSrv(pkeyNobjClass.create(L"InProcServer32", wszModule)); 706 pkeyNobjSrv.setValue(L"ThreadingModel", L"Both"); 707 } 708 catch (AdHocRegError) 709 { 710 return SELFREG_E_CLASS; 711 } 712 713 try { 714 AdHocRegKey keyTypeLib(L"TypeLib"); 715 AdHocRegKeyPtr pkeyNobjLib(keyTypeLib.create(L"{2A0C94D1-40E1-439C-8FE8-24107CAB0840}\\1.1", 716 L"VirtualBox Bridged Networking Driver Notify Object v1.1 Type Library")); 717 AdHocRegKeyPtr pkeyNobjLib0(pkeyNobjLib.create(L"0\\win64", wszModule)); 718 AdHocRegKeyPtr pkeyNobjLibFlags(pkeyNobjLib.create(L"FLAGS", L"0")); 719 if (GetSystemDirectoryW(wszModule, MAX_PATH) == 0) 720 return SELFREG_E_TYPELIB; 721 AdHocRegKeyPtr pkeyNobjLibHelpDir(pkeyNobjLib.create(L"HELPDIR", wszModule)); 722 } 723 catch (AdHocRegError) 724 { 725 return SELFREG_E_CLASS; 726 } 727 592 728 return S_OK; 593 #endif594 729 } 595 730 596 731 STDAPI DllUnregisterServer() 597 732 { 598 // this is a "just in case" conditional, which is not defined 599 #ifdef VBOX_FORCE_REGISTER_SERVER 600 return _Module.UnregisterServer(TRUE); 601 #else 733 try { 734 AdHocRegKey keyTypeLib(L"TypeLib"); 735 keyTypeLib.remove(L"{2A0C94D1-40E1-439C-8FE8-24107CAB0840}"); 736 } 737 catch (AdHocRegError) { return SELFREG_E_TYPELIB; } 738 739 try { 740 AdHocRegKey keyCLSID(L"CLSID"); 741 keyCLSID.remove(L"{f374d1a0-bf08-4bdc-9cb2-c15ddaeef955}"); 742 } 743 catch (AdHocRegError) { return SELFREG_E_CLASS; } 744 602 745 return S_OK; 603 #endif 604 } 746 }
Note:
See TracChangeset
for help on using the changeset viewer.