Skip to content

Commit

Permalink
load manifest dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
weltkante committed Feb 17, 2024
1 parent f50bf2f commit 1a9ee44
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<NativeProjectReference Include="..\InteropTests\NativeTests\CMakeLists.txt" CMakeProject="..\InteropTests\NativeTests\NativeTests.proj" BuildNative="true" />
</ItemGroup>

<Target Name="CopyTlbProjectFiles" BeforeTargets="CopyNativeProjectBinaries">
<ItemGroup>
<NativeProjectBinaries Include="$(NativeProjectOutputFolder)\*.tlb" />
<NativeProjectBinaries Include="$(NativeProjectOutputFolder)\*.manifest" />
</ItemGroup>
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,101 @@

using System.ComponentModel;
using System.Runtime.InteropServices;
using Windows.Win32.System.ApplicationInstallationAndServicing;

namespace System.Windows.Forms.Tests;

public class AxHostAtlTests
{
[WinFormsFact]
public void AxHost_AtlControl_CreateAndSetText()
#region Utilities

// taken from \src\System.Windows.Forms\tests\InteropTests\PropertyGridTests.cs

[DllImport("kernel32", SetLastError = true)]
private static extern void ReleaseActCtx(IntPtr hActCtx);

private unsafe void ExecuteWithActivationContext(string applicationManifest, Action action)
{
if (RuntimeInformation.ProcessArchitecture != Architecture.X86)
ACTCTXW context = new();
HANDLE handle;
fixed (char* p = applicationManifest)
{
return;
}

using Form form = new();
using AxNativeAtlControl control = new();
context.cbSize = (uint)sizeof(ACTCTXW);
context.lpSource = p;

int textChangedEventCount = 0;
string textChangedEventArg = null;
const string testText = "Hello World";
handle = PInvoke.CreateActCtx(&context);
}

control.AxTextChanged += (string text) =>
if (handle == IntPtr.Zero)
{
textChangedEventCount++;
textChangedEventArg = text;
};
throw new Win32Exception();
}

form.Shown += (object sender, EventArgs e) =>
try
{
nuint cookie;
if (!PInvoke.ActivateActCtx(handle, &cookie))
{
throw new Win32Exception();
}

try
{
action();
}
finally
{
if (!PInvoke.DeactivateActCtx(0, cookie))
{
throw new Win32Exception();
}
}
}
finally
{
control.AxText = testText;
form.Close();
};
ReleaseActCtx(handle);
}
}

((ISupportInitialize)control).BeginInit();
form.Controls.Add(control);
((ISupportInitialize)control).EndInit();
form.ShowDialog();
#endregion

[WinFormsFact]
public void AxHost_AtlControl_CreateAndSetText()
{
if (RuntimeInformation.ProcessArchitecture != Architecture.X86)
{
return;
}

Assert.Equal(1, textChangedEventCount);
Assert.Equal(testText, textChangedEventArg);
ExecuteWithActivationContext("App.manifest", () =>
{
using Form form = new();
using AxNativeAtlControl control = new();

int textChangedEventCount = 0;
string textChangedEventArg = null;
const string testText = "Hello World";

control.AxTextChanged += (string text) =>
{
textChangedEventCount++;
textChangedEventArg = text;
};

form.Shown += (object sender, EventArgs e) =>
{
control.AxText = testText;
form.Close();
};

((ISupportInitialize)control).BeginInit();
form.Controls.Add(control);
((ISupportInitialize)control).EndInit();
form.ShowDialog();

Assert.Equal(1, textChangedEventCount);
Assert.Equal(testText, textChangedEventArg);
});
}

private unsafe class AxNativeAtlControl : AxHost
Expand Down

0 comments on commit 1a9ee44

Please sign in to comment.