Register event in comobject

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
oliverlipkau

Register event in comobject

12 Jun 2015, 15:29

Hey there.

I am writing a lib for winscpnet.DLL
However I fail to figure out how to register for the progress event.

bits: https://dl.dropboxusercontent.com/u/115921/WinSCP.zip
This is the line I need help with;
t.Session.FileTransferProgress(ShowProgress())

The documentation is here:
;http://winscp.net/eng/docs/library_sess ... erprogress

I appreciate any help and tips.
Thanks

The complete code:

Code: Select all

;~ -----------------------------------------------------------------------------
;~ Name:               WinSCP Class
;~ -----------------------------------------------------------------------------
;~ Version:            2.0
;~ Date:               2015-06-012
;~ Author:             Lipkau, Oliver <[email protected]>
;~                     https://githubom/lipkau
;~                     http://oliver.lipkau.net/blog
;~ -----------------------------------------------------------------------------
;~ TODO:
;~ 		- Try/Catch if dll is not registered?
;~ 		- Allow dll to be outside A_ScriptDir?
;~ 		- better documentation
;~ 		- more methods
;~ 		- cleanup



WinSCP := { FtpMode:                {Passive:0
						            ,Active:1}
		   ,FtpSecure:              {None:0
					                ,Implicit:1
					                ,ExplicitTls:2  ;for older versions
					                ,ExplicitSsl:3}
		   ,FtpProtocol:            {Sftp:0
		                            ,Scp:1
						            ,Ftp:2}
           ,TransferMode:           {Binary:0
		                            ,Ascii:1
									,Automatic:2}
		   ,SynchronizationMode:    {Local:0
		                            ,Remote:1
									,Both:2}
		   ,SynchronizationCriteria:{None:0
		                            ,Time:1
									,Size:2
									,Either:3}}
									
;~ t.dispose()
t := new FTP()
t.Hostname		:= "lipkau.net"
t.Protocol 		:= WinSCP.FtpProtocol.Ftp
t.Secure 		:= WinSCP.FtpSecure.ExplicitSsl
t.User			:= "t"
t.Password		:= "1@2"
t.Fingerprint   := "c4:04:6a:91:c5:ff:43:cf:e4:60:4a:b5:ba:c0:71:1b:e6:02:e4:91"
t.Session.SessionLogPath := "c:\temp\l.log"

;~ msgbox % t.Secure " \n pr " t.Protocol " \n ps " t.Password " \n u " t.User " \n h " t.Hostname " \n m " t.Mode

try
{
	t.OpenConnection()
	
	;TODO!!!
	;http://winscp.net/eng/docs/library_session_filetransferprogress
	t.Session.FileTransferProgress(ShowProgress())

	FileCollection := t.ListDirectory("/")
	for file in FileCollection.Files {
		if (file.Name != "." && file.Name != "..")
			msgbox % "Name: " file.Name "`nPermission: " file.FilePermissions.Octal "`nIsDir: " file.IsDirectory "`nFileType: " file.FileType "`nGroup: " file.Group "`nLastWriteTime: " file.LastWriteTime "`nLength: " file.Length "`nLength32: " file.Length32 "`nOwner: " file.Owner
	}
	
	Progress, b w200, My SubText, %name%, My Title
	t.PutFiles(t.EscapeFileMask("C:\temp\DEF-CLK-0041_German_Calc - Copy.xls"), "/")
	Progress, Off


} catch e
	msgbox % "failed: " e.Message

;~ t.FilePermissions.Octal := 746
;~ msgbox % t.FilePermissions.Octal

ShowProgress(e)
{
	name := e.FileProgress
Progress, % e.FileProgress


}
ProgressHandler(WorkerThread, Progress)
{
	this.tmpNotificationWindow.Progress := Round(Progress / WorkerThread.CurrentFile.FileSize * 100)
	this.tmpNotificationWindow.Text := WorkerThread.CurrentFile.RemoteName " - " FormatFileSize(Progress) " / " FormatFileSize(WorkerThread.CurrentFile.FileSize)
}




class FTP ;extends BaseClassName
{
	static Hostname                := ""
	static Port                    := ""
	static Secure                  := ""
	static Protocol                := ""
	static User                    := ""
	static Password                := ""
	static Mode                    := ""
	static TransferMode            := ""
	static SynchronizationMode     := ""
	static SynchronizationCriteria := ""
	static Fingerprint             := ""

