File: ShellLinkEx.cs

package info (click to toggle)
keepass2 2.41%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,892 kB
  • sloc: cs: 103,600; xml: 5,869; cpp: 308; sh: 48; makefile: 46
file content (183 lines) | stat: -rw-r--r-- 5,288 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2019 Dominik Reichl <dominik.reichl@t-online.de>

  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 St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Diagnostics;

using KeePassLib.Utility;

namespace KeePass.Native
{
	internal sealed class ShellLinkEx
	{
		private string m_strPath = null;
		public string Path
		{
			get { return m_strPath; }
			set { m_strPath = value; }
		}

		private string m_strArgs = null;
		public string Arguments
		{
			get { return m_strArgs; }
			set { m_strArgs = value; }
		}

		private string m_strDesc = null;
		public string Description
		{
			get { return m_strDesc; }
			set
			{
				if((value != null) && (value.Length >= NativeMethods.INFOTIPSIZE))
				{
					Debug.Assert(false);
					m_strDesc = StrUtil.CompactString3Dots(value,
						NativeMethods.INFOTIPSIZE - 1);
				}
				else m_strDesc = value;
			}
		}

		public ShellLinkEx()
		{
		}

		public ShellLinkEx(string strPath, string strArgs, string strDesc)
		{
			m_strPath = strPath;
			m_strArgs = strArgs;
			this.Description = strDesc; // Shortens description if necessary
		}

		public static ShellLinkEx Load(string strLnkFilePath)
		{
			try
			{
				CShellLink csl = new CShellLink();

				IShellLinkW sl = (csl as IShellLinkW);
				if(sl == null) { Debug.Assert(false); return null; }
				IPersistFile pf = (csl as IPersistFile);
				if(pf == null) { Debug.Assert(false); return null; }

				pf.Load(strLnkFilePath, (int)(NativeMethods.STGM.Read |
					NativeMethods.STGM.ShareDenyWrite));

				const int ccMaxPath = KeePassLib.Native.NativeMethods.MAX_PATH;
				const int ccInfoTip = NativeMethods.INFOTIPSIZE;

				ShellLinkEx r = new ShellLinkEx();

				StringBuilder sb = new StringBuilder(ccMaxPath + 1);
				sl.GetPath(sb, sb.Capacity, IntPtr.Zero, 0);
				r.Path = sb.ToString();

				sb = new StringBuilder(ccInfoTip + 1);
				sl.GetArguments(sb, sb.Capacity);
				r.Arguments = sb.ToString();

				sb = new StringBuilder(ccInfoTip + 1);
				sl.GetDescription(sb, sb.Capacity);
				r.Description = sb.ToString();

				return r;
			}
			catch(Exception) { Debug.Assert(false); }

			return null;
		}

		public bool Save(string strLnkFilePath)
		{
			try
			{
				CShellLink csl = new CShellLink();

				IShellLinkW sl = (csl as IShellLinkW);
				if(sl == null) { Debug.Assert(false); return false; }
				IPersistFile pf = (csl as IPersistFile);
				if(pf == null) { Debug.Assert(false); return false; }

				if(!string.IsNullOrEmpty(m_strPath))
					sl.SetPath(m_strPath);
				if(!string.IsNullOrEmpty(m_strArgs))
					sl.SetArguments(m_strArgs);
				if(!string.IsNullOrEmpty(m_strDesc))
					sl.SetDescription(m_strDesc);

				pf.Save(strLnkFilePath, true);
				return true;
			}
			catch(Exception) { Debug.Assert(false); }

			return false;
		}
	}

	[ComImport]
	[Guid("000214F9-0000-0000-C000-000000000046")]
	[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
	internal interface IShellLinkW
	{
		void GetPath([MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile,
			int cch, IntPtr pfd, uint fFlags);

		void GetIDList(out IntPtr ppidl);
		void SetIDList(IntPtr pidl);

		void GetDescription([MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName,
			int cch);
		void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);

		void GetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir,
			int cch);
		void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);

		void GetArguments([MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cch);
		void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);

		void GetHotkey(out ushort pwHotkey);
		void SetHotkey(ushort wHotkey);

		void GetShowCmd(out int piShowCmd);
		void SetShowCmd(int iShowCmd);

		void GetIconLocation([MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
			int cch, out int piIcon);
		void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath,
			int iIcon);

		void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel,
			uint dwReserved);

		void Resolve(IntPtr hwnd, uint fFlags);

		void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
	}

	[ComImport]
	[Guid("00021401-0000-0000-C000-000000000046")]
	internal class CShellLink { }
}