呵呵,msdn 上的例子,希望不要吓着你:HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath)
{
HRESULT hres;
IShellLink* psl;
char szGotPath[MAX_PATH];
char szDescription[MAX_PATH];
WIN32_FIND_DATA wfd;
*lpszPath = 0; // assume failure
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;
// Get a pointer to the IPersistFile interface.
hres = psl->QueryInterface(&IID_IPersistFile,
&ppf);
if (SUCCEEDED(hres)) {
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz,
MAX_PATH);
// Load the shortcut.
hres = ppf->Load(wsz, STGM_READ);
if (SUCCEEDED(hres)) {
// Resolve the link.
hres = psl->Resolve(hwnd, 0);
if (SUCCEEDED(hres)) {
// Get the path to the link target.
hres = psl->GetPath(szGotPath,
MAX_PATH, (WIN32_FIND_DATA *)&wfd,
SLGP_SHORTPATH );
if (FAILED(hres)
HandleErr(hres); // application-defined function
// Get the description of the target.
hres = psl->GetDescription(szDescription, MAX_PATH);
if (FAILED(hres))
HandleErr(hres);
lstrcpy(lpszPath, szGotPath);
}
}
// Release the pointer to the IPersistFile interface.
ppf->Release();
}
// Release the pointer to the IShellLink interface.
psl->Release();
}
return hres;
}
{
HRESULT hres;
IShellLink* psl;
char szGotPath[MAX_PATH];
char szDescription[MAX_PATH];
WIN32_FIND_DATA wfd;
*lpszPath = 0; // assume failure
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;
// Get a pointer to the IPersistFile interface.
hres = psl->QueryInterface(&IID_IPersistFile,
&ppf);
if (SUCCEEDED(hres)) {
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz,
MAX_PATH);
// Load the shortcut.
hres = ppf->Load(wsz, STGM_READ);
if (SUCCEEDED(hres)) {
// Resolve the link.
hres = psl->Resolve(hwnd, 0);
if (SUCCEEDED(hres)) {
// Get the path to the link target.
hres = psl->GetPath(szGotPath,
MAX_PATH, (WIN32_FIND_DATA *)&wfd,
SLGP_SHORTPATH );
if (FAILED(hres)
HandleErr(hres); // application-defined function
// Get the description of the target.
hres = psl->GetDescription(szDescription, MAX_PATH);
if (FAILED(hres))
HandleErr(hres);
lstrcpy(lpszPath, szGotPath);
}
}
// Release the pointer to the IPersistFile interface.
ppf->Release();
}
// Release the pointer to the IShellLink interface.
psl->Release();
}
return hres;
}
不过很谢谢你的答案.
但这段程序在编译的时候
hres = psl->QueryInterface(&IID_IPersistFile,
&ppf);
中的两个参数有问题,我把第一个&去掉,第二个参数前加了
一个(VOID**)强制转换才通过,可以这样换吗?
还有程序中的HandleErr识别不出来,要包含哪个头文件呢?
最后问一个弱小的问题,ResolveIt的第一个参数到底是该
赋什么值?
了。
什么?你竟然问可不可以?当然是不可以这样转换了!而是“必须”这样转换!
呵呵,HandleErr 当然是自己写个处理错误代码的函数了(HRESULT)。
至于 hwnd,IShellLink 把它作为错误信息/提示信息对话框的父窗口。@C MSDN IShellLink Reference
犯罪一样:))
对了,你能不能帮忙看一看我这里的调用是否可行
这是基于MFC的单对话框程序中的"确定"键的程序
void CTestDlg::OnOK()
{
LPSTR lpFileName;
lpFileName=new char[256];
for(int i=0;i<256;i++)
lpFileName[i]=0;
if(SUCCEEDED(ResolveIT(hwnd,
"C:\\WINDOWS\\Recent\\file.lnk",
lpFileName)
)
)
AfxMessageBox("SUCCESS!");
else
AfxMessageBox("FAILURE!");
delete(lpFileName);
}
一开始我把hwnd设为NULL了,返回总是FAILURE
该怎么设置hwnd呢?能否具体一些,我对于这方面
是很弱小的:)
麻烦你了.....不知你在线上吗,我都有些困了
zzzzzzzzzzzzzz.......................
::CoInitialize(NULL);
最好别忘了在 CYourApp::ExitInstance 里加上这行代码:
::CoUninitialize();提示:DEBUG/TRACE 进 ResolveIt,看一看出错的地方 hres 是否等于 0x800401f0。