home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / rename.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  1.9 KB  |  68 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\install
  4.  * File Name: rename.cpp
  5.  * Purpose  : Contains code to rename a network connection
  6.  */
  7.  
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <tchar.h>
  11. #include <shellapi.h>
  12. #include <objbase.h>
  13. #include <shlobj.h>
  14. #include <wtypes.h>
  15.  
  16. //
  17. // This is the GUID for the network connections folder. It is constant.
  18. // {7007ACC7-3202-11D1-AAD2-00805FC1270E}
  19. //
  20. const GUID CLSID_NetworkConnections = {
  21.     0x7007ACC7, 0x3202, 0x11D1,
  22.     { 0xAA, 0xD2, 0x00, 0x80, 0x5F, 0xC1, 0x27, 0x0E }
  23. };
  24.  
  25. //
  26. // NB: This function leaks memory/references.
  27. //
  28. EXTERN_C void
  29. RenameAdapter(const GUID *AdapterGuid, const WCHAR *NewName)
  30. {
  31.     HRESULT hr;
  32.  
  33.     IShellFolder *pShellFolder;
  34.     hr = CoCreateInstance(CLSID_NetworkConnections, NULL,
  35.                           CLSCTX_INPROC_SERVER,
  36.                           IID_IShellFolder,
  37.                           reinterpret_cast<LPVOID *>(&pShellFolder));
  38.     if (FAILED(hr)) {
  39.         fprintf(stderr, "VirtualWiFi: RenameAdapter: CoCreateInstance: %x\n", hr);
  40.         return;
  41.     }
  42.  
  43.     LPOLESTR szClsId;
  44.     hr = StringFromCLSID(*AdapterGuid, &szClsId);
  45.     if (FAILED(hr)) {
  46.         fprintf(stderr, "VirtualWiFi: RenameAdapter: StringFromCLSID: %x\n", hr);
  47.         return;
  48.     }
  49.  
  50.     WCHAR szAdapterGuid[MAX_PATH];
  51.     swprintf(szAdapterGuid, L"::%s", szClsId);
  52.  
  53.     LPITEMIDLIST pidl;
  54.     hr = pShellFolder->ParseDisplayName(NULL, NULL,
  55.                                         szAdapterGuid, NULL,
  56.                                         &pidl, NULL);
  57.     if (FAILED(hr)) {
  58.         fprintf(stderr, "VirtualWiFi: RenameAdapter: ParseDisplayName: %x\n", hr);
  59.         return;
  60.     }
  61.  
  62.     hr = pShellFolder->SetNameOf(NULL, pidl, NewName, SHGDN_NORMAL, &pidl);
  63.     if (FAILED(hr)) {
  64.         fprintf(stderr, "VirtualWiFi: RenameAdapter: SetNameOf: %x\n", hr);
  65.         return;
  66.     }
  67. }
  68.