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:


Unfortunately, there is an unacceptable delay when using this call. The receiving process does not recognize its presence for over 5 seconds. As such, moved onto native Win32 SDK calls. Namely: SetClipboardData() and its required support functions. This call gives me the speed I require as the contents are transferred near immediately to the clipboard when I use the native methods. Unfortunately, the call to free allocalted heap memory failes like clockwork on the fourth iteration.
When I comment out the FreeHGlobal function (bad juju...), everything operates as expected. Here is the class I use to use native methods:

class ClipboardHelper
    static extern bool OpenClipboard(IntPtr hWndNewOwner);
    static extern bool EmptyClipboard();
    static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
    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);

        return true;

The top level exception is:
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:

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

6 Years
Discussion Span
Last Post by kpen

Please note that Clipboard-based inter-process communication is not a choice I have made because I thought it was a great idea. I am using this methodology because the receiving process API is already defined and I have no control over it.

Edited by kpen: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.