Access ASP Application object from a remote script
ActiveX RegEdit.   ActiveX User account Manager   Pure-ASP Upload
Export MDB/DBF from ASP   Active LogFile   WebChecker   ActiveX/ASP Multi Dictionary object
 See 
 also 
 IISTracer, real-time IIS monitor and logging tool.
 Huge ASP file upload with progress bar. 



Do you like this article?
Please, rate it
and write review!
Rated:
by Aspin.com users
What do you think?
 Top messages
 4.5.2002 9:16:43 
 Send an email from ASP (WSH) using VBSscript, CDONTS and Outlook. (nbsp;ASP / ASP.NetWSHVBScriptEmail)
 22.3.2003 17:07:03 
 Work with binary files in VBSscript - read and write local and remote files (nbsp;WSHFile & data transferFunctionsVBScript)
 12.6.2003 9:14:29 
 Download multiple files in one http request (nbsp;File & data transferVBScript)
 10.2.2005 
 Add an email account to a windows 2003 pop3 service using script (nbsp;VBAEmail)

 Access ASP Application object from a remote script 

 Areas>Languages>VBScript
 Areas>WSH
 Areas>ASP / ASP.Net

1. Basic idea

      ASP has no interface to access Application data from other environment than VBS/JS in context of ASP page in the same application. But we can create a simple http interface and use the Application object from any other environment - wsh, VBA (Word, Excel), etc.

      The basic idea is to make simple ASP page, which will provide a text/http interface to ASP intrinsic objects (specially Application object). We can read/write variables in application dictionary using this ASP script. And we can specify name and value of application variable by a query string to read and write specified variable.

<%
'Application dictionary access script - Basic idea
'2002 Antonin Foller, http://www.motobit.com

'Get a name of a variable we want To access.
Dim vName: vName = Request.QueryString("vName")

'QueryString Action W = Write variable.
'QS Value contains value To store.
if Request.QueryString("Action") = "W" Then 'Write
  Application(vName) = Request.QueryString("Value")
End If

'Export application value 
if Len(vName)>0 Then Response.Write Application(vName)
%>
      This file lets you access any variable in application dictionary. Store this file in the space of ASP application you want to access from an external environment - in our case, file name is toapp.asp on 127.0.0.1 IIS server.

      Now we can create a VBS class, which will access application values from any vbs environment, including wsh (cscript/wscript), hta or chm. You can also use this class in another ASP page, so you can access one Application dictionary in one ASP application from any other ASP application, including ASP applications on remote servers.
      First of all basic idea of applications class - this class will access only Value property of application class.
'Application dictionary client class - Basic idea
'2002 Antonin Foller, http://www.motobit.com

Const AppURL = "http://127.0.0.1/toapp.asp"
Class ClassApplication
  Dim Http
  'Function To get HTML document As a string variable.
  'You can use WinHttp, XMLHttp, IE, WinInet API, ...
  Private Function GetURL(URL)
    'Create Http object
    If IsEmpty(Http) Then Set Http = CreateObject("WinHttp.WinHttpRequest.5")
    'Send request To URL
    Http.Open "GET", URL
    Http.Send
    'Get response data As a string
    GetURL = Http.ResponseText
  End Function

  'Gets data from remote Application object 
  Public Default Property Get Value(vName)
    Dim URL: URL = AppURL & "?vName=" & vName
    Value = GetURL(URL)
  End Property

  'Puts data To remote Application object 
  Public Property Let Value(vName, NewValue)
    Dim URL: URL = AppURL & "?Akce=Z&vName=" & vName & "&Value=" & NewValue
    GetURL URL
  End Property
End Class
      And real vbs code to access application variables from wsh.
Dim Application: Set Application = New ClassApplication

'Set application variable
Application("FirstVariable") = Now
'Echo this variable
Wscript.echo Application("FirstVariable")

'Set application variable
Application("NextVariable") = 7899
'Echo this variable
Wscript.echo Application("NextVariable")

'Set application variable
Application("ThirdVariable") = "you can write any text here"
'Echo this variable
Wscript.echo Application("ThirdVariable")

2. Variable type extensions

      Previous script can handle only string variables. You can use other variable type, but the type is converted to string every time. We must make some small extension, if we want to work with another types, for example long or date. We can create a protocol, which will send variable value and its type in two separate string variables (query string parameters Value and Type).
      We will add Type QS parameter, and also type to output text.

       There is also an extension to access Contents property of Application object - Action=C. Script will enumerate contents of Application object and write it with "~" & vbcrlf separator to output stream.

<%
'toapp-contents.asp 
'Application dictionary access script - type extensions
'2002 Antonin Foller, http://www.motobit.com

