Clipboard.SetText(string);
class ClipboardHelper{[DllImport("user32.dll")]static extern bool OpenClipboard(IntPtr hWndNewOwner);[DllImport("user32.dll")]static extern bool EmptyClipboard();[DllImport("user32.dll")]static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);[DllImport("user32.dll")]static extern bool CloseClipboard();[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]public static extern IntPtr GlobalFree(IntPtr handle);// Metafile mf is set to a state that is not valid inside this function.static public bool PutTextOnClipboard(IntPtr hWnd, StringBuilder s){ bool bResult = false; if (OpenClipboard(hWnd)) { IntPtr ptr = Marshal.StringToHGlobalAnsi(s.ToString()); if (EmptyClipboard()) { IntPtr hRes = SetClipboardData(1, ptr); CloseClipboard(); } Marshal.FreeHGlobal(ptr); //GlobalFree(ptr); } return true;}
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
mscorlib.dll!System.AccessViolationException.AccessViolationException() + 0x14 bytes [Native to Managed Transition] [Managed to Native Transition] mscorlib.dll!System.Reflection.CustomAttributeEncodedArgument.ParseAttributeArguments(System.Reflection.ConstArray attributeBlob, ref System.Reflection.CustomAttributeCtorParameter[] customAttributeCtorParameters, ref System.Reflection.CustomAttributeNamedParameter[] customAttributeNamedParameters, System.Reflection.RuntimeModule customAttributeModule) + 0x5b bytes mscorlib.dll!System.Reflection.CustomAttributeData.CustomAttributeData(System.Reflection.RuntimeModule scope, System.Reflection.CustomAttributeRecord caRecord) + 0x4c4 bytes mscorlib.dll!System.Reflection.CustomAttributeData.GetCustomAttributes(System.Reflection.RuntimeModule module = {System.Reflection.RuntimeModule}, int tkTarget) + 0x53 bytes mscorlib.dll!System.Reflection.CustomAttributeData.GetCustomAttributesInternal(System.Reflection.RuntimeAssembly target = {System.Reflection.RuntimeAssembly}) + 0x47 bytes mscorlib.dll!System.Reflection.RuntimeAssembly.GetCustomAttributesData() + 0x5 bytes mscorlib.dll!System.Reflection.CustomAttributeData.GetCustomAttributes(System.Reflection.Assembly target) + 0x21 bytes mscorlib.dll!System.Resources.ManifestBasedResourceGroveler.GetNeutralResourcesLanguage(System.Reflection.Assembly a = {System.Reflection.RuntimeAssembly}, ref System.Resources.UltimateResourceFallbackLocation fallbackLocation = MainAssembly) + 0x31 bytes mscorlib.dll!System.Resources.ResourceManager.CommonSatelliteAssemblyInit() + 0x6d bytes mscorlib.dll!System.Resources.ResourceManager.ResourceManager(string baseName, System.Reflection.Assembly assembly = {System.Reflection.RuntimeAssembly}) + 0x51 bytes mscorlib.dll!System.Environment.ResourceHelper.GetResourceStringCode(object userDataIn) + 0x111 bytes [Native to Managed Transition] [Managed to Native Transition] mscorlib.dll!System.Environment.ResourceHelper.GetResourceString(string key, System.Globalization.CultureInfo culture) + 0xd1 bytes mscorlib.dll!System.Environment.GetResourceStringLocal(string key) + 0x48 bytes [Native to Managed Transition] [Managed to Native Transition] mscorlib.dll!System.Runtime.InteropServices.COMException.COMException() + 0x14 bytes [Native to Managed Transition] [Managed to Native Transition] mscorlib.dll!System.Runtime.InteropServices.Marshal.FreeHGlobal(System.IntPtr hglobal) + 0x74 bytes