Option Explicit
Private Const STD_ERROR_HANDLE As Long = -12&
Private Const STD_INPUT_HANDLE As Long = -10&
Private Const STD_OUTPUT_HANDLE As Long = -11&
Private Const GENERIC_WRITE As Long = &H40000000
Private Const FILE_SHARE_READ As Long = &H1&
Private Const OPEN_ALWAYS As Long = 4&
Private Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20&
Private Const FILE_END As Long = 2&
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, _
lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" _
(ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Public Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As Long, _
ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, _
ByVal dwMoveMethod As Long) As Long
Private Declare Function GetStdHandle Lib "kernel32" _
(ByVal nStdHandle As Long) As Long
Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" _
(ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Public Sub Main()
Dim STDOUT As Long 'handle to standard output
Dim strHTTP As String 'data buffer for HTTP header output
Dim strBuffer As String 'data buffer for writing to the log file
Dim lngBytesWritten As Long 'the number of bytes that were written
Dim retval As Long 'API return value
Dim FILEOUT As Long 'handle to the file to log to
'CGI Variables
Dim strQueryString As String
Dim strRemoteAddr As String
Dim strHTTPUserAgent As String
'fill the buffers with nothing
strQueryString = Space$(256)
strRemoteAddr = strQueryString
strHTTPUserAgent = strQueryString
'get and store the CGI variables needed
retval = GetEnvironmentVariable("QUERY_STRING", strQueryString, 256)
strQueryString = Left$(strQueryString, retval)
retval = GetEnvironmentVariable("REMOTE_ADDR", strRemoteAddr, 256)
strRemoteAddr = Left$(strRemoteAddr, retval)
retval = GetEnvironmentVariable("HTTP_USER_AGENT", strHTTPUserAgent, 256)
strHTTPUserAgent = Left$(strHTTPUserAgent, retval)
strHTTP = "Location: ./win2000.gif" & vbCrLf & vbCrLf
'start the console
'retval = AllocConsole 'UNCOMMENT FOR IDE DEBUGGING
'get the handle to standard output
STDOUT = GetStdHandle(STD_OUTPUT_HANDLE)
'write the string to STDOUT, just like writing to a file
retval = WriteFile(STDOUT, ByVal strHTTP, Len(strHTTP), lngBytesWritten, ByVal 0&)
'close the handle to standard out
retval = CloseHandle(STDOUT)
'close the console
'retval = FreeConsole 'UNCOMMENT FOR IDE DEBUGGING
'open the file for writing
FILEOUT = CreateFile("C:\Inetpub\wwwroot\Test\image.log", GENERIC_WRITE, FILE_SHARE_READ, _
ByVal 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)
'if error
If FILEOUT = -1 Then Exit Sub
'fill the buffer
strBuffer = Now & ";" & strQueryString & ";" & strRemoteAddr & ";" & strHTTPUserAgent & vbCrLf
'move the file pointer to EOF
retval = SetFilePointer(FILEOUT, 0, 0, FILE_END)
'write the buffer to the file
retval = WriteFile(FILEOUT, ByVal strBuffer, Len(strBuffer), lngBytesWritten, ByVal 0&)
'close the handle to the file
retval = CloseHandle(FILEOUT)
End Sub