Skip to content

Commit

Permalink
Now uses default AppDomain as fallback option (fixes #66)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinstenning committed Jan 27, 2016
1 parent f8ace5d commit fa7d211
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions EasyLoad/Loader.cs
Expand Up @@ -86,25 +86,41 @@ static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
/// Loads EasyHook and commences the loading of user supplied assembly. This method is exported as a DllExport, making it consumable from native code.
/// </summary>
/// <param name="inParam"></param>
/// <returns>0 for success, -1 for fail</returns>
/// <returns>0 if successfully loaded into new AppDomain, 1 if could not use new AppDomain but successfully loaded into default, or -1 for fail</returns>
[DllExport("Load", System.Runtime.InteropServices.CallingConvention.StdCall)]
public static int Load([MarshalAs(UnmanagedType.LPWStr)]String inParam)
{
try
{
lock (_lock)
{
_injectCount++;
if (_easyHookDomain == null)
try
{
System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
ps.AddPermission(new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.AllFlags));
// Evidence of null means use the current appdomain evidence
_easyHookDomain = AppDomain.CreateDomain("EasyHook", null, new AppDomainSetup()
_injectCount++;
if (_easyHookDomain == null)
{
ApplicationBase = Path.GetDirectoryName(typeof(Loader).Assembly.Location),
// ShadowCopyFiles = "true", // copies assemblies from ApplicationBase into cache, leaving originals unlocked and updatable
}, ps);
System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
ps.AddPermission(new System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityPermissionFlag.AllFlags));
// Evidence of null means use the current appdomain evidence
_easyHookDomain = AppDomain.CreateDomain("EasyHook", null, new AppDomainSetup()
{
ApplicationBase = Path.GetDirectoryName(typeof(Loader).Assembly.Location),
// ShadowCopyFiles = "true", // copies assemblies from ApplicationBase into cache, leaving originals unlocked and updatable
}, ps);
}
}
catch (OutOfMemoryException ome)
{
// Creation of AppDomain failed, so fall back to using default domain (means it cannot be unloaded)

// The reason is there could be an issue with the target application's stack commit size.
// The default stack commit size must be <= 253952 (or 0x3E000) - due to bug in .NET Framework,
// this can be checked with dumpbin.exe and edited with editbin.exe.

// Load EasyHook and the target assembly
LoadEasyHookProxy lp = new LoadEasyHookProxy();
lp.Load(inParam);
return 1;
}
}

Expand All @@ -129,7 +145,7 @@ public static int Load([MarshalAs(UnmanagedType.LPWStr)]String inParam)
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Exception occured within Loader.Load: " + e.ToString());
System.Diagnostics.Debug.WriteLine("Exception occurred within Loader.Load: " + e.ToString());
}
finally
{
Expand Down

0 comments on commit fa7d211

Please sign in to comment.