simplify tryf
MODIFY
TestConsole/TestConsole.csproj
TestConsole/TestConsole.csproj
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;net472</TargetFrameworks>
+ <!--<PlatformTarget>x86</PlatformTarget>-->
</PropertyGroup>
<ItemGroup>

MODIFY
Tests/Tests.csproj
Tests/Tests.csproj
<Optimize>false</Optimize>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
+ <PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>

MODIFY
TextCopy/WindowsClipboard.cs
TextCopy/WindowsClipboard.cs
@@ -12,22 +12,17 @@ static class WindowsClipboard
}
EmptyClipboard();
+ var hGlobal = IntPtr.Zero;
try
{
- var bytes = ((uint) text.Length + 1) * 2;
- var hGlobal = GlobalAlloc(GMEM_MOVABLE, (UIntPtr) bytes);
+ var bytes = (text.Length + 1) * 2;
+ hGlobal = Marshal.AllocHGlobal(bytes);
if (hGlobal == IntPtr.Zero)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
- try
- {
- var source = Marshal.StringToHGlobalUni(text);
-
- try
- {
var target = GlobalLock(hGlobal);
if (target == IntPtr.Zero)
@@ -37,7 +32,7 @@ static class WindowsClipboard
try
{
- CopyMemory(target, source, bytes);
+ Marshal.Copy(text.ToCharArray(), 0, target, text.Length);
}
finally
{
@@ -52,27 +47,16 @@ static class WindowsClipboard
hGlobal = IntPtr.Zero;
}
finally
- {
- Marshal.FreeHGlobal(source);
- }
- }
- finally
{
if (hGlobal != IntPtr.Zero)
{
GlobalFree(hGlobal);
}
- }
- }
- finally
- {
+
CloseClipboard();
}
}
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);
-
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GlobalFree(IntPtr hMem);
@@ -83,9 +67,6 @@ static class WindowsClipboard
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GlobalUnlock(IntPtr hMem);
- [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", ExactSpelling = true)]
- public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
-
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
@@ -101,5 +82,4 @@ static class WindowsClipboard
static extern bool EmptyClipboard();
const uint CF_UNICODETEXT = 13;
- const uint GMEM_MOVABLE = 0x0002;
}
\ No newline at end of file

simplify tryf