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
|
//------------------------------------------------------------------------------
// CreateWirelessShortcut
//
// Creates a .lnk file for access to the wireless connection properties.
//
// Args:
// strWifiLink = path and unique name for Wifi link (using the adapter GUID)
//
// Return:
// BOOL = TRUE if successful
//
BOOL CMainFrame::CreateWirelessShortcut(const CString& strWifiLink)
{
HRESULT hResult = NULL;
hResult = ::CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(PVOID*)&m_piShellLink);
if (SUCCEEDED(hResult))
{
IPersistFile* piPersistFile = NULL;
GetNetConnPIDL();
hResult = m_piShellLink->QueryInterface(IID_IPersistFile, (PVOID*)&piPersistFile);
if (SUCCEEDED(hResult))
{
WCHAR pszPath[MAX_PATH] = {0};
if (::MultiByteToWideChar(CP_ACP, 0, strWifiLink, -1, pszPath, MAX_PATH) != 0)
{
hResult = piPersistFile->Save(pszPath, TRUE);
}
else
{
hResult = -1;
}
piPersistFile->Release();
}
m_piShellLink->Release();
}
return (SUCCEEDED(hResult));
}
//------------------------------------------------------------------------------
// GetNetConnPIDL
//
// Gets the PIDL of the wireless adapter listed in 'Network Connections'.
//
// Args:
// None
//
// Returns:
// None
//
void CMainFrame::GetNetConnPIDL()
{
// Bind to network connections folder.
ITEMIDLIST* pIDList1 = NULL;
LPSHELLFOLDER lpDesktop = NULL;
LPSHELLFOLDER lpNCFolder = NULL;
SHGetFolderLocation(NULL, CSIDL_CONNECTIONS, NULL, 0, &pIDList1);
SHGetDesktopFolder(&lpDesktop);
lpDesktop->BindToObject(pIDList1, NULL, IID_IShellFolder,(LPVOID*)&lpNCFolder);
lpDesktop->Release();
// Enumerate subitems.
ITEMIDLIST* pIDList2 = NULL;
LPENUMIDLIST lpItems = NULL;
lpNCFolder->EnumObjects(NULL, SHCONTF_NONFOLDERS, &lpItems);
while (S_OK == lpItems->Next(1, &pIDList2, NULL))
{
STRRET sr = {STRRET_WSTR};
lpNCFolder->GetDisplayNameOf(pIDList2, SHGDN_NORMAL, &sr);
TCHAR chBuffer[MAX_PATH] = _T("");
StrRetToBuf(&sr, pIDList2, chBuffer, MAX_PATH);
if (0 == _tcsicmp(chBuffer, WIRELESS_LINK_DESC))
{
ITEMIDLIST* pIDList3 = ILCombine(pIDList1, pIDList2);
m_piShellLink->SetIDList(pIDList3);
ILFree(pIDList3);
ILFree(pIDList2);
break;
}
ILFree(pIDList2);
pIDList2 = NULL;
}
ILFree(pIDList1);
}
|