PortableApps.com Platform and Suite 1.6 have been released.

WinSCP integration with PuTTY?

Submitted by beq on December 21, 2008 - 3:06am

Does anyone know why PuTTY Portable must be started manually before WinSCP Portable can invoke it via its toolbar button? Do the standard (non-PortableApps) versions of these programs have this same limitation?

Also, I noticed that WinSCP Portable is configured to use the file "PuTTYPortableLinker.exe" as the path for the PuTTY program. Would the integration also work if I change this WinSCP setting to the direct path of the putty.exe program file (or perhaps the puttyportable.exe launcher file)?

Sorry I'm somewhat clueless...


( categories: )

Oh, is the use of the

Oh, is the use of the puttyportablelinker.exe file necessary because the PuTTY path setting in WinSCP must be an absolute (instead of relative) path?

As Is

You must do it as is with no changes. They both use the registry to pass information from one to the other which is why you must use the launcher and have it running first.

Sometimes, the impossible can become possible, if you're awesome!

How about this?

So let me see if I understand this. At the point where PuTTYPortableLinker.exe sends up the error and the prompt to the user, it has already checked for the existence of PuTTYPortable and found it (from reading the linker source). So the problem is that the one key HKCU\...\Sessions\WinSCP Portable Temporary Session is a "portable" key, but it has been put into the local version of the registry,so when you launch PuTTYPortable, that session will get backup up as part of the local registry and not passed on to the portable version. So this is why PuTTYPortable must be launched first, so when that key is created, the key will go into the portable version of the registry. Ok, so how about this for an idea? At the point where PuTTYPortableLinker normally deletes that key and sends up an error, why can't we do the following:

Export just the temp session key into a .reg file.
Delete the temp session key from the registry