	;http://winscp.net/eng/docs/library_session
	static Session                 := ComObjCreate("WinSCP.Session")
	static SessionOptions          := ComObjCreate("WinSCP.SessionOptions")
	static TransferOptions         := ComObjCreate("WinSCP.TransferOptions")
	;http://winscp.net/eng/docs/library_filepermissions
	static FilePermissions         := ComObjCreate("WinSCP.FilePermissions")
	
	/*
	Description
		Constructor - sets default values
	*/
	__New()
	{
		global WinSCP ; WinSCP Enums
		
		;~ Set defaults
		this.Port                    := 21
		this.Secure                  := WinSCP.FtpSecure.None
		this.Protocol                := WinSCP.FtpProtocol.Ftp
		this.Mode                    := WinSCP.FtpMode.Passive
		this.TransferMode            := WinSCP.TransferMode.Binary
		this.SynchronizationMode     := WinSCP.SynchronizationMode.Local
	    this.SynchronizationCriteria := WinSCP.SynchronizationCriteria.Time
		this.Fingerprint             := false
	}
	
	/*
	Description
		Destructor
	*/
	__Delete()
	{
		this.Dispose()
	}
	
	/*
	Descitipion
		Open connection to server
		
	Input
		srv        : [string] Server DNS or IP
		uName      : [string] User Name
		pWord      : [string] Password
	*/
	OpenConnection(srv="",uName="",pWord="")
	{
		global WinSCP ; WinSCP Enums
		
		this.SessionOptions.HostName := (srv)   ? srv   : this.Hostname
		this.SessionOptions.UserName := (uName) ? uName : this.User
		this.SessionOptions.Password := (pWord) ? pWord : this.Password
		
		;~ FTP Mode
		this.SessionOptions.FtpMode := this.Mode
		
		;~ FTP Protocol
		this.SessionOptions.Protocol := this.Protocol
		
		;~ FTP Security
		this.SessionOptions.FtpSecure := this.Secure
		
		;~ Fingerprint
		IsEncrypted := this.Secure
		Encryptions := WinSCP.FtpSecure.Implicit "," WinSCP.FtpSecure.ExplicitTls "," WinSCP.FtpSecure.ExplicitSsl
		if IsEncrypted in %Encryptions%
		{
			if (StrLen(this.Fingerprint) > 1)
			{
				if (this.Protocol==WinSCP.FtpProtocol.Scp)
					this.SessionOptions.SshHostKeyFingerprint := this.Fingerprint
				else
					this.SessionOptions.TlsHostCertificateFingerprint := this.Fingerprint
			} else {
				if (this.Protocol==WinSCP.FtpProtocol.Scp)
					this.SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey := true
				else
					this.SessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate := true
			}
		}
		
		;~ MsgBox % "s " . this.SessionOptions.FtpSecure . ". p " . this.SessionOptions.Protocol . ". m " . this.SessionOptions.FtpMode . ". u " . this.SessionOptions.UserName . ". p " this.SessionOptions.Password
		;~ MsgBox % this.SessionOptions.SshHostKeyFingerprint " - " this.SessionOptions.TlsHostCertificateFingerprint " - " this.SessionOptions.GiveUpSecurityAndAcceptAnyTlsHostCertificate " - " this.SessionOptions.GiveUpSecurityAndAcceptAnySshHostKey
		
		this.Session.Open(this.SessionOptions)
	}
	
	/*
	Descitipion
		Closes session.
		New session can be opened using Session.Open using the same instance of Session.
	*/
	CloseConnection()
	{
		this.Session.Close()
	}
	
	/*
	Descitipion
		If session was opened, closes it, terminates underlying WinSCP process, deletes XML log file and disposes object.
	*/
	Dispose()
	{
		this.Session.Dispose()
	}
	
	/*
	Description:
		Lists the contents of specified remote directory.
		http://winscp.net/eng/docs/library_session_listdirectory
	
	Input:
		remotePath : [string] Full path to remote directory to be read.
	
	Output:
		[object] RemoteDirectoryInfo
	*/
	ListDirectory(remotePath)
	{
		return this.Session.ListDirectory(remotePath)
	}
	
