300 分的 API, 在线等(有人回答了,但没有解决,高手们请进入) ( 积分: 300 )

  • 主题发起人 主题发起人 WataShiSuKi
  • 开始时间 开始时间
W

WataShiSuKi

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何在自已的程序中引入并使用“SHGetIDispatchForFolder”这个API
 
请问如何在自已的程序中引入并使用“SHGetIDispatchForFolder”这个API
 
类似这个这样,动态调用<br><br> fun = function(ProcessID: hwnd; ServiceFlags: hwnd): hwnd; stdcall;<br><br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var h: hwnd;<br> &nbsp;kk: fun;<br>begin<br><br> &nbsp; &nbsp;h := LoadLibrary('kernel32.dll');<br> &nbsp; &nbsp;if h &lt;&gt; 0 then begin<br> &nbsp; &nbsp; &nbsp;kk := GetProcAddress(h, 'RegisterServiceProcess');<br> &nbsp; &nbsp; &nbsp;if @kk &lt;&gt; nil then kk(Windows.GetCurrentProcessId, 1);<br> &nbsp; &nbsp;end;<br>end;
 
多谢上面的回答!<br>问题是我不知到这个API用的参数是什么!!<br><br>function SHGetIDispatchForFolder(ITEMIDLIST : PItemIDList; ??? : ???) : ???; stdcall;
 
uses SHDocVw, ShlObj;<br>function SHGetIDispatchForFolder(pidl: PItemIDList; var ppwba: IWebBrowserApp): Longint;<br> &nbsp;stdcall; external 'shdocvw.dll' name 'SHGetIDispatchForFolder';
 
多谢上面的回答!
 
我原来也是用 IWebBrowserApp 的,但这个“IWebBrowserApp”还是不会用:
 
不好意思,没仔细看声明,第二个参数应该是个变参:<br>uses SHDocVw, ShlObj;<br>function SHGetIDispatchForFolder(pidl: PItemIDList; var ppwba: IWebBrowserApp): Longint;<br> &nbsp;stdcall; external 'shdocvw.dll' name 'SHGetIDispatchForFolder';
 
第二个参数还是不会用(查找目标)
 
给你个例子,打开特殊文件夹:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> &nbsp;pidl: PItemIDList;<br> &nbsp;pwba: IWebBrowserApp;<br>begin<br> &nbsp;//SIDL_RECENT, CSIDL_PERSONAL, CSIDL_APPDATA<br> &nbsp;SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, pidl);<br> &nbsp;SHGetIDispatchForFolder(pidl, pwba);<br>end;
 
有是未能及时加回复!!!
 
怎么个查找目标啊,SHGetIDispatchForFolder 有这个作用么。
 
