我有一個簡單的 Delphi 應用程式,可以為 URL 創建桌面快捷方式。它.url在用戶的桌面檔案夾中創建一個帶有檔案擴展名的兩行文本檔案:
[InternetShortcut]
URL=http://127.0.0.1/admin
這很好用。當我需要使用新 URL 更新檔案時,我會覆寫舊檔案。但是在我重新啟動資源管理器或重新啟動之前,Windows 不會識別更改。所以我SHChangeNotify()在覆寫檔案后了解并呼叫了它:
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH or SHCNF_FLUSH, PChar(Path), nil);
但它沒有效果:
- 我試過有沒有
SHCNF_FLUSH國旗; - 也
SHCNF_FLUSHNOWAIT標志沒什么區別。 - 我也嘗試先洗掉檔案,然后使用
SHCNE_DELETE事件,然后重新創建檔案。這也不起作用,它只是繼續使用舊的 URL。
如何強制資源管理器在不重新啟動的情況下從檔案重新加載 URL?
uj5u.com熱心網友回復:
雖然檔案的內容可以像任何 INI 檔案一樣對待,但我還沒有找到一種直接的方法來控制對它的操作:
- 當創建一個檔案時,它的內容按預期讀取:系統默認的
URL='s 協議應用程式被啟動(即http它很可能是互聯網瀏覽器)。 - 修改每個檔案系統的檔案沒有任何影響 - MSIE 本身維護快取或 COM 的魔法。
可以通過以下方式間接操作:
- 清空檔案的現有內容。為什么?因為后面的步驟只會
URL=再次添加具有值的相同 INI 部分,但第一個部分的URL=值仍然是被考慮的那個。 - 訪問每個 COM 的檔案并更改其屬性。可悲的是,這會在檔案中寫入更多內容 - 在我的情況下,結果/檔案的內容是:
[{000214A0-0000-0000-C000-000000000046}] Prop3=19,2 [InternetShortcut] URL=http://127.0.0.1/index.php IDList=
但是,它“有效”如下:更改(說:不同的 URL)被識別。把它放在一起,我在 Windows 7 上的 Delphi 7 的以下代碼也應該對你有用 - 只需呼叫函式:
uses
ShlObj, ActiveX, ComObj;
const
SID_IUniformResourceLocatorA= '{FBF23B80-E3F0-101B-8488-00AA003E56F8}';
SID_IUniformResourceLocatorW= '{CABB0DA0-DA57-11CF-9974-0020AFD79762}';
SID_InternetShortcut= '{FBF23B40-E3F0-101B-8488-00AA003E56F8}';
type
PUrlInvokeCommandInfoA= ^TUrlInvokeCommandInfoA;
TUrlInvokeCommandInfoA= record
dwcbSize,
dwFlags: DWORD; // Bit field of IURL_INVOKECOMMAND_FLAGS
hwndParent: HWND; // Parent window. Valid only if IURL_INVOKECOMMAND_FL_ALLOW_UI is set.
pcszVerb: LPCSTR; // Verb to invoke. Ignored if IURL_INVOKECOMMAND_FL_USE_DEFAULT_VERB is set.
end;
PUrlInvokeCommandInfoW= ^TUrlInvokeCommandInfoW;
TUrlInvokeCommandInfoW= record
dwcbSize,
dwFlags: DWORD;
hwndParent: HWND;
pcszVerb: LPCWSTR;
end;
IUniformResourceLocatorA= interface( IUnknown )
[SID_IUniformResourceLocatorA]
function SetURL( pcszURL: LPCSTR; dwInFlags: DWORD ): HRESULT; stdcall;
function GetURL( ppszURL: LPSTR ): HRESULT; stdcall;
function InvokeCommand( purlici: PUrlInvokeCommandInfoA ): HRESULT; stdcall;
end;
IUniformResourceLocatorW= interface( IUnknown )
[SID_IUniformResourceLocatorW]
function SetURL( pcszURL: LPCWSTR; dwInFlags: DWORD ): HRESULT; stdcall;
function GetURL( ppszURL: LPWSTR ): HRESULT; stdcall;
function InvokeCommand(purlici: PUrlInvokeCommandInfoW ): HRESULT; stdcall;
end;
function SetURL( sFile, sUrl: Widestring ): Integer;
const
CLSID_InternetShortCut: TGUID= SID_InternetShortcut;
var
oUrl: IUniformResourceLocatorW;
oFile: IPersistFile;
hFile: THandle;
begin
// First, the existing file's content should be emptied
hFile:= CreateFileW( PWideChar(sFile), GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0, 0 );
if hFile= INVALID_HANDLE_VALUE then begin
result:= 1; // File might not exist, sharing violation, etc.
exit;
end;
// Initial file pointer is at position 0
if not SetEndOfFile( hFile ) then begin
result:= 2; // Missing permissions, etc.
CloseHandle( hFile );
exit;
end;
// Gracefully end accessing the file
if not CloseHandle( hFile ) then begin
result:= 3; // File system crashed, etc.
exit;
end;
// Using COM to access properties
result:= 0;
try
oUrl:= CreateComObject( CLSID_InternetShortCut ) as IUniformResourceLocatorW;
except
result:= 4; // CLSID unsupported, COM not available, etc.
end;
if result<> 0 then exit;
// Opening the file again
oFile:= oUrl as IPersistFile;
if oFile.Load( PWideChar(sFile), STGM_READWRITE )<> S_OK then begin
result:= 5; // Sharing violations, access permissions, etc.
exit;
end;
// Set the property as per interface - only saving the file is not enough
if oUrl.SetURL( PWideChar(sUrl), 0 )<> S_OK then begin
result:= 6;
exit;
end;
// Storing the file's new content - setting only the property is not enough
if oFile.Save( PWideChar(sFile), TRUE )<> S_OK then begin
result:= 7;
exit;
end;
// Success!
result:= 0;
end;
根據我的桌面防火墻,執行程序會修改explorer.exeon的記憶體IPersistFile.Save()- 在執行 URL 檔案之后應該反映其新內容,而在此之前的任何嘗試仍然應該對舊檔案的內容起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/388375.html
上一篇:nasm中的嵌套回圈
