-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAPLogonHandler.cs
165 lines (134 loc) · 5.93 KB
/
SAPLogonHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright (C) 2016 Marko Graf
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using KeePassLib.Security;
namespace KeeSAPLogon
{
public class SAPLogonHandler
{
public static string SAPGUIShortCutEXE = "sapshcut.exe";
private static string m_sapguiPath = "";
//---------------------------------------------------------------------------------------------------
// Public Static Methods
//---------------------------------------------------------------------------------------------------
public static string SAPGUIPath
{
get { return m_sapguiPath; }
set { m_sapguiPath = value; }
}
public static bool DoLogon(LogonColumn logonPoint, ProtectedString user, ProtectedString password)
{
if (ValidateSAPGUIPath(m_sapguiPath) && logonPoint.IsValid())
{
//Example:
//
// "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut" -system=ABC -client=010 -user=username -pw=pass -language=EN -maxgui -command=SE10
//
string strArgs = "-maxgui";
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("-system", logonPoint.SAPID);
args.Add("-client", logonPoint.SAPClient);
args.Add("-user", user.ReadString());
args.Add("-pw", password.ReadString());
args.Add("-language", logonPoint.SAPLanguage);
args.Add("-command", logonPoint.SAPTransaction);
foreach (string key in args.Keys)
{
string value;
args.TryGetValue(key, out value);
strArgs = strArgs + " ";
strArgs = strArgs + key + "=" + value;
}
string fileLoc = Path.Combine(m_sapguiPath, SAPLogonHandler.SAPGUIShortCutEXE);
FileInfo fileInfo = new FileInfo(fileLoc);
ProcessStartInfo info = new ProcessStartInfo(fileInfo.FullName);
info.Arguments = strArgs;
info.CreateNoWindow = false;
info.UseShellExecute = true;
info.ErrorDialog = true;
info.RedirectStandardInput = false;
info.RedirectStandardOutput = false;
Process process = Process.Start(info);
return !(process.HasExited);
}
return false;
}
public static bool ValidateSAPGUIPath(string path)
{
//Validating if 'sapshcut.exe' located by given path
string fileLoc = Path.Combine(path, SAPLogonHandler.SAPGUIShortCutEXE);
FileInfo fileInfo = new FileInfo(fileLoc);
return (fileInfo.Exists);
}
//---------------------------------------------------------------------------------------------------
// Implementation: Detect SAP GUI installation automatically via Windows registry
//---------------------------------------------------------------------------------------------------
//***************************************************************************************************
//* *
//* Special thanks to ANatrix for the idea how to retrieve SAPGUI installation path. *
//* *
//***************************************************************************************************
#region SAPGUIDetection
public static string DetectSAPGUIPath()
{
RegistryKey rootKey = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, "");
RegistryKey subKey = null;
bool foundPath = true;
object objPath;
string sPath = "";
string resPath = "";
try
{
// Check path from registry for x86
subKey = rootKey.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + SAPGUIShortCutEXE);
objPath = subKey.GetValue("Path");
sPath = Convert.ToString(objPath);
}
catch
{
foundPath = false;
};
if (!foundPath)
{
try
{
// Check path from registry for 64bit
subKey = rootKey.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\App Paths\\" + SAPGUIShortCutEXE);
objPath = subKey.GetValue("Path");
sPath = Convert.ToString(objPath);
}
catch
{
foundPath = false;
}
}
if (foundPath)
{
for (int i = 0; ((i < sPath.Length) && (sPath[i] != ';')); i++)
{
resPath += sPath[i].ToString();
}
if (resPath.Length < 3) // "C:\"
{
foundPath = false;
}
else
{
if (SAPLogonHandler.ValidateSAPGUIPath(resPath)) return resPath;
}
}
else
{
MessageBox.Show("SAPGUI not installed!", KeeSAPLogonExt.PlugInName + " settings error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return resPath;
} //DetectSAPGUIPath
#endregion
}
}