确实太长了,不好进来!<br>这样就好了!<br>int GetNumberOfSelected(HGLOBAL hMem)<br>{<br>int ret = 0;<br>int* pInt;<br><br>if ((pInt = (int*)GlobalLock(hMem)) != NULL) {<br>ret = *pInt;<br>GlobalUnlock(hMem);<br>}<br><br>return ret;<br>}<br><br>BOOL _7FD0276A(DWORD* vp, DWORD var)<br>{<br>if (var &lt;= vp[0])<br>return SHELL32_25((BYTE*)vp + vp[1], (BYTE*)vp + vp[var + 2]);<br><br>return FALSE;<br>}<br><br>LPCITEMIDLIST GetPIDL(HGLOBAL hGlobal, int var)<br>{<br>LPVOID vp;<br><br>if ((vp = GlobalLock(hGlobal)) != NULL) {<br>LPCITEMIDLIST pidl = _7FD0276A(vp, var);<br>GlobalUnlock(hGlobal);<br>return pidl;<br>}<br><br>return NULL;<br>}<br><br>BOOL GetTargetInfo(HGLOBAL hGlobal, int var, char FilePath[MAX_PATH], WIN32_FIND_DATA* lpFindData)<br>{<br>LPCITEMIDLIST pidl;<br><br>PathName[0] = '/0';<br>if ((pidl = GetPIDL(hGlobal, var)) != NULL) {<br>if (SHGetPathFromIDList(pidl, FilePath)) {<br>if (lpFindData) {<br>if ((hFff = FindFirstFile(FilePath, lpFindData)) == INVALID_HANDLE_VALUE)<br>memset(lpFindData, 0, sizeof(WIN32_FIND_DATA));<br>else<br>CloseHandle(hFff);<br>}<br><br>SHELL32_155_FreePidl(pidl);<br>return TRUE;<br>}<br>else {<br>SHELL32_155_FreePidl(pidl);<br>return FALSE;<br>}<br>}<br><br>return FALSE;<br>}<br><br>int CreatePropDialog(p1, pfn, IDataObject* pDataObj)<br>{<br>FORMATETC formatetc;<br>STGMEDIUM medium;<br>PROPSHEETPAGE psp;<br>HLOCAL hLocal;<br>HGLOBAL hGlobal;<br>WIN32_FIND_DATA ffd;<br>HPROPSHEETPAGE hPsp;<br>char PathName[MAX_PATH];<br><br>BOOL var_4;<br>int var_8;<br><br>formatetc.lindex = -1;<br>formatetc.cfFormat = gcfFormat;<br>formatetc.ptd = 0;<br>formatetc.dwAspect = 1;<br>formatetc.tymed = TYMED_HGLOBAL;<br><br>var_4 = FALSE;<br>var_8 = 0;<br><br>if (FAILED(pDataObj-&gt;GetData(&amp;formatetc, &amp;medium)))<br>return FALSE;<br><br>PathName[0] = 0;<br>psp.hInstance = ghModule;<br>psp.dwSize = 952; //NOT sizeof(psp); !!!<br>psp.dwFlags = PSP_USECALLBACK; //0x80;<br>psp.pfnCallback = PropCallback;<br>var_2a0 = 1;<br>var_3a8 = 0;<br>var_34 = 0;<br>var_30 = 0;<br><br>hGlobal = medium.hGlobal;<br>if (hGlobal) {<br>hLocal = LocalAlloc(LPTR, GlobalSize(hGlobal));<br>if (hLocal) <br>memmove(hLocal, hGlobal, GlobalSize(hGlobal));<br>}<br>else<br>hLocal = NULL;<br><br>if (GetNumberOfSelected(hGlobal) == 1) { // else 7fd05dd5<br>if (GetTargetInfo(hGlobal, 0, &amp;PathName, &amp;ffd)) { // else 7fd05d9f<br>psp.pfnDlgProc = ConventionalDlgProc;<br>psp.pResource = MAKEINTRESOURCE(ffd.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY? 1044 : 1041);<br>if ((hPsp = CreatePropertySheetPage(&amp;psp)) != NULL) { // else loc_7FD05D9F<br>if (pfn(hPsp, p1)) { // else loc_7FD05E07<br>var_4 = 1;<br>if (CreateShortcutPage(hGlobal, &amp;PathName, pfn, p1))<br>var_8 = 2;<br>CreateVersionPage(&amp;PathName, pfn, p1);<br>}<br>else // loc_7FD05E07<br>DestroyPropertySheetPage(hPsp);<br>}<br>}<br>}<br>else { // loc_7FD05DD5<br>psp.pResource = MAKEINTRESOURCE(1043);<br>psp.pfnDlgProc = MultiSelDlgProc;<br>if ((hPsp = CreatePropertySheetPage(&amp;psp)) != NULL) { // else loc_7FD05D9F<br>if (pfn(hPsp, p1))<br>var_4 = 1;<br>else<br>DestroyPropertySheetPage(hPsp);<br>}<br>}<br><br>// loc_7FD05D9F<br>_7FCC6204(&amp;medium);<br>if (!var_4 &amp;&amp; hLocal)<br>LocalFree(hLocal);<br><br>return var_8;<br>}<br><br>BOOL IsLnkFile(char* PathName)<br>{<br>if (PathName)<br>return lstrcmpi(PathFindExtension(PathName), &quot;.lnk&quot;) == 0;<br><br>return FALSE;<br>}<br><br><br>HRESULT CreateShellInstance(IUnknown* pUnknownOuter, const IID&amp; iid, void** ppv)<br>{<br>CShellFactory* pCShellFactory;<br>HRESULT hr;<br><br>if (pUnknownOuter)<br>return CLASS_E_NOAGGREGATION;<br><br>pCShellFactory = new CShellFactory;<br><br>if (pCShellFactory == NULL)<br>return E_OUTOFMEMORY;<br><br>pCShellFactory-&gt;m_cRef = 1;<br>pCShellFactory-&gt;Init();<br><br>hr = pCShellFactory-&gt;CreateInstance(iid, ppv);<br><br>pCShellFactory-&gt;Release();<br><br>return hr;<br>}<br><br>typedef struct FindTargetData<br>{<br>HGLOBAL hGlobal; // 0<br>IShellLinkA* pIShellLink; // 4<br>int hDlgWnd; // 8<br>int dwc; // c<br>int dw10; // 10<br>int dw14; // 14<br>int dw18; // 18<br>char Path[MAX_PATH]; // 1c<br>};<br><br>BOOL CreateShortcutPage(HGLOBAL hGlobal, char PathName[MAX_MATH], pfn, p1)<br>{<br>PROPSHEETPAGE psp;<br>IShellLink* pIShellLink;<br>FindTargetData* pFtd;<br>HPROPSHEETPAGE hPsp;<br><br>if (IsLinkFile(PathName) &amp;&amp; <br>SUCCEEDED(CreateShellInstance(NULL, IID_IShellLink, &amp;pIShellLink))) <br>{ // else loc_7FD040F6<br>pFtd = (FindTargetData*) LocalAlloc(LPTR, sizeof(FindTargetData));<br>if (pFtd != NULL) { // else loc_7FD040ED<br>psp.hInstance = ghModule;<br>psp.dwSize = sizeof(PROPSHEETPAGE);<br>psp.dwFlags = PSP_USECALLBACK;<br>psp.pResource = MAKEINTRESOURCE(1040);<br>psp.pfnDlgProc = ShortcutDlgProc;<br>psp.pfnCallback = CallbackProc;<br>psp.lParam = pFtd;<br>lstrcpyn(pFtd-&gt;Path, PathName, MAX_PATH);<br>pFtd-&gt;dw14 = -1;<br>pFtd-&gt;hGlobal = hGlobal;<br>pFtd-&gt;pIShellLink = pIShellLink;<br>if ((hPsp = CreatePropertySheetPage(&amp;psp)) != NULL) {<br>if (pfn(hPsp, p1))<br>return TRUE;<br>DestroyPropertySheetPage(hPsp);<br>}<br>LocalFree(pFtd);<br>}<br>pObj-&gt;Release();<br>}<br><br>return FALSE;<br>}
 
先多谢你对我们这些&quot;初学者&quot;热心的帮助!
 
呵呵,代码太长,没时间看,“查找目标”的具体效果是什么样的?<br>感觉这篇文章/代码的作者基本是个半疯,光会写代码,语言表达一塌糊涂。
 
如果是“快捷方式”,单击右KEY的属性,我就是想要“查找目标”的功能:<br>可以找到文件所在的地方,再自动选择目标!
 
//这个 API 的用法写在下面了,它打开 Link 所指文件所在的文件夹<br>procedure FindShellLink(hWnd: HWND; const lpShellLink: PWideChar);<br>var<br> &nbsp;ppf: IPersistFile;<br> &nbsp;psl: IShellLink;<br> &nbsp;pszFile: array[0..MAX_PATH] of Char;<br> &nbsp;pfd: _WIN32_FIND_DATAA;<br> &nbsp;lpPath{, lpFile}: string;<br> &nbsp;psf: IShellFolder;<br> &nbsp;dwNum, dwFlags: LongWord;<br> &nbsp;pidl: PItemIDList;<br> &nbsp;pwba: IWebBrowserApp;<br>begin<br> &nbsp;ppf := CreateComObject(CLSID_ShellLink) as IPersistFile;<br> &nbsp;ppf.Load(lpShellLink, STGM_READ);<br> &nbsp;psl := ppf as IShellLink;<br> &nbsp;psl.Resolve(0, SLR_ANY_MATCH or SLR_NO_UI);<br> &nbsp;psl.GetPath(pszFile, MAX_PATH, pfd, SLGP_UNCPRIORITY);<br> &nbsp;lpPath := ExtractFilePath(pszFile);<br> &nbsp;//lpFile := ExtractFileName(pszFile);<br><br> &nbsp;SHGetDesktopFolder(psf);<br> &nbsp;psf.ParseDisplayName(hWnd, nil, StringToOleStr(lpPath), dwNum, pidl, dwFlags);<br> &nbsp;SHGetIDispatchForFolder(pidl, pwba);<br>end;<br><br>begin<br> &nbsp;FindShellLink(Handle, 'C:/Documents and Settings/Red Baron/桌面/Microsoft Visual C++ 6.0.lnk');<br>end;
 
显然是把问题复杂化了。<br>按您的要求其实只要从快捷方式中取出目标执行文件,读取快捷方式和建立快捷方式的代码很多就不用我写了,得到目标文件后,再用Explorer.exe带/select参数打开文件就可以了。下面是我写的一个过程:<br>//用资源管理器打开文件所在目录并定位选中文件<br>procedure OpenExplorer(FileName : String);<br>begin<br> ShellExecute(0, nil, PChar('Explorer.exe'),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PChar('/select,'+ FileName), nil, SW_SHOWNORMAL);<br>end;
 
注意:'/select,'后面一定要有个“,”号
 
兄弟你可以看下這個api函數的說明:<br>Retrieves information about an object in the file system, such as a file, a folder, a directory, or a drive root.<br><br>WINSHELLAPI DWORD WINAPI SHGetFileInfo(<br><br> &nbsp; &nbsp;LPCTSTR pszPath, <br> &nbsp; &nbsp;DWORD dwFileAttributes, <br> &nbsp; &nbsp;SHFILEINFO FAR *psfi, <br> &nbsp; &nbsp;UINT cbFileInfo, <br> &nbsp; &nbsp;UINT uFlags <br> &nbsp; ); <br> <br><br>Parameters<br><br>pszPath<br><br>Pointer to a buffer that contains the path and filename. Both absolute and relative paths are valid. If uFlags includes the SHGFI_PIDL, value pszPath must be the address of an ITEMIDLIST structure that contains the list of item identifiers that uniquely identifies the file within the shell's name space. <br>This string can use either short (the 8.3 form) or long filenames.<br><br>dwFileAttributes<br><br>Array of file attribute flags (FILE_ATTRIBUTE_ values). If uFlags does not include the SHGFI_USEFILEATTRIBUTES value, this parameter is ignored.<br><br>psfi and cbFileInfo<br><br>Address and size, in bytes, of the SHFILEINFO structure that receives the file information. <br><br>uFlags<br><br>Flag that specifies the file information to retrieve. This parameter can be a combination of the following values:<br><br>SHGFI_ATTRIBUTES Retrieves the file attribute flags. The flags are copied to the dwAttributes member of the structure specified by psfi. <br>SHGFI_DISPLAYNAME Retrieves the display name for the file. The name is copied to the szDisplayName member of the structure specified by psfi.The returned display name uses the long filename, if any, rather than the 8.3 form of the filename.<br>SHGFI_EXETYPE Returns the type of the executable file if pszPath identifies an executable file. For more information, see the comments below.<br>SHGFI_ICON Retrieves the handle of the icon that represents the file and the index of the icon within the system image list. The handle is copied to the hIcon member of the structure specified by psfi, and the index is copied to the iIcon member. The return value is the handle of the system image list.<br>SHGFI_ICONLOCATION Retrieves the name of the file that contains the icon representing the file. The name is copied to the szDisplayName member of the structure specified by psfi. <br>SHGFI_LARGEICON Modifies SHGFI_ICON, causing the function to retrieve the file's large icon. <br>SHGFI_LINKOVERLAY Modifies SHGFI_ICON, causing the function to add the link overlay to the file's icon.<br>SHGFI_OPENICON Modifies SHGFI_ICON, causing the function to retrieve the file's open icon. A container object displays an open icon to indicate that the container is open. <br>SHGFI_PIDL Indicates that pszPath is the address of an ITEMIDLIST structure rather than a path name. <br>SHGFI_SELECTED Modifies SHGFI_ICON, causing the function to blend the file's icon with the system highlight color. <br>SHGFI_SHELLICONSIZE Modifies SHGFI_ICON, causing the function to retrieve a shell-sized icon. If this flag is not specified, the function sizes the icon according to the system metric values. <br>SHGFI_SMALLICON Modifies SHGFI_ICON, causing the function to retrieve the file's small icon. <br>SHGFI_SYSICONINDEX Retrieves the index of the icon within the system image list. The index is copied to the iIcon member of the structure specified by psfi. The return value is the handle of the system image list.<br>SHGFI_TYPENAME Retrieves the string that describes the file's type. The string is copied to the szTypeName member of the structure specified by psfi.<br>SHGFI_USEFILEATTRIBUTES Indicates that the function should use the dwFileAttributes parameter.<br> <br><br>To retrieve the executable file type, uFlags must specify only SHGFI_EXETYPE. The return value specifies the type of the executable file: <br><br>0 Nonexecutable file or an error condition.<br>LOWORD = NE or PE<br>HIWORD = 3.0, 3.5, or 4.0 Windows application<br>LOWORD = MZ<br>HIWORD = 0 MS-DOS .EXE, .COM or .BAT file<br>LOWORD = PE<br>HIWORD = 0 Win32 console application<br> <br><br>Return Values<br><br>?Returns a value whose meaning depends on the uFlags parameter. If uFlags specifies the SHGFI_EXETYPE value, the return value indicates the type of the executable file. For more information, see the comments below.<br>?If uFlags includes the SHGFI_ICON or SHGFI_SYSICONINDEX value, the return value is the handle of the system image list that contains the large icon images. If the SHGFI_SMALLICON value is also included, the return value is the handle of the image list that contains the small icon images. <br><br>?If uFlags does not include SHGFI_EXETYPE, SHGFI_ICON, SHGFI_SYSICONINDEX, or SHGFI_SMALLICON, the return value is nonzero the function succeeds, or zero otherwise.
 
后退
顶部