Hello,
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:
Clipboard.SetText(string);
|
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 { [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;
}
|
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
|
theLizardPosted Aug 17, 2010, 5:44 PM
It is very difficult to formulate a solution without knowing a lot more details, It may be possible to use the send keys command to the specific window in the other process, it is not that difficult to do or it could be possible to use SendMessage( .... ) to the window process.
Ken PendleburyPosted Aug 17, 2010, 10:51 AM
Thanks.
theLizardPosted Aug 17, 2010, 1:40 AM
Ken PendleburyPosted Aug 16, 2010, 5:01 PM