'Get a name of a variable we want To access.
Dim Value, vType, vName, Action
vName  = Request.QueryString("Name")
Action = Request.QueryString("Action")
If Len(Action+vName)=0 Then response.end

'QueryString Action W = Write variable.
'QS Value contains value To store.

If Action = "W" Then 'Write
  Value = Request.QueryString("Value")
  vType = clng(Request.QueryString("Type"))

  Application(vName) = GetOrigValue(Value, vType)
elseif Action = "N" Then 'Contents
  'Interface To count property of Contents
  Response.Write Application.Contents.Count
elseif Action = "C" Then 'Contents
  Dim n, Out:For Each n In Application.contents
    Out = Out &  "~" & vbCrLf & n
  Next
  Response.Write Mid(Out, 4)
else'Get variable
  'Export application value with type 
  Value = Application(vName)
  vType = varType(Value)

  'First 5 chars of OutPut is a type of variable.
  'value is converted To string And stored at pos 7
  Response.Write Right("0000" & vType & "-", 6) & _
    GetStrValue(Value, varType(Value))
end if'if Request.QueryString("Action") = "W" Then 


'Converts value To a string
Function GetStrValue(ByVal Value, vType)
  Select Case vType
    Case vbCurrency, vbDecimal, vbDouble, vbSingle
      GetStrValue = replace(cstr(Value), ",", ".")
    Case vbDate:
      'Convert date To its Double revbssentation.
      GetStrValue = replace(cstr(cdbl(Value)), ",", ".")
    Case Else GetStrValue = "" & Value
  End Select
End Function

'Restore value And its original type.
Function GetOrigValue(ByVal Value, vType)
  Select Case vType
  	Case vbBoolean:  GetOrigValue = cbool(Value)
  	Case vbByte:     GetOrigValue = cbyte(Value)
  	Case vbCurrency: GetOrigValue = ccur(Value)
  	Case vbDate:     GetOrigValue = cdate(Value)
  	Case vbDecimal:  GetOrigValue = CDec(Value)
  	Case vbDouble:   GetOrigValue = CDbl(Value)
  	Case vbError:    GetOrigValue = CVErr(Value)
  	Case vbInteger:  GetOrigValue = CInt(Value)
  	Case vbLong:     GetOrigValue = CLng(Value)
  	Case vbSingle:   GetOrigValue = CSng(Value)
  	Case vbVariant:  GetOrigValue = Cvar(Value)
  	Case vbString:   GetOrigValue = Value
  	Case vbNull:     GetOrigValue = Null
  	Case vbEmpty:    GetOrigValue = Empty
  	Case Else        GetOrigValue = Value
  End Select
End Function
%>
      Remote Application class will be also changed - it sends query string parameter 'Type' and receives modified document data with variable type. I added some extensions to handle problems with different regional settings on ASP machine and remote machine - dates are transferred as double, decimal separator is always dot (.) 
      Contents property will read string array of contents variables and split it to an array. 
'toapp-contents.vbs 
'2002 Antonin Foller, http://www.motobit.com
'****** Remote application class - wsh/vbs

Const AppURL = "http://localhost/toapp-contents.asp "
Class ClassApplication
  Dim Http

  'Function To get HTML document As a string variable.
  'You can use WinHttp, XMLHttp, IE, WinInet API, ...
  Private Function GetURL(URL)
    'Create Http object
    If IsEmpty(Http) Then Set Http = CreateObject("WinHttp.WinHttpRequest.5")
    'Send request To URL
    Http.Open "GET", URL
    Http.Send
    'Get response data As a string
    GetURL = Http.ResponseText
  End Function

  'Gets data from remote Application object 
  Public Default Property Get Value(vName)
    Dim URL: URL = AppURL & "?Name=" & vName
    Dim sValue 'String revbssentation of a value
    Dim vType 'Value And its type.
    sValue = GetURL(URL)

    'First 5 chars is the variable type
    vType = clng(Left(sValue, 5))

    'The value is from character 7
    Value = GetOrigValue(Mid(sValue, 7), vType)
  End Property

  'Puts data To remote Application object 
  Public Property Let Value(vName, NewValue)
    Dim URL, vType
    
    vType = varType(NewValue)
    URL = AppURL & "?Action=W&Name=" & vName & _
      "&Type=" & vType & "&Value=" & NewValue
    
    GetURL URL
  End Property

  'Get appliaction contents - list of variable names
  Public Property Get Contents
    Dim URL: URL = AppURL & "?Action=C" 

    Dim sContents'String revbssentation of contents
    sContents = GetURL(URL)
    Contents = split(sContents, "~" & vbCrLf)
  End Property

  'Get a number of Application variables
  Public Property Get Count
    Dim URL: URL = AppURL & "?Action=N" 
    Count = clng(GetURL(URL))
  End Property
