I am fairly new to all this and I am hoping somone can help. I am writing a C# application that requires use of the Clipborad for inter-process communication. C# has methods I can leverage in order to accomplish this:
Expand|Select|Wrap|Line Numbers
- Clipboard.SetText(string);
When I comment out the FreeHGlobal function (bad juju...), everything operates as expected. Here is the class I use to use call the native methods:
Expand|Select|Wrap|Line Numbers
- 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);
- 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 /*CF_TEXT*/, ptr);
- CloseClipboard();
- }
- Marshal.FreeHGlobal(ptr);
- //GlobalFree(ptr);
- }
- return true;
- }
Expand|Select|Wrap|Line Numbers
- An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
Does anyone have any idea what I am doing wrong here?
Partial stack trace:
Expand|Select|Wrap|Line Numbers
- 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