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...
Copyright and use this code
The source code on this page and other samples at
https://www.motobit.com/tips/
are a free code, you can use it as you want: copy it, modify it, use it in your products, ...
If you use this code, please:
1. Leave the author note in the source.
or
2. Link this sample from you page.
<A
Href="https://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>