Results 1 to 4 of 4

Thread: cgi in Visual Basic

  1. #1
    ddt
    Guest

    cgi in Visual Basic

    exactly how do you code cgi in visual basic without any files comeing up for download

  2. #2
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Don't use the VB object model, and configure your server to run executables (*.exe) as cgi, then compile something like this into *.exe:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const STD_ERROR_HANDLE As Long = -12&
    4. Private Const STD_INPUT_HANDLE As Long = -10&
    5. Private Const STD_OUTPUT_HANDLE As Long = -11&
    6.  
    7. Private Const GENERIC_WRITE As Long = &H40000000
    8. Private Const FILE_SHARE_READ As Long = &H1&
    9. Private Const OPEN_ALWAYS As Long = 4&
    10. Private Const FILE_ATTRIBUTE_ARCHIVE As Long = &H20&
    11.  
    12. Private Const FILE_END As Long = 2&
    13.  
    14. Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
    15.  
    16. Private Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, _
    17.     lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, _
    18.     lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
    19.    
    20. Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" _
    21.     (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
    22.     ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
    23.     ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
    24.     ByVal hTemplateFile As Long) As Long
    25.    
    26. Public Declare Function SetFilePointer Lib "kernel32.dll" (ByVal hFile As Long, _
    27.     ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, _
    28.     ByVal dwMoveMethod As Long) As Long
    29.  
    30. Private Declare Function GetStdHandle Lib "kernel32" _
    31.     (ByVal nStdHandle As Long) As Long
    32.    
    33. Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" _
    34.     (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
    35.  
    36. Private Declare Function AllocConsole Lib "kernel32" () As Long
    37. Private Declare Function FreeConsole Lib "kernel32" () As Long
    38.  
    39. Public Sub Main()
    40.     Dim STDOUT As Long 'handle to standard output
    41.     Dim strHTTP As String 'data buffer for HTTP header output
    42.     Dim strBuffer As String 'data buffer for writing to the log file
    43.     Dim lngBytesWritten As Long 'the number of bytes that were written
    44.     Dim retval As Long 'API return value
    45.     Dim FILEOUT As Long 'handle to the file to log to
    46.     'CGI Variables
    47.     Dim strQueryString As String
    48.     Dim strRemoteAddr As String
    49.     Dim strHTTPUserAgent As String
    50.    
    51.     'fill the buffers with nothing
    52.     strQueryString = Space$(256)
    53.     strRemoteAddr = strQueryString
    54.     strHTTPUserAgent = strQueryString
    55.    
    56.     'get and store the CGI variables needed
    57.     retval = GetEnvironmentVariable("QUERY_STRING", strQueryString, 256)
    58.     strQueryString = Left$(strQueryString, retval)
    59.     retval = GetEnvironmentVariable("REMOTE_ADDR", strRemoteAddr, 256)
    60.     strRemoteAddr = Left$(strRemoteAddr, retval)
    61.     retval = GetEnvironmentVariable("HTTP_USER_AGENT", strHTTPUserAgent, 256)
    62.     strHTTPUserAgent = Left$(strHTTPUserAgent, retval)
    63.    
    64.     strHTTP = "Location: ./win2000.gif" & vbCrLf & vbCrLf
    65.    
    66.     'start the console
    67.     'retval = AllocConsole 'UNCOMMENT FOR IDE DEBUGGING
    68.    
    69.     'get the handle to standard output
    70.     STDOUT = GetStdHandle(STD_OUTPUT_HANDLE)
    71.    
    72.     'write the string to STDOUT, just like writing to a file
    73.     retval = WriteFile(STDOUT, ByVal strHTTP, Len(strHTTP), lngBytesWritten, ByVal 0&)
    74.  
    75.     'close the handle to standard out
    76.     retval = CloseHandle(STDOUT)
    77.    
    78.     'close the console
    79.     'retval = FreeConsole 'UNCOMMENT FOR IDE DEBUGGING
    80.    
    81.     'open the file for writing
    82.     FILEOUT = CreateFile("C:\Inetpub\wwwroot\Test\image.log", GENERIC_WRITE, FILE_SHARE_READ, _
    83.         ByVal 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_ARCHIVE, 0)
    84.    
    85.     'if error
    86.     If FILEOUT = -1 Then Exit Sub
    87.        
    88.     'fill the buffer
    89.     strBuffer = Now & ";" & strQueryString & ";" & strRemoteAddr & ";" & strHTTPUserAgent & vbCrLf
    90.        
    91.     'move the file pointer to EOF
    92.     retval = SetFilePointer(FILEOUT, 0, 0, FILE_END)
    93.    
    94.     'write the buffer to the file
    95.     retval = WriteFile(FILEOUT, ByVal strBuffer, Len(strBuffer), lngBytesWritten, ByVal 0&)
    96.        
    97.     'close the handle to the file
    98.     retval = CloseHandle(FILEOUT)
    99. End Sub
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  3. #3
    Frenzied Member
    Join Date
    Feb 2001
    Posts
    1,140
    Damn, Josh, you rock.

    But why VB CGIs? Are you just looking for the slowest version of slow?
    Travis, Kung Foo Journeyman
    As always, RTFM.

    WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
    Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
    YBMS, but Mozilla doesn't.

  4. #4
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Heh, thanks. I wrote it simply to prove that you can use VB for CGI.
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width