Monday, May 31, 2010

OLE/COM & Connect:Direct

Day 11

Sometimes we want to be able to automate tasks with regard to Connect:Direct.  On Windows we could do this using batch files (.bat or .cmd), or with an application using the Connect:Direct Windows SDK.  My preferred way is to use some form of VBScript and the OLE/COM interface that the Connect:Direct Windows SDK provides.

That sounds more complicated that it really is.  I'll show an example Windows Script Host (WSH) script that uses the OLE/COM interface to connect to the local node and send/receive files  with a remote node.

Start by looking at the example below from the comment "Processing starts here" where you will see the creation of an object of type "CD.Node".  If my memory serves me well this is not well documented in the Connect:Direct Windows SDK manual.

Once we have an object that represents our local node in my case called "NICKE" we can make a connection with our credentials.  In this example the user name and password are empty strings and it makes the connection based on how the "CD Client Connection Utility" is setup.  This utility allows you to define several user credentials and which one is to be used as the default when none are given as in my example below.

The rest of the script then goes on to perform a transfer using the "CDSend" function to a remote node called "CD.OTHER", check for an error and then perform a receive of a file and then another check for an error.  Both "CDSend" & "CDRecv" make use of a generic function "CDTransfer" to perform a file transfer with the direction of the transfer passed as a parameter which is then used to generate a dynamic Connect:Direct process which is the submitted using the "Node.Submit" function and then the statistics for the process just submitted and executed are analysed and the highest condition code returned to the caller of the "CDTransfer" function.

At the end of the script the connection with the local node is disconnected and the "Node" object de-allocated.

The script below can either be run by double-clicking directly on the ".vbs" script in the file explorer or by giving the name of the script as a parameter to the "CSCRIPT.EXE" program as follows:

C:\> cscript cdvbsdk.vbs

In the next post I will an alternative method of driving Connect:Direct from VBScript.


' Windows Script Host Sample Connect:Direct Script. 
' 
' Includes ability to push and pull files to/from the other Node. 
 
Dim Node        ' Represents the Connect Direct Node we are using. 
Dim procs       ' A collection of process records. 
Dim proc        ' An individual process record. 
Dim stats       ' A collection of statistic records. 
Dim stat        ' An individual statistic record. 
 
' Connect:Direct condition codes values 
 
warningConditionCode = 4 
errorConditionCode   = 8 
 
 
 
 
Function CDTransfer (fromFile, toFile, otherNode, direc) 
 
        If direct = "PUSH" then 
                localOrRemote = "LOCAL" 
                remoteOrLocal = "REMOTE" 
        else 
                localOrRemote = "REMOTE" 
                remoteOrLocal = "LOCAL" 
        End If 
 
        ' The process text to be submitted. 
 
        txtProcess = "TRANSFER PROCESS"                 + vbCrLf _ 
                + "     MAXDELAY=UNLIMITED"             + vbCrLf _ 
                + "     REMOTE=" & otherNode            + vbCrLf _ 
                + "     HOLD = NO"                      + vbCrLf _ 
                + "STEP01 COPY  FROM ("                 + vbCrLf _ 
                + "     FILE=" & fromFile               + vbCrLf _ 
                + "     " & localOrRemote               + vbCrLf _ 
                + "     TO ("                           + vbCrLf _ 
                + "     " & remoteOrLocal               + vbCrLf _ 
                + "     FILE=" & toFile                 + vbCrLf _ 
                + "     DISP=(RPL))"                    + vbCrLf _ 
                + "PEND"                                + vbCrLf
 
 
        set proc = Node.Submit(txtProcess)             ' Submit the process 
 
        ' Get the statistics regarding the process that has now finished. 
 
        set stats = Node.SelectStats("SELECT STATISTICS PNUMBER=" & proc.ProcessNumber) 
 
        ' If any step in the process has a condition code greater than "warning" we have a problem. 
 
        highestConditionCode =0 
 
        For Each stat in stats
 
                if stat.ConditionCode > highestConditionCode then 
                        highestConditionCode = stat.ConditionCode
                End If 
 
        Next 
 
        If highestConditionCode > warningConditionCode then 
 
                CDTRansfer = highestConditionCode
 
        End If 
 
End Function 
 
 
 
 
Function CDSend ( fromfile, toFile, otherNode ) 
        CDSend = CDTransfer ( fromfile, toFile, otherNode, "PUSH" ) 
End Function 
 
Function CDRecv ( fromfile, toFile, otherNode ) 
        CDRecv = CDTransfer ( fromfile, toFile, otherNode, "PULL" ) 
End Function 
 
 
' ------------------------------------------------------------------------------------------ 
' 
' Processing starts here 
' 
Set Node = CreateObject("CD.Node")              ' Make the OLE/COM connection to Connect:Direct 
 
Node.Connect "NICKE","",""      ' Sign on 
 
exitcode = 0 
 
rtc = CDSend ( "c:\temp\input.txt", "c:\temp\output.txt", "CD.OTHER") 
 
if rtc > warningConditionCode then 
        wscript.echo "Send Failed" 
        exitcode = exitcode + rtc
End If 
 
rtc = CDRecv ( "c:\temp\input.txt", "c:\temp\recv.txt", "CD.OTHER") 
 
if rtc > warningConditionCode then 
        wscript.echo "Recv Failed" 
        exitcode = exitcode + rtc
End If 
 
' You need to disconnect from the Node or you will leave cscript or wscript processes hanging around 
Node.Disconnect 
Set Node=Nothing 
Wscript.Quit(exitcode) 

5 comments:

Unknown said...

What is the prerequisite to use createobject (CD.NODE) ? I get error ActiveX component can't create object:CD. NODE. I'm running the above script in server where CONNECT Direct is installed..I can make connection using direct.exe but unable to use create object method. Could you please advise?

Thank you

Nick Evans said...

You need to have the Connect:Direct for Windows SDK installed to use this example. If you have it installed you will find CDAuto.dll in a directory like "C:\Program Files\Sterling Commerce\Connect Direct v4.6.00\SDK\CDAuto.dll". In recent versions of Connect:Direct the SDK install files come with the Connect:Direct Server & Requester install media, but you install it manually. See the following PDF:
http://public.dhe.ibm.com/software/commerce/doc/mft/cdsdk/46/CDWindowsSDKGuide.pdf

Unknown said...

Thank you very much for your help. I registered DLL and then able to submit the process and get statistics using above script. I got an error at "Node.Disconnect" saying 'not a defined method'. I tried to use Node.Quit/Exit/Close/DisconnectAll but did not help. Please advice.

Also, for "Node.Connect" I am unable to connect C:D without specifying username and password though I set up a user with CD Client Connection Utility. I added a user using this utility with options 'Default user' and 'Remember password' enabled. But the connection fails with error node rejected for the user "". But, I can use direct.exe and connect to C:D without entering password by enabling 'remember password' option. How to make this work for Node.Connect in the script? Requirement is that its allowed to provide user id in the script but password must set up in C:D so that no need specify it exclusively. I am using 4.6 version. Kindly advice.

Thank you,
Sreenivas

pusni.com said...

Hi Nick,

I tried running above code in my system, I am getting an "Microsoft VBScript runtime error: Ac
tiveX component can't create object: 'CD.NODE'"

Please advice on this..

Thanks,
Guna

Nick Evans said...

You will not be able to create object "CD.NODE" unless you have the Connect:Direct for Windows SDK installed.