|
仔细看了一下,那段VB代码的VISTA部分有点问题
索性我就全部翻译成VC吧
// For XP
void Pin()
{
CoInitialize(NULL);
CComPtr<IShellDispatch> pShell;
CoCreateInstance(CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID*)&pShell);
CComPtr<Folder> pFolder;
pShell->NameSpace(CComVariant(_T("C:\\Windows\\System32")), &pFolder);
CComPtr<FolderItem> pItem;
pFolder->ParseName(CComBSTR(_T("notepad.exe")), &pItem);
pItem->InvokeVerb(CComVariant(_T("附到「开始」菜单(&I)")));
}
void UnPin()
{
CoInitialize(NULL);
CComPtr<IShellDispatch> pShell;
CoCreateInstance(CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID*)&pShell);
CComPtr<Folder> pFolder;
pShell->NameSpace(CComVariant(_T("C:\\Windows\\System32")), &pFolder);
CComPtr<FolderItem> pItem;
pFolder->ParseName(CComBSTR(_T("notepad.exe")), &pItem);
pItem->InvokeVerb(CComVariant(_T("从「开始」菜单脱离(&I)")));
}
// For Vista,其实XP中也能用,就是比上面的多了个循环判断
void PinVista()
{
CoInitialize(NULL);
CComPtr<IShellDispatch> pShell;
CoCreateInstance(CLSID_Shell, NULL, CLSCTX_SERVER, IID_IDispatch, (LPVOID*)&pShell);
CComPtr<Folder> pFolder;
pShell->NameSpace(CComVariant(_T("C:\\Windows\\System32")), &pFolder);
CComPtr<FolderItem> pItem;
pFolder->ParseName(CComBSTR(_T("notepad.exe")), &pItem);
CComPtr<FolderItemVerbs> pItemVerbs;
pItem->Verbs(&pItemVerbs);
long lCount;
pItemVerbs->get_Count(&lCount);
for (long i=0; i!=lCount; i++)
{
CComPtr<FolderItemVerb> pItemVerb;
pItemVerbs->Item(CComVariant(i), &pItemVerb);
CComBSTR bstrName;
pItemVerb->get_Name(&bstrName);
if (bstrName == _T("附到「开始」菜单(&I)"))
{
pItemVerb->DoIt();
}
}
}
这样整合进PECMD应该很容易啦^_^
CoInitialize(NULL);是COM的初始函数,程序的初始化部分写一次就可以了,我写的时候为了测试方便,所以每段函数里都有。
我用的XP测试,For Vista 的菜单名是用的XP的
用到几个ATL的工具类,纯API的话还得改改
PS: 没有缩进代码好丑^_^
[ 本帖最后由 风沙星辰 于 2009-9-12 01:42 编辑 ] |
|