Fixed issue 1588: SSHAskPass fails to start in Win XP
[TortoiseGit.git] / src / SshAskPass / SshAskPass.cpp
blob8840865653e357f78333c362fcbc06f75843f91b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // SshAskPass.cpp : Defines the entry point for the application.
23 #include "stdafx.h"
24 #include "SshAskPass.h"
25 #include <stdio.h>
26 #include "propsys.h"
27 #include "PropKey.h"
28 #include <atlbase.h>
30 #include <commctrl.h>
31 #pragma comment(lib, "comctl32.lib")
32 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
34 #define MAX_LOADSTRING 100
36 // Global Variables:
37 HINSTANCE hInst; // current instance
38 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
39 TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
41 // Forward declarations of functions included in this code module:
42 INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
44 TCHAR g_Promptphrase[] = _T("Enter your OpenSSH passphrase:");
45 TCHAR *g_Prompt = NULL;
47 TCHAR g_PassWord[MAX_LOADSTRING];
49 int APIENTRY _tWinMain(HINSTANCE /*hInstance*/,
50 HINSTANCE /*hPrevInstance*/,
51 LPTSTR lpCmdLine,
52 int /*nCmdShow*/)
54 SetDllDirectory(L"");
56 InitCommonControls();
58 if( _tcslen(lpCmdLine) == 0 )
60 g_Prompt = g_Promptphrase;
62 else
64 g_Prompt = lpCmdLine;
67 TCHAR *yesno=_T("(yes/no)");
68 size_t lens = _tcslen(yesno);
69 TCHAR *p = lpCmdLine;
70 BOOL bYesNo=FALSE;
72 while(*p)
74 if (_tcsncicmp(p, yesno, lens) == 0)
76 bYesNo = TRUE;
77 break;
79 p++;
82 if(bYesNo)
84 if (::MessageBox(NULL, lpCmdLine, _T("TortoiseGit - git CLI stdin wrapper"), MB_YESNO|MB_ICONQUESTION) == IDYES)
86 _tprintf(_T("yes"));
88 else
90 _tprintf(_T("no"));
92 return 0;
94 else
96 if(DialogBox(hInst, MAKEINTRESOURCE(IDD_ASK_PASSWORD), NULL, About) == IDOK)
98 _tprintf(_T("%s\n"), g_PassWord);
99 return 0;
101 _tprintf(_T("\n"));
102 return -1;
106 void MarkWindowAsUnpinnable(HWND hWnd)
108 typedef HRESULT (WINAPI *SHGPSFW) (HWND hwnd,REFIID riid,void** ppv);
110 HMODULE hShell = AtlLoadSystemLibraryUsingFullPath(_T("Shell32.dll"));
112 if (hShell) {
113 SHGPSFW pfnSHGPSFW = (SHGPSFW)::GetProcAddress(hShell, "SHGetPropertyStoreForWindow");
114 if (pfnSHGPSFW) {
115 IPropertyStore *pps;
116 HRESULT hr = pfnSHGPSFW(hWnd, IID_PPV_ARGS(&pps));
117 if (SUCCEEDED(hr)) {
118 PROPVARIANT var;
119 var.vt = VT_BOOL;
120 var.boolVal = VARIANT_TRUE;
121 hr = pps->SetValue(PKEY_AppUserModel_PreventPinning, var);
122 pps->Release();
125 FreeLibrary(hShell);
129 // Message handler for password box.
130 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM /*lParam*/)
132 switch (message)
134 case WM_INITDIALOG:
136 MarkWindowAsUnpinnable(hDlg);
137 RECT rect;
138 ::GetWindowRect(hDlg,&rect);
139 DWORD dwWidth = GetSystemMetrics(SM_CXSCREEN);
140 DWORD dwHeight = GetSystemMetrics(SM_CYSCREEN);
142 DWORD x,y;
143 x=(dwWidth - (rect.right-rect.left))/2;
144 y=(dwHeight - (rect.bottom-rect.top))/2;
146 ::MoveWindow(hDlg,x,y,rect.right-rect.left,rect.bottom-rect.top,TRUE);
147 HWND title=::GetDlgItem(hDlg,IDC_STATIC_TITLE);
148 ::SetWindowText(title,g_Prompt);
150 TCHAR *pass =_T("pass");
151 size_t passlens = _tcslen(pass);
152 TCHAR *p = g_Prompt;
153 bool password = false;
154 while (*p)
156 if (_tcsncicmp(p, pass, passlens) == 0)
158 password = true;
159 break;
161 p++;
163 if (!password)
164 SendMessage(::GetDlgItem(hDlg, IDC_PASSWORD), EM_SETPASSWORDCHAR, 0, 0);
166 return (INT_PTR)TRUE;
168 case WM_COMMAND:
170 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
172 if( LOWORD(wParam) == IDOK )
174 HWND password = ::GetDlgItem(hDlg,IDC_PASSWORD);
175 ::GetWindowText(password,g_PassWord,MAX_LOADSTRING);
177 EndDialog(hDlg, LOWORD(wParam));
178 return (INT_PTR)TRUE;
180 break;
182 return (INT_PTR)FALSE;