Simulate a launch of PuTTYPortable.exe (but only the registry edits)
-Back up the registry the same way PuTTYPortable does.
-Import PuTTYPortable/Data/settings/putty.reg into the registry
(Because we've already found PuTTYPortable, so we can access its Data)

Now we re-import the temp session key into the registry because it is in it's "portable state"

Now simulate a PuTTYPortable shutdown
Export the entire PuTTY key back into PuTTYPortable/Data/settings/putty.reg
Delete the portable PuTTY keys from the registry
Restore the registry to it's original state

Now you can launch PuTTYPortable and the temp session key will be loaded correctly, without having to have PuTTYPortable open first Smiling

In effect, this would achieve the same as having PuTTYPortable open when the user clicked on the PuTTY button in WinSCP because the registry would be in the correct state.

[Update]
I think I got this working, testing and feedback would be appreciated.

;Copyright (C) 2004-2007 John T. Haller of PortableApps.com
;Edited by Ben Whitman 2009

;Website: http://portableapps.com/PuTTYPortable

;This software is OSI Certified Open Source Software.
;OSI Certified is a certification mark of the Open Source Initiative.

;This program is free software; you can redistribute it and/or
;modify it under the terms of the GNU General Public License
;as published by the Free Software Foundation; either version 2
;of the License, or (at your option) any later version.

;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.

;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

!define NAME "PuTTYPortableLinker"
!define FULLNAME "PuTTY Portable Linker"
!define APP "PuTTY"
!define VER "1.7.0.0"
!define WEBSITE "PortableApps.com/WinSCPPortable"

;=== Program Details
Name "${FULLNAME}"
OutFile "${NAME}.exe"
Caption "${FULLNAME} | PortableApps.com"
VIProductVersion "${VER}"
VIAddVersionKey ProductName "${FULLNAME}"
VIAddVersionKey Comments "Allows ${APP} to be run from a removable drive.  For additional details, visit ${WEBSITE}"
VIAddVersionKey CompanyName "PortableApps.com"
VIAddVersionKey LegalCopyright "John T. Haller"
VIAddVersionKey FileDescription "${FULLNAME}"
VIAddVersionKey FileVersion "${VER}"
VIAddVersionKey ProductVersion "${VER}"
VIAddVersionKey InternalName "${FULLNAME}"
VIAddVersionKey LegalTrademarks "PortableApps.com is a trademark of Rare Ideas, LLC."
VIAddVersionKey OriginalFilename "${NAME}.exe"
;VIAddVersionKey PrivateBuild ""
;VIAddVersionKey SpecialBuild ""

;=== Runtime Switches
CRCCheck On
WindowIcon Off
SilentInstall Silent
AutoCloseWindow True
RequestExecutionLevel user

;=== Include
;(Standard NSIS)
!include FileFunc.nsh
!insertmacro GetParent
!insertmacro GetParameters
!include Registry.nsh
!include ReplaceInFile.nsh
!include StrRep.nsh

;=== Program Icon
Icon "${NAME}.ico"

Var EXECSTRING
Var PORTABLEAPPSDIRECTORY
Var SETTINGSDIRECTORY
Var FAILEDTORESTOREKEY

Section "Main"
	;=== Check for EXE
	${GetParent} "$EXEDIR" $R0
	${GetParent} $R0 $R0
	${GetParent} $R0 $PORTABLEAPPSDIRECTORY
	IfFileExists `$PORTABLEAPPSDIRECTORY\PuTTYPortable\PuTTYPortable.exe` FoundProgramEXE
		MessageBox MB_OK|MB_ICONINFORMATION `Could not locate $PORTABLEAPPSDIRECTORY\PuTTYPortable\PuTTYPortable.exe.  Please ensure that PuTTY Portable and WinSCP Portable are installed within your PortableApps directory.`
		Abort
		
        

	FoundProgramEXE:
		;=== Check if running
		FindProcDLL::FindProc "PuTTYPortable.exe"
		StrCmp $R0 "1" GetPassedParameters CheckINI

        CheckINI:
                 ;=== Find the INI file, if there is one
                 IfFileExists "$PORTABLEAPPSDIRECTORY\PuTTYPortable\PuTTYPortable.ini" "" NoINI
                 
        ;ReadINI:
                 ;=== Read the parameters from the INI file
                 ReadINIStr $0 "$PORTABLEAPPSDIRECTORY\PuTTYPortable\PuTTYPortable.ini" "PuTTYPortable" "SettingsDirectory"
                 StrCpy "$SETTINGSDIRECTORY" "$PORTABLEAPPSDIRECTORY\PuTTYPortable\$0"
                 IfErrors NoINI RegistryBackup
                 
        NoINI:
                 StrCpy "$SETTINGSDIRECTORY" "$PORTABLEAPPSDIRECTORY\PuTTYPortable\Data\settings"

        RegistryBackup:
                ${registry::SaveKey} "HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\WinSCP%20Portable%20Temporary%20Session" "$SETTINGSDIRECTORY\tempsession.reg" "" $0
                ${registry::DeleteKey} "HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\WinSCP%20Portable%20Temporary%20Session" $R0
		;=== Backup the registry
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\SimonTatham-BackupByPuTTYPortable" $R0
		StrCmp $R0 "0" RestoreSettings
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\SimonTatham" $R0
		StrCmp $R0 "-1" RestoreSettings
		${registry::MoveKey} "HKEY_CURRENT_USER\Software\SimonTatham" "HKEY_CURRENT_USER\Software\SimonTatham-BackupByPuTTYPortable" $R0
		Sleep 100
		
	RestoreSettings:
		IfFileExists "$SETTINGSDIRECTORY\putty.reg" "" SetRandomSeedPath

		;=== Get last drive letter
		ReadINIStr $0 "$SETTINGSDIRECTORY\putty.reg" "HKEY_CURRENT_USER\Software\SimonTatham\PuTTY" `"RandSeedFile"`
		StrCpy $1 $0 1 ;last drive letter
		StrCpy $2 $EXEDIR 1 ;current drive letter
		StrCmp $2 $1 RestoreTheKey
		StrCpy $3 `"PublicKeyFile"="$1:\\`
		StrCpy $4 `"PublicKeyFile"="$2:\\`
		;MessageBox MB_OK|MB_ICONINFORMATION `$3 *** $4`
		${ReplaceInFile} "$SETTINGSDIRECTORY\putty.reg" $3 $4

	RestoreTheKey:
		IfFileExists "$WINDIR\system32\reg.exe" "" RestoreTheKey9x
			nsExec::ExecToStack `"$WINDIR\system32\reg.exe" import "$SETTINGSDIRECTORY\putty.reg"`
			Pop $R0
			StrCmp $R0 '0' "" RestoreTheKey9x
			nsExec::ExecToStack `"$WINDIR\system32\reg.exe" import "$SETTINGSDIRECTORY\tempsession.reg"`
			Pop $R0
			StrCmp $R0 '0' SetRandomSeedPath
	RestoreTheKey9x:
		${registry::RestoreKey} "$SETTINGSDIRECTORY\putty.reg" $R0
		Sleep 2000
		StrCmp $R0 '0' "" +4
		${registry::RestoreKey} "$SETTINGSDIRECTORY\tempsession.reg" $R0
		Sleep 2000
		StrCmp $R0 '0' SetRandomSeedPath
		StrCpy $FAILEDTORESTOREKEY "true"

	SetRandomSeedPath:
		Sleep 100
		${registry::Write} "HKEY_CURRENT_USER\Software\SimonTatham\PuTTY" "RandSeedFile" "$SETTINGSDIRECTORY\PUTTY.RND" "REG_SZ" $0
		Sleep 100
		
        ;CheckSuccessful:
		StrCmp $FAILEDTORESTOREKEY "true" SetOriginalKeyBack
		CreateDirectory $SETTINGSDIRECTORY
		${registry::SaveKey} "HKEY_CURRENT_USER\Software\SimonTatham\PuTTY" "$SETTINGSDIRECTORY\putty.reg" "" $0
		Sleep 100
		
	SetOriginalKeyBack:
		${registry::DeleteKey} "HKEY_CURRENT_USER\Software\SimonTatham" $R0
		Sleep 100
		${registry::KeyExists} "HKEY_CURRENT_USER\Software\SimonTatham-BackupByPuTTYPortable" $R0
		StrCmp $R0 "-1" GetPassedParameters
		${registry::MoveKey} "HKEY_CURRENT_USER\Software\SimonTatham-BackupByPuTTYPortable" "HKEY_CURRENT_USER\Software\SimonTatham" $R0
		Sleep 100

        StrCmp $FAILEDTORESTOREKEY "true" "" GetPassedParameters
		MessageBox MB_OK|MB_ICONINFORMATION `Please start PuTTY Portable before attempting to use PuTTY integration within WinSCP Portable.`
		Abort
	
	GetPassedParameters:
		;=== Get any passed parameters
		${GetParameters} $0
		StrCmp "'$0'" "''" "" LaunchProgramParameters

		;=== No parameters
		StrCpy $EXECSTRING `"$PORTABLEAPPSDIRECTORY\PuTTYPortable\PuTTYPortable.exe"`
		Goto StartPuTTYPortable

	LaunchProgramParameters:
		StrCpy $EXECSTRING `"$PORTABLEAPPSDIRECTORY\PuTTYPortable\PuTTYPortable.exe" $0`
		
	StartPuTTYPortable:
		Exec $EXECSTRING
SectionEnd

Quamquam omniam nescio, nec nihil scio.

Also...

Also, it would be nice if the PuTTYPortable launcher could check for the existence of HKCU\Software\SimonTatham\Sessions\WinSCP Portable Temporary Session on shutdown and delete it before it backs up the key to putty.reg. It is a TEMPORARY session after all, so it's just annoying for it to be there the next time you launch PuTTYPortable.

Quamquam omniam nescio, nec nihil scio.