Visual Basic Programming Code Examples Visual Basic > Internet Web Mail Stuff Code Examples Creating an internet cookie Creating an internet cookie The following code demonstates how to create internet cookies. Please read the "Notes" for an explaination of the current limitations of creating cookies using the wininet dll. Option Explicit Private Declare Function InternetSetCookie Lib "wininet.dll" Alias "InternetSetCookieA" (ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As String) As Boolean Private Declare Function InternetGetCookie Lib "wininet.dll" Alias "InternetGetCookieA" (ByVal lpszUrlName As String, ByVal lpszCookieName As String, ByVal lpszCookieData As String, lpdwSize As Long) As Boolean 'Purpose : Creates an internet cookie 'Inputs : sURL The URL to create the cookie for. ' sCookieName The name of the cookie. ' sCookieData The data to store in the cookie 'Outputs : Returns True if succeeded in creating the cookie. 'Notes : Cookies created by this function without an expiration date are stored in memory and are ' available only in the same process that created them. Cookies that include an expiration ' date are stored in the windows\cookies directory. ' Creating a new cookie might cause a dialog box to appear on the screen if the appropriate ' registry value, AllowCookies, is set. Function InternetCookieCreate(sURL As String, sCookieName As String, sCookieData As String) As Boolean On Error GoTo ErrFailed InternetCookieCreate = InternetSetCookie(sURL, sCookieName, sCookieData) Exit Function ErrFailed: Debug.Print Err.Description InternetCookieCreate = False End Function 'Purpose : Returns ALL the data from an internet cookie 'Inputs : sURL The URL to create the cookie for. ' sCookieName Not supported in the current version of the wininet dll ' sCookieData The data to store in the cookie 'Outputs : Returns True if succeeded in creating the cookie. 'Notes : Function InternetCookieGet(sURL As String, sCookieName As String, sCookieData As String) As String Const clMaxCookieLen = 5012 'Arbitary limit Dim sResult As String * clMaxCookieLen Dim bRet As Boolean, lLen As Long On Error GoTo ErrFailed lLen = clMaxCookieLen bRet = InternetGetCookie(sURL, sCookieName, sResult, lLen) If bRet Then InternetCookieGet = Left$(sResult, lLen) End If Exit Function ErrFailed: Debug.Print Err.Description InternetCookieGet = "" End Function 'Demonstration routine Sub Test() InternetCookieCreate "http://Example", "Red", "Test Cookie" Debug.Print InternetCookieGet("http://Example", "", "Test Cookie") End Sub