rx: Remove RX_CALL_BUSY
[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 #include "loopbackutils.h"
34
35 #define NETSHELL_LIBRARY _T("netshell.dll")
36
37 // Use the IShellFolder API to rename the connection.
38 static HRESULT rename_shellfolder(PCWSTR wGuid, PCWSTR wNewName)
39 {
40     // This is the GUID for the network connections folder. It is constant.
41     // {7007ACC7-3202-11D1-AAD2-00805FC1270E}
42     const GUID CLSID_NetworkConnections = {
43         0x7007ACC7, 0x3202, 0x11D1, {
44             0xAA, 0xD2, 0x00, 0x80, 0x5F, 0xC1, 0x27, 0x0E
45         }
46     };
47     LPITEMIDLIST pidl;
48     IShellFolder *pShellFolder;
49     IMalloc *pShellMalloc;
50
51     // Build the display name in the form "::{GUID}".
52     if (wcslen(wGuid) >= MAX_PATH)
53         return E_INVALIDARG;
54     WCHAR szAdapterGuid[MAX_PATH + 2];
55     swprintf(szAdapterGuid, L"::%ls", wGuid);
56
57     // Initialize COM.
58     CoInitialize(NULL);
59
60     // Get the shell allocator.
61     HRESULT hr = SHGetMalloc(&pShellMalloc);
62     if (SUCCEEDED(hr))
63     {
64         // Create an instance of the network connections folder.
65         hr = CoCreateInstance(CLSID_NetworkConnections, NULL,
66                               CLSCTX_INPROC_SERVER, IID_IShellFolder,
67                               reinterpret_cast<LPVOID *>(&pShellFolder));
68     }
69     // Parse the display name.
70     if (SUCCEEDED(hr))
71     {
72         hr = pShellFolder->ParseDisplayName(NULL, NULL, szAdapterGuid, NULL,
73                                             &pidl, NULL);
74     }
75     if (SUCCEEDED(hr))
76     {
77         hr = pShellFolder->SetNameOf(NULL, pidl, wNewName, SHGDN_NORMAL,
78                                      &pidl);
79         pShellMalloc->Free(pidl);
80     }
81     CoUninitialize();
82     return hr;
83 }
84
85 extern "C" int RenameConnection(PCWSTR GuidString, PCWSTR NewName)
86 {
87     typedef HRESULT (WINAPI *lpHrRenameConnection)(const GUID *, PCWSTR);
88     lpHrRenameConnection RenameConnectionFunc = NULL;
89     HRESULT status;
90
91     // First try the IShellFolder interface, which was unimplemented
92     // for the network connections folder before XP.
93     status = rename_shellfolder(GuidString, NewName);
94     if (status == E_NOTIMPL)
95     {
96         // The IShellFolder interface is not implemented on this platform.
97         // Try the (undocumented) HrRenameConnection API in the netshell
98         // library.
99         CLSID clsid;
100         HINSTANCE hNetShell;
101         status = CLSIDFromString((LPOLESTR) GuidString, &clsid);
102         if (FAILED(status))
103             return -1;
104         hNetShell = LoadLibrary(NETSHELL_LIBRARY);
105         if (hNetShell == NULL)
106             return -1;
107         RenameConnectionFunc =
108           (lpHrRenameConnection) GetProcAddress(hNetShell,
109                                                 "HrRenameConnection");
110         if (RenameConnectionFunc == NULL)
111         {
112             FreeLibrary(hNetShell);
113             return -1;
114         }
115         status = RenameConnectionFunc(&clsid, NewName);
116         FreeLibrary(hNetShell);
117     }
118     if (FAILED(status))
119         return -1;
120     return 0;
121 }