new-loopback-dll-20040622
[openafs.git] / src / WINNT / install / loopback / renameconnection.cpp
1 /*
2
3 Copyright 2004 by the Massachusetts Institute of Technology
4
5 All rights reserved.
6
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the name of the Massachusetts
12 Institute of Technology (M.I.T.) not be used in advertising or publicity
13 pertaining to distribution of the software without specific, written
14 prior permission.
15
16 M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
18 M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
19 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 SOFTWARE.
23
24 */
25
26 #include <afx.h>
27 #include <windows.h>
28 #include <shellapi.h>
29 #include <objbase.h>
30 #include <shlobj.h>
31 #include <wtypes.h>
32
33 #define NETSHELL_LIBRARY _T("netshell.dll")
34
35 // Use the IShellFolder API to rename the connection.
36 static HRESULT rename_shellfolder(PCWSTR wGuid, PCWSTR wNewName)
37 {
38     // This is the GUID for the network connections folder. It is constant.
39     // {7007ACC7-3202-11D1-AAD2-00805FC1270E}
40     const GUID CLSID_NetworkConnections = {
41         0x7007ACC7, 0x3202, 0x11D1, {
42             0xAA, 0xD2, 0x00, 0x80, 0x5F, 0xC1, 0x27, 0x0E
43         }
44     };
45     LPITEMIDLIST pidl;
46     IShellFolder *pShellFolder;
47     IMalloc *pShellMalloc;
48
49     // Build the display name in the form "::{GUID}".
50     if (wcslen(wGuid) >= MAX_PATH)
51         return E_INVALIDARG;
52     WCHAR szAdapterGuid[MAX_PATH + 2];
53     swprintf(szAdapterGuid, L"::%ls", wGuid);
54
55     // Initialize COM.
56     CoInitialize(NULL);
57
58     // Get the shell allocator.
59     HRESULT hr = SHGetMalloc(&pShellMalloc);
60     if (SUCCEEDED(hr))
61     {
62         // Create an instance of the network connections folder.
63         hr = CoCreateInstance(CLSID_NetworkConnections, NULL,
64                               CLSCTX_INPROC_SERVER, IID_IShellFolder,
65                               reinterpret_cast<LPVOID *>(&pShellFolder));
66     }
67     // Parse the display name.
68     if (SUCCEEDED(hr))
69     {
70         hr = pShellFolder->ParseDisplayName(NULL, NULL, szAdapterGuid, NULL,
71                                             &pidl, NULL);
72     }
73     if (SUCCEEDED(hr))
74     {
75         hr = pShellFolder->SetNameOf(NULL, pidl, wNewName, SHGDN_NORMAL,
76                                      &pidl);
77         pShellMalloc->Free(pidl);
78     }
79     CoUninitialize();
80     return hr;
81 }
82
83 extern "C" int RenameConnection(PCWSTR GuidString, PCWSTR NewName)
84 {
85     typedef HRESULT (WINAPI *lpHrRenameConnection)(const GUID *, PCWSTR);
86     lpHrRenameConnection RenameConnectionFunc = NULL;
87     HRESULT status;
88
89     // First try the IShellFolder interface, which was unimplemented
90     // for the network connections folder before XP.
91     status = rename_shellfolder(GuidString, NewName);
92     if (status == E_NOTIMPL)
93     {
94         // The IShellFolder interface is not implemented on this platform.
95         // Try the (undocumented) HrRenameConnection API in the netshell
96         // library.
97         CLSID clsid;
98         HINSTANCE hNetShell;
99         status = CLSIDFromString((LPOLESTR) GuidString, &clsid);
100         if (FAILED(status))
101             return -1;
102         hNetShell = LoadLibrary(NETSHELL_LIBRARY);
103         if (hNetShell == NULL)
104             return -1;
105         RenameConnectionFunc =
106           (lpHrRenameConnection) GetProcAddress(hNetShell,
107                                                 "HrRenameConnection");
108         if (RenameConnectionFunc == NULL)
109         {
110             FreeLibrary(hNetShell);
111             return -1;
112         }
113         status = RenameConnectionFunc(&clsid, NewName);
114         FreeLibrary(hNetShell);
115     }
116     if (FAILED(status))
117         return -1;
118     return 0;
119 }