要創建可以使用InternetSetCookie
函數指定的URL相關的cookie。爲了檢索指定URL的cookie,您可以使用InternetGetCookie
函數。下面是他們的翻譯,其中展示瞭如何創建和讀取cookie(真正的代碼實現討論錯誤消息,或者我會保留的包裝函數;以此代碼爲例,展示如何使用這些API函數):
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
[Code]
#ifdef UNICODE
#define AW "W"
#else
#define AW "A"
#endif
const
ERROR_INSUFFICIENT_BUFFER = 122;
ERROR_NO_MORE_ITEMS = 259;
function InternetSetCookie(lpszUrl: string; lpszCookieName: string;
lpszCookieData: string): BOOL;
external 'InternetSetCookie{#AW}@wininet.dll stdcall';
function InternetGetCookie(lpszUrl: string; lpszCookieName: string;
lpszCookieData: string; var lpdwSize: DWORD): BOOL;
external 'InternetGetCookie{#AW}@wininet.dll stdcall';
function TryCreateCookie(const URL, Name, Data: string): Boolean;
begin
// try to create a specified cookie
Result := InternetSetCookie(URL, Name, Data);
// if the function call failed, we can optionally report the reason why
if not Result then
MsgBox('Cookie creation failed!' + #13#10 +
SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
function TryRetrieveCookie(const URL, Name: string; out Data: string): Boolean;
var
S: string;
BufferSize: DWORD;
begin
// initialize function result
Result := False;
// initialize buffer size to 0 to query needed buffer size
BufferSize := 0;
// first call is to determine whether there's a requested cookie, or if so,
// to retrieve the needed buffer size
if not InternetGetCookie(URL, Name, #0, BufferSize) then
begin
// the function failed as expected, so let's inspect the reason
case DLLGetLastError of
// if the reason for failure was the insufficient buffer, it means that
// there's a cookie matching the request and that we have just received
// the required buffer size
ERROR_INSUFFICIENT_BUFFER:
begin
// initialize buffer size by the previously returned size
SetLength(S, BufferSize div SizeOf(Char));
BufferSize := Length(S);
// and call the function again, now with the initialized buffer; this
// time it should succeed; if it is so, then...
if InternetGetCookie(URL, Name, S, BufferSize) then
begin
// everything went fine, so let's return success state and assign a
// retrieved value to the output parameter
Result := True;
Data := S;
end
else
// the second function call failed; that should not happen...
MsgBox('Cookie retrieval failed!' + #13#10 +
SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
// this code is returned when there's no cookie found
ERROR_NO_MORE_ITEMS:
begin
// no cookie matching the criteria was found; the return value of this
// function has already been initialized to False but it's upon you to
// react on this fact somehow, if needed
end;
else
// the first function call failed for unexpected reason
MsgBox('Cookie search failed!' + #13#10 +
SysErrorMessage(DLLGetLastError), mbError, MB_OK);
end;
end;
end;
procedure InitializeWizard;
var
CookieData: string;
begin
// try to create cookie
TryCreateCookie('http://example.com', 'MyCookie',
'TestData=1234; expires=Wed,31-Dec-2014 23:59:59 GMT');
// try to retrieve cookie
if TryRetrieveCookie('http://example.com', 'MyCookie', CookieData) then
MsgBox(CookieData, mbInformation, MB_OK);
end;
這是可能的。但你確定這是你想要的方式嗎? – TLama
@TLama:YES - 這是讓InnoSetup爲IE,Safari瀏覽器提供定製Cookie的唯一最佳方式,以便下次Safari瀏覽器可以幫助JavaScript檢測。從我們的網站,我們提供官方代碼簽名的插件,一旦從瀏覽器安裝插件,我需要檢測插件是否安裝。 (我嘗試過幾種方式,但都不是最好的方法) – YumYumYum