mscorlib(4.0.0.0) API with additions
ActivationContext.cs
1 using System.Collections;
4 using System.IO;
7 using System.Security;
8 
9 namespace System
10 {
12  [Serializable]
13  [ComVisible(false)]
15  {
17  public enum ContextForm
18  {
20  Loose,
22  StoreBounded
23  }
24 
25  internal enum ApplicationState
26  {
27  Undefined,
28  Starting,
29  Running
30  }
31 
32  internal enum ApplicationStateDisposition
33  {
34  Undefined = 0,
35  Starting = 1,
36  StartingMigrated = 65537,
37  Running = 2,
38  RunningFirstTime = 131074
39  }
40 
41  private ApplicationIdentity _applicationIdentity;
42 
43  private ArrayList _definitionIdentities;
44 
45  private ArrayList _manifests;
46 
47  private string[] _manifestPaths;
48 
49  private ContextForm _form;
50 
51  private ApplicationStateDisposition _appRunState;
52 
53  private IActContext _actContext;
54 
55  private const int DefaultComponentCount = 2;
56 
59  public ApplicationIdentity Identity => _applicationIdentity;
60 
63  public ContextForm Form => _form;
64 
67  public byte[] ApplicationManifestBytes => GetApplicationManifestBytes();
68 
71  public byte[] DeploymentManifestBytes => GetDeploymentManifestBytes();
72 
73  internal string[] ManifestPaths => _manifestPaths;
74 
75  internal string ApplicationDirectory
76  {
77  [SecurityCritical]
78  get
79  {
80  if (_form == ContextForm.Loose)
81  {
82  return Path.GetDirectoryName(_manifestPaths[_manifestPaths.Length - 1]);
83  }
84  _actContext.ApplicationBasePath(0u, out string ApplicationPath);
85  return ApplicationPath;
86  }
87  }
88 
89  internal string DataDirectory
90  {
91  [SecurityCritical]
92  get
93  {
94  if (_form == ContextForm.Loose)
95  {
96  return null;
97  }
98  _actContext.GetApplicationStateFilesystemLocation(1u, UIntPtr.Zero, IntPtr.Zero, out string ppszPath);
99  return ppszPath;
100  }
101  }
102 
103  internal ICMS ActivationContextData
104  {
105  [SecurityCritical]
106  get
107  {
108  return ApplicationComponentManifest;
109  }
110  }
111 
112  internal ICMS DeploymentComponentManifest
113  {
114  [SecurityCritical]
115  get
116  {
117  if (_form == ContextForm.Loose)
118  {
119  return (ICMS)_manifests[0];
120  }
121  return GetComponentManifest((IDefinitionIdentity)_definitionIdentities[0]);
122  }
123  }
124 
125  internal ICMS ApplicationComponentManifest
126  {
127  [SecurityCritical]
128  get
129  {
130  if (_form == ContextForm.Loose)
131  {
132  return (ICMS)_manifests[_manifests.Count - 1];
133  }
134  return GetComponentManifest((IDefinitionIdentity)_definitionIdentities[_definitionIdentities.Count - 1]);
135  }
136  }
137 
138  internal ApplicationStateDisposition LastApplicationStateResult => _appRunState;
139 
140  private ActivationContext()
141  {
142  }
143 
144  [SecurityCritical]
145  private ActivationContext(SerializationInfo info, StreamingContext context)
146  {
147  string applicationIdentityFullName = (string)info.GetValue("FullName", typeof(string));
148  string[] array = (string[])info.GetValue("ManifestPaths", typeof(string[]));
149  if (array == null)
150  {
151  CreateFromName(new ApplicationIdentity(applicationIdentityFullName));
152  }
153  else
154  {
155  CreateFromNameAndManifests(new ApplicationIdentity(applicationIdentityFullName), array);
156  }
157  }
158 
159  internal ActivationContext(ApplicationIdentity applicationIdentity)
160  {
161  CreateFromName(applicationIdentity);
162  }
163 
164  internal ActivationContext(ApplicationIdentity applicationIdentity, string[] manifestPaths)
165  {
166  CreateFromNameAndManifests(applicationIdentity, manifestPaths);
167  }
168 
169  [SecuritySafeCritical]
170  private void CreateFromName(ApplicationIdentity applicationIdentity)
171  {
172  if (applicationIdentity == null)
173  {
174  throw new ArgumentNullException("applicationIdentity");
175  }
176  _applicationIdentity = applicationIdentity;
177  IEnumDefinitionIdentity enumDefinitionIdentity = _applicationIdentity.Identity.EnumAppPath();
178  _definitionIdentities = new ArrayList(2);
179  IDefinitionIdentity[] array = new IDefinitionIdentity[1];
180  while (enumDefinitionIdentity.Next(1u, array) == 1)
181  {
182  _definitionIdentities.Add(array[0]);
183  }
184  _definitionIdentities.TrimToSize();
185  if (_definitionIdentities.Count <= 1)
186  {
187  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidAppId"));
188  }
189  _manifestPaths = null;
190  _manifests = null;
191  _actContext = IsolationInterop.CreateActContext(_applicationIdentity.Identity);
192  _form = ContextForm.StoreBounded;
193  _appRunState = ApplicationStateDisposition.Undefined;
194  }
195 
196  [SecuritySafeCritical]
197  private void CreateFromNameAndManifests(ApplicationIdentity applicationIdentity, string[] manifestPaths)
198  {
199  if (applicationIdentity == null)
200  {
201  throw new ArgumentNullException("applicationIdentity");
202  }
203  if (manifestPaths == null)
204  {
205  throw new ArgumentNullException("manifestPaths");
206  }
207  _applicationIdentity = applicationIdentity;
208  IEnumDefinitionIdentity enumDefinitionIdentity = _applicationIdentity.Identity.EnumAppPath();
209  _manifests = new ArrayList(2);
210  _manifestPaths = new string[manifestPaths.Length];
211  IDefinitionIdentity[] array = new IDefinitionIdentity[1];
212  int num = 0;
213  while (enumDefinitionIdentity.Next(1u, array) == 1)
214  {
215  ICMS iCMS = (ICMS)IsolationInterop.ParseManifest(manifestPaths[num], null, ref IsolationInterop.IID_ICMS);
216  if (IsolationInterop.IdentityAuthority.AreDefinitionsEqual(0u, iCMS.Identity, array[0]))
217  {
218  _manifests.Add(iCMS);
219  _manifestPaths[num] = manifestPaths[num];
220  num++;
221  continue;
222  }
223  throw new ArgumentException(Environment.GetResourceString("Argument_IllegalAppIdMismatch"));
224  }
225  if (num != manifestPaths.Length)
226  {
227  throw new ArgumentException(Environment.GetResourceString("Argument_IllegalAppId"));
228  }
229  _manifests.TrimToSize();
230  if (_manifests.Count <= 1)
231  {
232  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidAppId"));
233  }
234  _definitionIdentities = null;
235  _actContext = null;
236  _form = ContextForm.Loose;
237  _appRunState = ApplicationStateDisposition.Undefined;
238  }
239 
241  ~ActivationContext()
242  {
243  Dispose(fDisposing: false);
244  }
245 
253  {
254  return new ActivationContext(identity);
255  }
256 
267  public static ActivationContext CreatePartialActivationContext(ApplicationIdentity identity, string[] manifestPaths)
268  {
269  return new ActivationContext(identity, manifestPaths);
270  }
271 
273  public void Dispose()
274  {
275  Dispose(fDisposing: true);
276  GC.SuppressFinalize(this);
277  }
278 
279  [SecurityCritical]
280  internal ICMS GetComponentManifest(IDefinitionIdentity component)
281  {
282  _actContext.GetComponentManifest(0u, component, ref IsolationInterop.IID_ICMS, out object ManifestInteface);
283  return ManifestInteface as ICMS;
284  }
285 
286  [SecuritySafeCritical]
287  internal byte[] GetDeploymentManifestBytes()
288  {
289  string FullPath;
290  if (_form == ContextForm.Loose)
291  {
292  FullPath = _manifestPaths[0];
293  }
294  else
295  {
296  _actContext.GetComponentManifest(0u, (IDefinitionIdentity)_definitionIdentities[0], ref IsolationInterop.IID_IManifestInformation, out object ManifestInteface);
297  ((IManifestInformation)ManifestInteface).get_FullPath(out FullPath);
298  Marshal.ReleaseComObject(ManifestInteface);
299  }
300  return ReadBytesFromFile(FullPath);
301  }
302 
303  [SecuritySafeCritical]
304  internal byte[] GetApplicationManifestBytes()
305  {
306  string FullPath;
307  if (_form == ContextForm.Loose)
308  {
309  FullPath = _manifestPaths[_manifests.Count - 1];
310  }
311  else
312  {
313  _actContext.GetComponentManifest(0u, (IDefinitionIdentity)_definitionIdentities[1], ref IsolationInterop.IID_IManifestInformation, out object ManifestInteface);
314  ((IManifestInformation)ManifestInteface).get_FullPath(out FullPath);
315  Marshal.ReleaseComObject(ManifestInteface);
316  }
317  return ReadBytesFromFile(FullPath);
318  }
319 
320  [SecuritySafeCritical]
321  internal void PrepareForExecution()
322  {
323  if (_form != 0)
324  {
325  _actContext.PrepareForExecution(IntPtr.Zero, IntPtr.Zero);
326  }
327  }
328 
329  [SecuritySafeCritical]
330  internal ApplicationStateDisposition SetApplicationState(ApplicationState s)
331  {
332  if (_form == ContextForm.Loose)
333  {
334  return ApplicationStateDisposition.Undefined;
335  }
336  _actContext.SetApplicationRunningState(0u, (uint)s, out uint ulDisposition);
337  _appRunState = (ApplicationStateDisposition)ulDisposition;
338  return _appRunState;
339  }
340 
341  [SecuritySafeCritical]
342  private void Dispose(bool fDisposing)
343  {
344  _applicationIdentity = null;
345  _definitionIdentities = null;
346  _manifests = null;
347  _manifestPaths = null;
348  if (_actContext != null)
349  {
350  Marshal.ReleaseComObject(_actContext);
351  }
352  }
353 
354  private static byte[] ReadBytesFromFile(string manifestPath)
355  {
356  byte[] array = null;
357  using (FileStream fileStream = new FileStream(manifestPath, FileMode.Open, FileAccess.Read))
358  {
359  int num = (int)fileStream.Length;
360  array = new byte[num];
361  if (fileStream.CanSeek)
362  {
363  fileStream.Seek(0L, SeekOrigin.Begin);
364  }
365  fileStream.Read(array, 0, num);
366  return array;
367  }
368  }
369 
373  [SecurityCritical]
375  {
376  if (_applicationIdentity != null)
377  {
378  info.AddValue("FullName", _applicationIdentity.FullName, typeof(string));
379  }
380  if (_manifestPaths != null)
381  {
382  info.AddValue("ManifestPaths", _manifestPaths, typeof(string[]));
383  }
384  }
385  }
386 }
static int ReleaseComObject(object o)
Decrements the reference count of the Runtime Callable Wrapper (RCW) associated with the specified CO...
Definition: Marshal.cs:2179
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
virtual void TrimToSize()
Sets the capacity to the actual number of elements in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:3110
ContextForm Form
Gets the form, or store context, for the current application.
FileMode
Specifies how the operating system should open a file.
Definition: FileMode.cs:8
Identifies the activation context for the current application. This class cannot be inherited.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
byte [] DeploymentManifestBytes
Gets the ClickOnce deployment manifest for the current application.
ApplicationIdentity Identity
Gets the application identity for the current application.
override int Read([In] [Out] byte[] array, int offset, int count)
Reads a block of bytes from the stream and writes the data in a given buffer.
Definition: FileStream.cs:1222
ContextForm
Indicates the context for a manifest-activated application.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
static ActivationContext CreatePartialActivationContext(ApplicationIdentity identity, string[] manifestPaths)
Initializes a new instance of the T:System.ActivationContext class using the specified application id...
SeekOrigin
Specifies the position in a stream to use for seeking.
Definition: SeekOrigin.cs:9
override bool CanSeek
Gets a value indicating whether the current stream supports seeking.
Definition: FileStream.cs:113
Provides a T:System.IO.Stream for a file, supporting both synchronous and asynchronous read and write...
Definition: FileStream.cs:15
byte [] ApplicationManifestBytes
Gets the ClickOnce application manifest for the current application.
void Dispose()
Releases all resources used by the T:System.ActivationContext.
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
virtual int Add(object value)
Adds an object to the end of the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2381
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
override long Length
Gets the length in bytes of the stream.
Definition: FileStream.cs:126
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
static ActivationContext CreatePartialActivationContext(ApplicationIdentity identity)
Initializes a new instance of the T:System.ActivationContext class using the specified application id...
Provides the ability to uniquely identify a manifest-activated application. This class cannot be inhe...
Specifies that the class can be serialized.
static string GetDirectoryName(string path)
Returns the directory information for the specified path string.
Definition: Path.cs:268
override long Seek(long offset, SeekOrigin origin)
Sets the current position of this stream to the given value.
Definition: FileStream.cs:1333
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14