Copy string to clipboard

 

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

 
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);
}
 
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?
 

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.

 

Sya: . 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.

So change it.
  1. If the parameter is zero, have it look it up.
  2. If the parameter is not zero, use that.
 
JC:

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.)

Hello Friends, 

I used this code for copy text to clipboard in MQL4 and it was work very good,

Now I want to use in MQL5 but not working, 

error is "Memory allocated but not locked"

actually this line not working correctly :

            int ptrMem = GlobalLock(hMem);

            if (ptrMem != 0) {

can you help me please ? 

 
prg_mt4:

Hello Friends, 

I used this code for copy text to clipboard in MQL4 and it was work very good,

Now I want to use in MQL5 but not working, 

error is "Memory allocated but not locked"

actually this line not working correctly :

            int ptrMem = GlobalLock(hMem);

            if (ptrMem != 0) {

can you help me please ? 

Any luck ?

 

According to JC the proper fix for this needs to happen inside MT5 with the core code that does the automatic C++ code generation.

There might be a work around until that happens and if so, perhaps someone can post that workaround.

I don't know how long it will take for the MT5 developers to get this update implemented and released. In my 30+ years of being in software development with many different companies, it usually doesn't happen very fast. However, agile/scrum software development methodologies are now all the rage with corporations and if MT5 is using that model, an update might not take very long.

 
Ralph Freshour:

According to JC the proper fix for this needs to happen inside MT5 with the core code that does the automatic C++ code generation.

There might be a work around until that happens and if so, perhaps someone can post that workaround.

I don't know how long it will take for the MT5 developers to get this update implemented and released. In my 30+ years of being in software development with many different companies, it usually doesn't happen very fast. However, agile/scrum software development methodologies are now all the rage with corporations and if MT5 is using that model, an update might not take very long.

Please note that you are replying to a thread in the MT4 section, not MT5.

Metaquotes will certainly not be doing any such updates for MT4.

 
Hey, someone can solve this?
 
someone can solve this after this long years?
Reason: