1

Basically IE remember for site www.stackoverflow.com using JavaScript cookies but is there anyway manually to create a same cookie within InnoSetup on behalf of stackoverflow.com?

Javascript cookie:

function setCookie(cname,cvalue,exdays) {
  var d = new Date();
  d.setTime(d.getTime()+(exdays*24*60*60*1000));
  var expires = "expires="+d.toGMTString();
  document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
  var name = cname + "=";
  var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++)  {
    var c = ca[i].trim();
    if (c.indexOf(name)==0) return c.substring(name.length,c.length);
  }
  return "";
}

function checkCookie(user) {
  var user=getCookie("username");
  if (user!="") {
    alert("Welcome again " + user);
  } else  {
    user='bandwidth - set to off for example';
    setCookie("username",user,30);  
  }
}

NOTE: because there is no way to detect from IE if my plugin is installed or not. I came to a mind set that i must have to deal with cookies. But instead of IE my plugin has to create that cookie for the first time installation.

EDIT: Reference

http://msdn.microsoft.com/en-us/library/windows/desktop/aa385107%28v=vs.85%29.aspx

  • 1
    It is possible. But are you sure it is the way you want to go ? – TLama Jul 10 '14 at 12:12
  • @TLama: YES - this is the only best way for to go that InnoSetup puts a customized cookie for IE, Safari browsers, so that next time the IE, Safari can help JavaScript to detect. From our website we provide official code signed plugin, once the plugin is installed from the browser i need to detect if the plugin is installed or not. (i have tried several way but it was all not best approach to go for) –  Jul 10 '14 at 12:56

1 Answers1

0

To create a cookie associated with the specified URL you can use the InternetSetCookie function. For retrieving a cookie for the specified URL you can use InternetGetCookie function. Here's their translation with an example showing how to create and read a cookie (real code implementation talking about error messages, or wrapper functions I'd keep upon you; take this code as an example, showing how to use those API functions):

[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
  • 75,147
  • 17
  • 214
  • 392
  • 1
    +1 You are Genius Guru. Thank you very much its an excellent work –  Jul 10 '14 at 14:12
  • 1
    The problem with this answer is that it doesn't work properly in modern IE versions. See Q10 here: http://blogs.msdn.com/b/ieinternals/archive/2009/08/20/wininet-ie-cookie-internals-faq.aspx – EricLaw Jul 12 '14 at 19:36
  • It works. Using innoSetup, you can also after install do 1) Launch IE to a static URL which execute JavaScript and create the cookie 2) Launch backend all the browsers and create cookies from web links –  Jul 12 '14 at 22:36
  • 1
    Saying "it works" is misleading for the reasons I describe. Launching IE to a static URL will generally work. "Launching backend all the browsers" is an idea with many problems. – EricLaw Jul 13 '14 at 12:23