Directly copying into the clipboard is possible only with some direct calls into the windows API: http://www.codeproject.com/KB/clipboard/archerclipboard1.aspx. These naked windows API calls always look like it is really great "fun" using them, especially from mql4 which has not the ability to define data structures :-/
The goal is to... "take screen shot" (done) and "send it to ftp server" (done) then "copy file link to windows clipboard" (problem)
#import "kernel32.dll" int GlobalAlloc(int Flags, int Size); int GlobalLock(int hMem); int GlobalUnlock(int hMem); int GlobalFree(int hMem); int lstrcpyA(int ptrhMem, string Text); #import #import "user32.dll" int OpenClipboard(int hOwnerWindow); int EmptyClipboard(); int CloseClipboard(); int SetClipboardData(int Format, int hMem); #import #define GMEM_MOVEABLE 2 #define CF_TEXT 1 // Copies the specified text to the clipboard, returning true if successful bool CopyTextToClipboard(string Text) { bool bReturnvalue = false; // Try grabbing ownership of the clipboard if (OpenClipboard(0) != 0) { // Try emptying the clipboard if (EmptyClipboard() != 0) { // Try allocating a block of global memory to hold the text int lnString = StringLen(Text); int hMem = GlobalAlloc(GMEM_MOVEABLE, lnString + 1); if (hMem != 0) { // Try locking the memory, so that we can copy into it int ptrMem = GlobalLock(hMem); if (ptrMem != 0) { // Copy the string into the global memory lstrcpyA(ptrMem, Text); // Release ownership of the global memory (but don't discard it) GlobalUnlock(hMem); // Try setting the clipboard contents using the global memory if (SetClipboardData(CF_TEXT, hMem) != 0) { // Okay bReturnvalue = true; } else { // Failed to set the clipboard using the global memory GlobalFree(hMem); } } else { // Meemory allocated but not locked GlobalFree(hMem); } } else { // Failed to allocate memory to hold string } } else { // Failed to empty clipboard } // Always release the clipboard, even if the copy failed CloseClipboard(); } else { // Failed to open clipboard } return (bReturnvalue); }
there are many ways to achieve "the clipboard" goals... wow... :D
again, thanks guys.
The following code copy text to clipboard.
I need an opposite function.
Could you help me copy text from clipboard?
string CopyTextFromClipboard()
{
}
The following code is by jjc.
jjc 2010.03.25 16:06#import "kernel32.dll" int GlobalAlloc(int Flags, int Size); int GlobalLock(int hMem); int GlobalUnlock(int hMem); int GlobalFree(int hMem); int lstrcpyA(int ptrhMem, string Text); #import #import "user32.dll" int OpenClipboard(int hOwnerWindow); int EmptyClipboard(); int CloseClipboard(); int SetClipboardData(int Format, int hMem); #import #define GMEM_MOVEABLE 2 #define CF_TEXT 1 // Copies the specified text to the clipboard, returning true if successful bool CopyTextToClipboard(string Text) { bool bReturnvalue = false; // Try grabbing ownership of the clipboard if (OpenClipboard(0) != 0) { // Try emptying the clipboard if (EmptyClipboard() != 0) { // Try allocating a block of global memory to hold the text int lnString = StringLen(Text); int hMem = GlobalAlloc(GMEM_MOVEABLE, lnString + 1); if (hMem != 0) { // Try locking the memory, so that we can copy into it int ptrMem = GlobalLock(hMem); if (ptrMem != 0) { // Copy the string into the global memory lstrcpyA(ptrMem, Text); // Release ownership of the global memory (but don't discard it) GlobalUnlock(hMem); // Try setting the clipboard contents using the global memory if (SetClipboardData(CF_TEXT, hMem) != 0) { // Okay bReturnvalue = true; } else { // Failed to set the clipboard using the global memory GlobalFree(hMem); } } else { // Meemory allocated but not locked GlobalFree(hMem); } } else { // Failed to allocate memory to hold string } } else { // Failed to empty clipboard } // Always release the clipboard, even if the copy failed CloseClipboard(); } else { // Failed to open clipboard } return (bReturnvalue);
}
The following code copy text to clipboard.
I need an opposite function.
Could you help me copy text from clipboard?
string CopyTextFromClipboard()
{
}
The following code is by jjc.
jjc 2010.03.25 16:06}
First, I know this post is old, but as I was looking for a way to copy to clipboard, I bumped into the code on this thread.
Since it helped me a lot with my program, I wanted to contribute back in order to close this thread properly without an open question.
Thanks to https://www.mql5.com/en/forum/153674, I managed to work with new version of Unicode text calls in Metatrader 4, as the above function does not work from build 600+.
So, to answer @Ray Kim's question, here it is fully (notice the lstrcpyW function, which works in both ways, from text to pointer, and from retrieved pointer to text):
//+------------------------------------------------------------------+ //| This piece of code will copy any text we want to the clipboard | //+------------------------------------------------------------------+ #import "kernel32.dll" int GlobalAlloc(int Flags, int Size); int GlobalLock(int hMem); int GlobalUnlock(int hMem); int GlobalFree(int hMem); int lstrcpyW(int ptrhMem, string Text); #import #import "user32.dll" int OpenClipboard(int hOwnerWindow); int EmptyClipboard(); int CloseClipboard(); int SetClipboardData(int Format, int hMem); #import #define GMEM_MOVEABLE 2 #define CF_UNICODETEXT 13 // Copies the specified text to the clipboard, returning true if successful bool CopyTextToClipboard(string Text) { bool bReturnvalue = false; // Try grabbing ownership of the clipboard if (OpenClipboard(0) != 0) { // Try emptying the clipboard if (EmptyClipboard() != 0) { // Try allocating a block of global memory to hold the text int lnString = StringLen(Text); int hMem = GlobalAlloc(GMEM_MOVEABLE, lnString * 2 + 2); if (hMem != 0) { // Try locking the memory, so that we can copy into it int ptrMem = GlobalLock(hMem); if (ptrMem != 0) { // Copy the string into the global memory lstrcpyW(ptrMem, Text); // Release ownership of the global memory (but don't discard it) GlobalUnlock(hMem); // Try setting the clipboard contents using the global memory if (SetClipboardData(CF_UNICODETEXT, hMem) != 0) { // Okay bReturnvalue = true; } else { // Failed to set the clipboard using the global memory GlobalFree(hMem); } } else { // Meemory allocated but not locked GlobalFree(hMem); } } else { // Failed to allocate memory to hold string } } else { // Failed to empty clipboard } // Always release the clipboard, even if the copy failed CloseClipboard(); } else { // Failed to open clipboard } return (bReturnvalue); } //+------------------------------------------------------------------+ //| This piece of code will return the text from clipboard | //+------------------------------------------------------------------+ #import "kernel32.dll" int GlobalLock(int hMem); int GlobalUnlock(int hMem); int lstrcpyW(char& Text[], int ptrMem); #import #import "user32.dll" int OpenClipboard(int hOwnerWindow); int CloseClipboard(); int GetClipboardData(int Format); bool IsClipboardFormatAvailable(uint format); #import #define CF_TEXT 1 #define CF_OEMTEXT 7 string GetTextFromClipboard() { bool bReturnvalue = false; char cData[256]; ArrayInitialize(cData,0); // Test to see if we can open the clipboard first. if (OpenClipboard(0) != 0) { if (IsClipboardFormatAvailable(CF_TEXT) != 0 || IsClipboardFormatAvailable(CF_OEMTEXT) != 0) { // Retrieve the Clipboard data (specifying that // we want ANSI text (via the CF_TEXT value). int hMem = GetClipboardData(CF_TEXT); if(hMem != 0) { // Call GlobalLock so that to retrieve a pointer // to the data associated with the handle returned // from GetClipboardData. int ptrMem = GlobalLock(hMem); if(ptrMem != 0) { // Set a local string variable to the data lstrcpyW(cData, ptrMem); } else { //Locking did not succeed } // Unlock the global memory. GlobalUnlock(hMem); } else { //Clipboard data is Text, but could not retrieve. } } else { Print("There is no text (ANSI) data on the Clipboard."); } // Finally, when finished I simply close the Clipboard // which has the effect of unlocking it so that other // applications can examine or modify its contents. CloseClipboard(); } return(CharArrayToString(cData)); }
First, I know this post is old, but as I was looking for a way to copy to clipboard, I bumped into the code on this thread.
Since it helped me a lot with my program, I wanted to contribute back in order to close this thread properly without an open question.
For completeness... There's also a relatively recent topic about copying images to the clipboard: https://www.mql5.com/en/forum/192231

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
The goal is to... "take screen shot" (done) and "send it to ftp server" (done) then "copy file link to windows clipboard" (problem)
This is the code i use, for the script:
:'(