Download MetaTrader 5
To add comments, please log in or register
Join MQL5 Cloud Network affiliate program and make money!
Sya
56
Sya 2014.11.01 04:11 

Hello,


for to copy text to clipboard, I tried the solution of ricx on https://forum.mql4.com/30795

It's running through without error, but in the clipboard there only is the first character of the input string. I tried to find an explanation and a solution and then replaced "CF_TEXT" with "CF_UNICODETEXT" but without success.

Microsoft tells further not to use lstrcpyA anymore, but StringCchCopy. With this modification there was no success either.


Do you have any idea what would work?

My goal is to copy the youngest OrderNo within a chart to the clipboard for quick insert into the parameters (input) of a script.


Thanks for reading

Sya

JC
1395
JC 2014.11.02 18:09  
Sya:

for to copy text to clipboard, I tried the solution of ricx on https://forum.mql4.com/30795

The old code works without an error message but only 1 character is copied because MT4 supplies a Unicode string when the code and the Windows API are expecting an Ascii string.

The code needs to be updated not only (a) to use CF_UNICODETEXT, but also (b) to use a Unicode rather than Ascii string-copying function, and (c) to allocate a memory buffer which is the number of characters in the string times 2, because each character now occupies two bytes rather than 1.  (Strictly speaking, characters may occupy more than 2 bytes, but this is very unlikely to occur in real life.)

#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 {
               // Memory 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);
}
JC
1395
JC 2014.11.02 18:11  
Sya:

My goal is to copy the youngest OrderNo within a chart to the clipboard for quick insert into the parameters (input) of a script.

... But why can't the script look up the order number, rather than requiring it to be pasted in?
Sya
56
Sya 2014.11.05 21:59  

Thanks, jjc, for all that effort to declare it to me in detail. Think, I understood.

I'll test it and then report further.

You ask, why the script can't do the work itself. It's because the OrderNo is part of the input parameters. Second reason is, that most of time the youngest OrderNo should be used, but not always. So I can't leave it to the script to decide.

/
To add comments, please log in or register