Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process of obtaining pageant PID. #316

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,31 @@ function Get-TempEnvPath($key){

# Retrieve the current SSH agent PID (or zero). Can be used to determine if there
# is a running agent.
function Get-SshAgent() {
function Get-SshAgent(){
if ($env:GIT_SSH -imatch 'plink') {
$pageantPid = Get-Process | Where-Object { $_.Name -eq 'pageant' } | Select -ExpandProperty Id -First 1
if ($null -ne $pageantPid) { return $pageantPid }
$sig=@'
using System;
using System.Runtime.InteropServices;

public static class Win32Api
{
[System.Runtime.InteropServices.DllImportAttribute("User32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern int GetWindowThreadProcessId([System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, out int lpdwProcessId);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
'@
Add-Type -TypeDefinition $sig
$HWND = [Win32Api]::FindWindow('pageant', 'pageant')

$myPid = [IntPtr]::Zero
[void][Win32Api]::GetWindowThreadProcessId($HWND, [ref] $myPid);
$pageantPid = Get-Process | Where-Object { $_.Id -eq $myPid }

if ($null -ne $pageantPid) {
return $pageantPid.Id
}
} else {
$agentPid = $Env:SSH_AGENT_PID
if ($agentPid) {
Expand Down Expand Up @@ -347,7 +368,9 @@ function Add-SshKey() {
$keystring = ""
$keyPath = Join-Path $Env:HOME ".ssh"
$keys = Get-ChildItem $keyPath/"*.ppk" -ErrorAction SilentlyContinue | Select -ExpandProperty FullName
& $pageant $keys
if ($keys){
& $pageant $keys
}
} else {
foreach ($value in $args) {
& $pageant $value
Expand Down