Desktop Context menu "Paste Shortcut" issue, Please help
-
Hi all, Please go through below function. Copy any file/folder & then use this function to past Shortcut It is crashing "InvokeCommand". Please help me.
void PasteShortcut()
{
try
{
CoInitialize(NULL);
IShellFolder* psf;
HWND hWnd = GetDesktopWindow();
HRESULT hr;
if (SUCCEEDED(SHGetDesktopFolder(&psf)))
{
IContextMenu* pcm;
if (SUCCEEDED(psf->CreateViewObject(hWnd,IID_IContextMenu,(void**)&pcm)))
{
HMENU hmenu = CreatePopupMenu();
if (hmenu != NULL) {
if (SUCCEEDED(pcm->QueryContextMenu(hmenu, 0,
0, 0xff, // since we're invoking by canonical name the menu ID range passed to QueryContextMenu can be arbitrary
CMF_NORMAL))) {
CMINVOKECOMMANDINFO cmi={0};
cmi.hwnd=hWnd;
cmi.cbSize=sizeof(cmi);
cmi.lpVerb= ".lnk";
hr= pcm->InvokeCommand(&cmi);
if (SUCCEEDED(hr)) {
MessageBox(hWnd, TEXT("Shortcut created"), TEXT("Message"), 0);
}
}
DestroyMenu(hmenu);
}
pcm->Release();
}
psf->Release();
}
CoUninitialize();} catch(...) { }
}
-
Hi all, Please go through below function. Copy any file/folder & then use this function to past Shortcut It is crashing "InvokeCommand". Please help me.
void PasteShortcut()
{
try
{
CoInitialize(NULL);
IShellFolder* psf;
HWND hWnd = GetDesktopWindow();
HRESULT hr;
if (SUCCEEDED(SHGetDesktopFolder(&psf)))
{
IContextMenu* pcm;
if (SUCCEEDED(psf->CreateViewObject(hWnd,IID_IContextMenu,(void**)&pcm)))
{
HMENU hmenu = CreatePopupMenu();
if (hmenu != NULL) {
if (SUCCEEDED(pcm->QueryContextMenu(hmenu, 0,
0, 0xff, // since we're invoking by canonical name the menu ID range passed to QueryContextMenu can be arbitrary
CMF_NORMAL))) {
CMINVOKECOMMANDINFO cmi={0};
cmi.hwnd=hWnd;
cmi.cbSize=sizeof(cmi);
cmi.lpVerb= ".lnk";
hr= pcm->InvokeCommand(&cmi);
if (SUCCEEDED(hr)) {
MessageBox(hWnd, TEXT("Shortcut created"), TEXT("Message"), 0);
}
}
DestroyMenu(hmenu);
}
pcm->Release();
}
psf->Release();
}
CoUninitialize();} catch(...) { }
}
-
It looks like the content of your
CMINVOKECOMMANDINFO
structure is not complete; take a look at the documentation[^] for further details.It's time for a new signature.
Hi Richard, Thanks for reply... now I have tried below code... Below code is not showing crash as I have used "link" as lpVerb. as you said I have provided all Content of CMINVOKECOMMANDINFO structure. "InvokeCommand" is return S_OK But still Paste Shortcut is not happening on Desktop. :-(
void PasteShortcut()
{
try
{
CoInitialize(NULL);IShellFolder\* psf; HWND hWnd = GetDesktopWindow(); HRESULT hr; if (SUCCEEDED(SHGetDesktopFolder(&psf))) { IContextMenu\* pcm; if (SUCCEEDED(psf->CreateViewObject(hWnd,IID\_IContextMenu,(void\*\*)&pcm))) { HMENU hmenu = CreatePopupMenu(); if (hmenu != NULL) { if (SUCCEEDED(pcm->QueryContextMenu(hmenu, 0, 0 , 0xff, // since we're invoking by canonical name the menu ID range passed to QueryContextMenu can be arbitrary CMF\_NORMAL))) { CMINVOKECOMMANDINFO cmi; cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.fMask = 0; cmi.hwnd = hWnd; cmi.lpVerb ="link" ;//(LPCSTR)(INT\_PTR)(idCmd - 1); cmi.lpParameters = NULL; cmi.lpDirectory = NULL; cmi.nShow = SW\_NORMAL; cmi.dwHotKey = 0; cmi.hIcon = NULL; hr = pcm->InvokeCommand(&cmi); if (SUCCEEDED(hr)) { MessageBox(hWnd, TEXT("Shortcut created"), TEXT("Message"), 0); } } DestroyMenu(hmenu); } pcm->Release(); } psf->Release(); } CoUninitialize(); } catch(...) { }
}
-
Hi Richard, Thanks for reply... now I have tried below code... Below code is not showing crash as I have used "link" as lpVerb. as you said I have provided all Content of CMINVOKECOMMANDINFO structure. "InvokeCommand" is return S_OK But still Paste Shortcut is not happening on Desktop. :-(
void PasteShortcut()
{
try
{
CoInitialize(NULL);IShellFolder\* psf; HWND hWnd = GetDesktopWindow(); HRESULT hr; if (SUCCEEDED(SHGetDesktopFolder(&psf))) { IContextMenu\* pcm; if (SUCCEEDED(psf->CreateViewObject(hWnd,IID\_IContextMenu,(void\*\*)&pcm))) { HMENU hmenu = CreatePopupMenu(); if (hmenu != NULL) { if (SUCCEEDED(pcm->QueryContextMenu(hmenu, 0, 0 , 0xff, // since we're invoking by canonical name the menu ID range passed to QueryContextMenu can be arbitrary CMF\_NORMAL))) { CMINVOKECOMMANDINFO cmi; cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.fMask = 0; cmi.hwnd = hWnd; cmi.lpVerb ="link" ;//(LPCSTR)(INT\_PTR)(idCmd - 1); cmi.lpParameters = NULL; cmi.lpDirectory = NULL; cmi.nShow = SW\_NORMAL; cmi.dwHotKey = 0; cmi.hIcon = NULL; hr = pcm->InvokeCommand(&cmi); if (SUCCEEDED(hr)) { MessageBox(hWnd, TEXT("Shortcut created"), TEXT("Message"), 0); } } DestroyMenu(hmenu); } pcm->Release(); } psf->Release(); } CoUninitialize(); } catch(...) { }
}