	/*
	Description:
		Creates remote directory.
		http://winscp.net/eng/docs/library_session_createdirectory
	
	Input:
		remotePath : [string] Full path to remote directory to create.
	
	Output:
		[void]
	*/  
	CreateDirectory(remotePath)
	{
		return this.Session.CreateDirectory(remotePath)
	}
	
	/*
	Description:
		Checks for existence of remote file.
		http://winscp.net/eng/docs/library_session_fileexists
	
	Input:
		remotePath : [string] Full path to remote file. Note that you cannot use wildcards here.
	
	Output:
		[bool] true if file exists, false otherwise.
	*/  
	FileExists(remotePath)
	{
		return this.Session.FileExists(remotePath)
	}
	
	/*
	Description:
		Retrieves information about remote file.
		http://winscp.net/eng/docs/library_session_getfileinfo
	
	Input:
		remotePath : [string] Full path to remote file.
	
	Output:
		[RemoteFileInfo]
		http://winscp.net/eng/docs/library_remotefileinfo
	*/
	GetFileInfo(remotePath)
	{
		return this.Session.GetFileInfo(remotePath)
	}
	
	/*
	Description:
		Downloads one or more files from remote directory to local directory.
		http://winscp.net/eng/docs/library_session_getfiles
		
		This Method supports Wildcards
		http://winscp.net/eng/docs/library_wildcard
	
	Input:
		remotePath : [string]          Full path to remote directory followed by slash and wildcard to
		                               select files or subdirectories to download. When wildcard is
							           omitted (path ends with slash), all files and subdirectories in
							           the remote directory are downloaded.
		localPath  : [string]          Full path to download the file to. When downloading multiple
		                               files, the filename in the path should be replaced with operation
							           mask or omitted (path ends with backslash).
		remove     : [bool]            When set to true, deletes source remote file(s) after transfer.
		                               Defaults to false.
		options    : [TransferOptions] Transfer options. Defaults to null, what is equivalent to new
		                               TransferOptions().
	
	Output:
		[TransferOperationResult]
		See also Capturing results of operations.
	*/
	GetFiles(remotePath, localPath, remove=false, options="")
	{
		global WinSCP ; WinSCP Enums
		
		;~ Check
		;remove
		if remove not in 0,1
			throw "Invalid value for remove"
		
		;TransferOptions
		if (options && (ComObjType(options,"Name")=="_TransferOptions"))
			throw "Invalid TransferOptions"
		
		return this.Session.GetFiles(remotePath, localPath, remove, options)
	}
	
	/*
	Description:
		Uploads one or more files from local directory to remote directory.
		http://winscp.net/eng/docs/library_session_putfiles
		
		This Method supports Wildcards
		http://winscp.net/eng/docs/library_wildcard
	
	Input:
		localPath  : [string]          Full path to local file or directory to upload. Filename in the
		                               path can be replaced with Windows wildcard1) to select multiple
					 		           files. When file name is omitted (path ends with backslash), all
							           files and subdirectories in the local directory are uploaded.
		remotePath : [string]          Full path to upload the file to. When uploading multiple files,
		                               the filename in the path should be replaced with operation mask
							           or omitted (path ends with slash).
		remove     : [bool]            When set to true, deletes source local file(s) after transfer.
		                               Defaults to false.
		options    : [TransferOptions] Transfer options. Defaults to null, what is equivalent to new
		                               TransferOptions().
	
	Output:
		[TransferOperationResult]
		See also Capturing results of operations.
	*/
	PutFiles(localPath, remotePath, remove:=false, options:="")
	{
		global WinSCP ; WinSCP Enums
		
		;~ Checks
		if remove not in 0,1
			throw "Invalid value for remove"
		
		;TransferOptions
		if (options && (ComObjType(options,"Name")=="_TransferOptions"))
			throw "Invalid TransferOptions"
		
		return this.Session.PutFiles(localPath, remotePath, remove, options)
	}
	
	/*
	Description:
		Moves remote file to another remote directory and/or renames remote file.
		http://winscp.net/eng/docs/library_session_movefile
	
	Input:
		sourcePath : [string] Full path to remote file to move/rename.
		targetPath : [string] Full path to new location/name to move/rename the file to.
	
	Output:
		[void]
	*/
	MoveFile(sourcePath, targetPath)
	{
		return this.Session.MoveFile(sourcePath, targetPath)
	}
	
	/*
	Description:
		Removes one or more remote files.
		http://winscp.net/eng/docs/library_session_removefiles
		
		This Method supports Wildcards
		http://winscp.net/eng/docs/library_wildcard
	
	Input:
		remotePath : [string] Full path to remote directory followed by slash and
		                      wildcard to select files or subdirectories to remove.
	
	Output:
		[RemovalOperationResult]
		See also Capturing results of operations.
	*/
	RemoveFiles(remotePath)
	{
		return this.Session.RemoveFiles(remotePath)
	}
	
	/*
	Description:
		Synchronizes directories.
		http://winscp.net/eng/docs/library_session_synchronizedirectories
	
	Input:
		[mandatory] [enum]   Synchronization mode. Possible values are SynchronizationMode.Local, SynchronizationMode.Remote and
		                     SynchronizationMode.Both.
		[mandatory] [string] Full path to local directory.
		[mandatory] [string] Full path to remote directory.
		[mandatory] [bool]   When set to true, deletes obsolete files. Cannot be used for SynchronizationMode.Both.
		[optional]  [bool]   When set to true, synchronizes in mirror mode (synchronizes also older files). Cannot be used for
		                     SynchronizationMode.Both. Defaults to false.
		[optional]  [enum]   Comparison criteria. Possible values are SynchronizationCriteria.None, SynchronizationCriteria.Time
		                     (default), SynchronizationCriteria.Size and SynchronizationCriteria.Either. For
							 SynchronizationMode.Both SynchronizationCriteria.Time can be used only.
		[optional]  [string] Transfer options. Defaults to null, what is equivalent to new TransferOptions().
		
	Output:
		[SynchronizationResult]
		See also Capturing results of operations.
	*/
	SynchronizeDirectories(mode, localPath, remotePath, removeFiles, mirror=false, criteria=1, options="" )
	{
		global WinSCP ; WinSCP Enums
		
		;~ Checks
		;Mode
		if FilePermissions not in % this.StringJoin(WinSCP.SynchronizationMode,",")
			throw "Invalid Mode"
		
		;removeFiles
		if removeFiles not in 0,1
			throw "Invalid removeFiles"
		if (removeFiles && (removeFiles==WinSCP.SynchronizationMode.Both))
			throw "Deletion of obsolete files cannot be used for SynchronizationMode.Both"
		
		;mirror
		if mirror not in 0,1
			throw "Invalid mirror"
		if (mirror && (removeFiles==WinSCP.SynchronizationMode.Both))
			throw "Synchronization in mirror mode (synchronizes also older files) cannot be used for SynchronizationMode.Both"
		
		;TransferOptions
		if (options && (ComObjType(options,"Name")=="_TransferOptions"))
			throw "Invalid TransferOptions"
		
		this.SetTransferOptions(WinSCP.TransferMode.Binary)
		options := this.TransferOptions
		
		return this.Session.SynchronizeDirectories(mode, localPath, remotePath, removeFiles, mirror, criteria, options)
	}
	
	;~ -----Aux Methods
	/*
	Description
		Set TransferOptions
		http://winscp.net/eng/docs/library_transferoptions
		
	Input
		FileMask          : [string]                FileMask
		FilePermissions   : [FilePermissions]       Permissions to applied to a remote file (used for
		                                            uploads only). Use default null to keep default permissions.
		PreserveTimestamp : [bool]                  Preserve timestamp (set last write time of destination file
													to that of source file). Defaults to true. 
                                                    When used with Session.SynchronizeDirectories, timestamp is
												    always preserved, disregarding property value, unless criteria
													parameter is SynchronizationCriteria.None or
													SynchronizationCriteria.Size.
		ResumeSupport     : [TransferResumeSupport] Configures automatic resume/transfer to temporary filename.
		                                            Read-only (set properties of returned TransferResumeSupport
													instance).
		SpeedLimit        : [int]                   Limit transfer speed (in KB/s).
		TransferMode      : [TransferMode]          Transfer mode. Possible values are TransferMode.Binary
		                                            (default), TransferMode.Ascii and TransferMode.Automatic
													(based on file extension).
	*/
	SetTransferOptions(FileMask="",FilePermissions="",PreserveTimestamp=true,ResumeSupport="",SpeedLimit="",TransferMode=0)
	{
		global WinSCP ; WinSCP Enums
		
		;~ Check
		;FilePermissions
		if (FilePermissions && (ComObjType(permissions,"Name")=="_FilePermissions"))
			throw "Invalid FilePermissions"
		
		;PreserveTimestamp
		if PreserveTimestamp not in 0,1
			throw "Invalid value for PreserveTimestamp"
			
		;permissions
		if (ResumeSupport && (ComObjType(ResumeSupport,"Name")=="_TransferResumeSupport"))
			throw "Invalid ResumeSupport"
		
		;SpeedLimit
		if SpeedLimit is not Integer
			throw "Invalid SpeedLimit"
		
		;TransferMode
		if TransferMode not in % this.StringJoin(WinSCP.TransferMode,",")
			throw "Invalid TransferMode"
		
		this.TransferOptions.FileMask 			:= (FileMask) ? FileMask : ""
		this.TransferOptions.FilePermissions 	:= (FilePermissions) ? FilePermissions: ""
		this.TransferOptions.PreserveTimestamp 	:= (PreserveTimestamp) ? PreserveTimestamp : ""
		this.TransferOptions.ResumeSupport	 	:= (ResumeSupport) ? ResumeSupport : ""
		this.TransferOptions.SpeedLimit	 		:= (SpeedLimit) ? SpeedLimit : ""
		this.TransferOptions.TransferMode  		:= (TransferMode) ? TransferMode : ""
	}
	
	/*
	Description
		Converts special characters in file path to make it unambiguous file mask/wildcard.
		http://winscp.net/eng/docs/library_session_escapefilemask
		
	Input
		fileMask : [string] File path to convert.
	*/
	EscapeFileMask(FileMask)
	{
		return this.Session.EscapeFileMask(FileMask)
	}
	
	/*
	Description
		Join Array to String
	*/
	StringJoin(array, delim=";")
	{
		t := ""
		for key, value in array
			t .= (t ? delim : "") . value
		return t
	}
}
lexikos
Posts: 9560
Joined: 30 Sep 2013, 04:07
Contact:

Re: Register event in comobject

12 Jun 2015, 20:52

Event handling in AutoHotkey should be similar to VBScript/JScript. There's a section in the WinSCP documentation for Event Handling in WSH (Windows Script Host). Rather than the two-parameter mode of WScript.CreateObject, you should try ComObjConnect.
oliverlipkau1

Re: Register event in comobject

17 Jun 2015, 04:17

Is working perfectly.
In case someone is interested in using WinSCP in AHK, I created a GitHub repo for that:
https://github.com/lipkau/WinSCP.ahk
TXShooter
Posts: 165
Joined: 13 Dec 2017, 09:27

Registering and Using WinSCP in AHK in Win 7 & 10

19 Mar 2018, 19:33

I am trying to figure out how to use WinSCP in my AHK script, but for some reason I am both lost and confused. I can't get the dll to register, and besides that I can't figure out how to use WinSCP even with the GUI to generate a script for uploading a folder and its contents.

Can you start a dialog on this in greater detail, or has this been dropped?
oliverlipkau
Posts: 9
Joined: 16 Jun 2015, 06:40
Contact:

Re: Register event in comobject

17 Aug 2018, 08:29

Hi TXShooter.

Did you manage to solve the problem? Do you still need help?
Tradesxi

Re: Register event in comobject

17 Aug 2018, 08:55

oliverlipkau wrote:Hi TXShooter.

Did you manage to solve the problem? Do you still need help?
can you tell me what register event can do?

i tried to google it but i got links to registering for events like real world even when i searched for register event windows programming

what can we do with it for example? what events like?
oliverlipkau
Posts: 9
Joined: 16 Jun 2015, 06:40
Contact:

Re: Register event in comobject

17 Aug 2018, 13:21

so... this line
https://github.com/lipkau/WinSCP.ahk/bl ... P.ahk#L179
registers the events which the .dll supports (https://winscp.net/eng/docs/library_session#events)

these events tell the running code about changes to the connection that happen async to the code running.

you can read the help of the dll for details about every event.
and the repository contains a good example on how to display a progress bar based on the progress events of the connection:
https://github.com/lipkau/WinSCP.ahk/bl ... hk#L21-L42

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], skeerrt and 152 guests