End Class

'Restore value And its original type.
Function GetOrigValue(ByVal Value, vType)
  Select Case vType
    Case vbBoolean:  GetOrigValue = cbool(Value)
    Case vbByte:     GetOrigValue = cbyte(Value)
    Case vbCurrency: GetOrigValue = ccur(Value)
    Case vbDate:     GetOrigValue = cdate(Value)
    Case vbDecimal:  GetOrigValue = CDec(Value)
    Case vbDouble:   GetOrigValue = CDbl(Value)
    Case vbError:    GetOrigValue = CVErr(Value)
    Case vbInteger:  GetOrigValue = CInt(Value)
    Case vbLong:     GetOrigValue = CLng(Value)
    Case vbSingle:   GetOrigValue = CSng(Value)
    Case vbVariant:  GetOrigValue = Cvar(Value)
    Case vbString:   GetOrigValue = Value
    Case vbNull:     GetOrigValue = Null
    Case vbEmpty:    GetOrigValue = Empty
    Case Else        GetOrigValue = Value
  End Select
End Function

'****** Remote application class - wsh/vbs - end
      You can run next code to see if the script works well - script will transfer any number, date or string variable within its original type to server application variable and back to the wsh script.
'****** test code
Dim Application: Set Application = New ClassApplication
Set application variable
Application("FirstVariable") = Now
'Echo this variable
Wscript.echo Application("FirstVariable"), typename(Application("FirstVariable"))

'Set application variable
Application("NextVariable") = 7899
'Echo this variable
Wscript.echo Application("NextVariable"), typename(Application("NextVariable"))
      We can also enumerate contents of Application object. Remember that ASP Contents property returns IVariantDictionary, our Contents property returns an array. So you can enumerate values, but you cannot use count property to get count of values in collection (Application.Contents.Count). Use UBound(Application.Contents) + 1, or Application.Count.
Dim vn
For Each vn In application.Contents
  Wscript.echo "Var " & vn, _
    Application(vn), typename(Application(vn))
Next

3. Security warning

      Remember, that anyone who can access toapp.asp script can modify contents of application variables. Be sure that only reliable clients have access to this script - you can set ntfs permissions on the script, set IP address of clients which can access this script (127.0.0.0/255 for local access) or use other authentication methods to be sure that this script will be safe.

4. And What about arrays, objects?

      Do you interested in it? Will be continue... 
 

See also for 'Access ASP Application object from a remote script' article:


If you like this page, please include next link on your pages:
<A
 Href="http://www.motobit.com/tips/detpg_a-remote-asp-application-access/"
 Title="Basic idea and full source code
  to remote access contents of
  ASP Application object from a
  remote script. Lets you read,
  write and enumerate application dictionary
  from the remote script (WSH,
  remote ASP/IIS server) or application."
>Access ASP Application object from a remote script</A>

     IISTracer - IIS ISAPI real-time monitor IISTracer is a real-time monitoring tool for Microsoft IIS, which will show/log you what is happenning on IIS server right now. It let's you reveal problems with long-running scripts (.asp, .cgi, cfm...), hang-up states and low resource situations and lets you stop long-running requests (uploads/downloads).      ActiveX User account Manager - Set of simple objects for creating, deleting, and managing user accounts, groups, servers and domains in the Windows NT environment.
     Active log file - Hi-performance text file logging for ASP/VBS/VBA applications. Lets you create daily/weekly/monthly log files with variable number of logged values and extra timing and performance info.      ActiveX windows registry editor - Intuitive, easy to use COM interface to windows registry. Set of classes to read/enumerate/modify windows registry keys and values from ASP, VBS and T-SQL.
     ActiveX/ASP Multi Dictionary object - Free-threaded hi-speed dictionary algorithm with unique/nonunique keys (map/multimap). Connect to another dictionary object in the same process. Lock and Unlock methods to synchronize tasks (application scope). Share ASP Application/Session objects.      Export DBF/MDB from ASP - Conversion from recordset to MDB/DBF. Direct binary output of MDB or DBF files from ASP pages with one row of code.
     Pure-ASP upload - lets you upload files using Pure ASP VBS code (using multipart/form-data and input type=file).      ByteArray - Works with safearray binary data (VT_UI1 | VT_ARRAY) - save/restore binary data from disk, find, work with code pages, convert to string/hexstring(SQL).
     WebChecker - Checks http, https, ftp and gopher internet connections in regular intervals. Lets you monitor web site functionality (uptime). Enables restart or notification on problems.      HTTPLog ISAPI filter - Lets you log incomming/outgoing http header and document data to separate files. Monitor of IIS service input/output.

© 1996 – 2010 Antonin Foller, PSTRUH Software, e-mail help@pstruh.cz