* Copyright (c) 2008 VMware, Inc. All rights reserved.
* **********************************************************/
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of VMware, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include "stdafx.h"
#include "wizard.h"
#include "ShellInterface.h"
#include "assert.h"
#include <shlobj.h>
#include <objbase.h>
#include <shlwapi.h>
#ifdef _DEBUG
# undef THIS_FILE
static char THIS_FILE[] = __FILE__;
# define new DEBUG_NEW
#endif
CShellInterface::CShellInterface()
{
}
CShellInterface::~CShellInterface()
{
}
BOOL CShellInterface::m_bInitialized = FALSE;
void
CShellInterface::Initialize()
{
if (m_bInitialized)
return;
int res = CoInitialize(NULL);
assert(res == S_OK);
m_bInitialized = TRUE;
}
void
CShellInterface::Uninitialize()
{
if (!m_bInitialized)
return;
CoUninitialize();
m_bInitialized = FALSE;
}
BOOL
CShellInterface::CreateLinkFile(LPCTSTR pszShortcutFile, LPTSTR pszLink, LPTSTR pszDesc)
{
HRESULT hres;
IShellLink *psl;
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink,
(void **)&psl);
if (SUCCEEDED(hres)) {
IPersistFile *ppf;
hres = psl->QueryInterface(IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED(hres)) {
hres = psl->SetPath(pszShortcutFile);
if (!SUCCEEDED(hres))
AfxMessageBox(_T("SetPath failed!"));
hres = psl->SetDescription(pszDesc);
if (!SUCCEEDED(hres))
AfxMessageBox(_T("SetDescription failed!"));
#ifndef UNICODE
WORD wsz[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, pszLink, -1, wsz, MAX_PATH);
hres = ppf->Save(wsz, TRUE);
#else
hres = ppf->Save(pszLink, TRUE);
#endif
if (!SUCCEEDED(hres))
AfxMessageBox(_T("Save failed!"));
ppf->Release();
}
psl->Release();
}
return (SUCCEEDED(hres));
}
BOOL
CShellInterface::CopyDir(LPCTSTR from, LPCTSTR to, HWND hwnd)
{
assert(_tcslen(from) < MAX_PATH - 1);
assert(_tcslen(to) < MAX_PATH - 1);
TCHAR myfrom[MAX_PATH + 2];
_tcscpy(myfrom, from);
myfrom[_tcslen(myfrom) + 1] = _T('\0');
TCHAR myto[MAX_PATH + 2];
_tcscpy(myto, to);
myto[_tcslen(myto) + 1] = _T('\0');
SHFILEOPSTRUCT fileop;
fileop.hwnd = hwnd;
fileop.wFunc = FO_COPY;
fileop.pFrom = myfrom;
fileop.pTo = myto;
fileop.fFlags = FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION | FOF_SILENT;
int res = SHFileOperation(&fileop);
return (res == 0);
}
BOOL
CShellInterface::DeleteFile(LPCTSTR name, HWND hwnd)
{
assert(_tcslen(name) < MAX_PATH - 1);
TCHAR myname[MAX_PATH + 2];
_tcscpy(myname, name);
myname[_tcslen(myname) + 1] = _T('\0');
SHFILEOPSTRUCT fileop;
fileop.hwnd = hwnd;
fileop.wFunc = FO_DELETE;
fileop.pFrom = myname;
fileop.pTo = NULL;
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT;
int res = SHFileOperation(&fileop);
return (res == 0);
}
#if 0
"SHORTCUT: A SampleThat Manipulates Shortcuts"
except that sample had ansi strings
*/
HRESULT CreateShortCut::CreateIt (LPCSTR pszShortcutFile, LPSTR pszLink,
LPSTR pszDesc)
{
HRESULT hres;
IShellLink *psl;
hres = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (void **)&psl);
if (SUCCEEDED (hres)) {
IPersistFile *ppf;
hres = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
if (SUCCEEDED (hres)) {
WORD wsz [MAX_PATH];
hres = psl->SetPath (pszShortcutFile);
if (! SUCCEEDED (hres))
AfxMessageBox (_T("SetPath failed!"));
hres = psl->SetDescription (pszDesc);
if (! SUCCEEDED (hres))
AfxMessageBox (_T("SetDescription failed!"));
MultiByteToWideChar (CP_ACP, 0, pszLink, -1, wsz, MAX_PATH);
hres = ppf->Save (wsz, TRUE);
if (! SUCCEEDED (hres))
AfxMessageBox (“Save failed!”);
ppf->Release ();
}
psl->Release ();
}
return hres;
}
#endif