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:
Code
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:
C#
classClipboardHelper
{
[DllImport("user32.dll")]
staticexternboolOpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
staticexternboolEmptyClipboard();
[DllImport("user32.dll")]
staticextern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
[DllImport("user32.dll")]
staticexternboolCloseClipboard();
[DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
publicstaticextern IntPtr GlobalFree(IntPtr handle);
// Metafile mf is set to a state that is not valid inside this function.staticpublicboolPutTextOnClipboard(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);
}
returntrue;
}
The top level exception is:
Code
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:
Ken Pendlebury5 reputationPosted Aug 16, 2010, 1:01 PM EDT
0
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.
Ken Pendlebury5 reputationPosted Aug 17, 2010, 6:51 AM EDT
0
I am trying to communicate simple text-based commands to a seperate process that "listens" to the clipboard. Commands are decorated with special text to identify them as such. When the receiving app detects their presence, it removes them from the clipboard and processes the requested command.
Thanks.
Can you explain what the receiving app does, an example of the message sent via clipboard and what the app does when it sees the clipboard message. is the app a windows based app with text boxes, combo boxes etc and do the messages affect these, if so how.
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 Pendlebury5 reputationPosted Aug 16, 2010, 1:01 PM EDT
theLizardRank #1,079 · 1.8K reputationPosted Aug 16, 2010, 9:40 PM EDT
Ken Pendlebury5 reputationPosted Aug 17, 2010, 6:51 AM EDT
Thanks.
theLizardRank #1,079 · 1.8K reputationPosted Aug 17, 2010, 1:44 PM EDT
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.