solved

 

solved!

 

Your declarations are incorrect. Pointers should be long on a 64 bits software.

#import "user32.dll"
   int OpenClipboard(int hOwnerWindow);
   int SetClipboardData(int Format, int hMem);
#import

Use the ones provided with the platform :

#include <WinAPI\winuser.mqh>
#include <WinAPI\winbase.mqh>
 
#include <WinAPI\winuser.mqh>
#include <WinAPI\winbase.mqh>

#define GMEM_MOVEABLE   0x0002
#define CF_UNICODETEXT  13
#import "kernel32.dll"
string                 lstrcpyW(PVOID string1,const string string2);
#import
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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);
         HANDLE hMem=GlobalAlloc(GMEM_MOVEABLE,lnString*2+2);
         if(hMem!=0)
           {
            // Try locking the memory, so that we can copy into it
            PVOID 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);
  }
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   string test="It works";
   CopyTextToClipboard(test);
  }
 
noChangepromise:
copy to clipboard on MT5 platform

Out of curiosity, what text do you copy to the clipboard?

That is, what could I use this code for?

 
Alain Verleyen:

big thx!

 
noChangepromise:

big thx!

An answer to Anthony would be nice.
 

Hi Alain,

could you please post a MT5 version of the code that copies the text FROM clipboard to EA. Whatever code snippets

i could find don't work in MT5.

Thank you.

This is a piece of code I've come up with sofar, but it doesn't work, I am getting just a bunch of numbers instead of the clipboard text.

#include <WinAPI\winuser.mqh>
#include <WinAPI\winbase.mqh>

#define GMEM_MOVEABLE   0x0002
#define CF_UNICODETEXT  13
#import "kernel32.dll"
string                 lstrcpyW(PVOID string1,const string string2);
#import

#define CF_TEXT     1
#define CF_OEMTEXT  7

string GetTextFromClipboard()
{
   bool bReturnvalue = false;
   ushort clipString[265];
   ArrayInitialize(clipString,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).
         HANDLE hMem = GetClipboardData(CF_OEMTEXT);
         if(hMem != 0)
         {
            // Call GlobalLock so that to retrieve a pointer
            // to the data associated with the handle returned
            // from GetClipboardData.
            Print("Call GlobalLock to retrieve a pointer");
           //int ptrMem = GlobalLock(hMem);
            PVOID ptrMem=GlobalLock(hMem);
            if(ptrMem != 0)
            {
               lstrcpyW(clipString, ptrMem);

            }
            else
            {
               //Locking did not succeed
            }
            // Unlock the global memory.
            GlobalUnlock(hMem);
          }
          else
          {
            //Clipboard data is Text, but could not retrieve.
            Print("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( ShortArrayToString(clipString) );
}
 
Ct Aishah Che Muda:

I am using the ones provided with the platform:

#include <WinAPI\winuser.mqh>
#include <WinAPI\winbase.mqh>

#define GMEM_MOVEABLE   0x0002
#define CF_UNICODETEXT  13
#import "kernel32.dll"
string                 lstrcpyW(PVOID string1,const string string2);
#import

#define CF_TEXT     1
#define CF_OEMTEXT  7
 
noChangepromise:

solved!

Please return the Topic and your initial comment to what they used to be. The purpose of this isn't just to help you, but to share information and solutions to people in the future who come up with the same problem.

 
Alain Verleyen:

Thanks a lot, Alain ! Worked nicely.

 
bloxa69 #:

Hi Alain,

could you please post a MT5 version of the code that copies the text FROM clipboard to EA. Whatever code snippets

i could find don't work in MT5.

Thank you.

This is a piece of code I've come up with sofar, but it doesn't work, I am getting just a bunch of numbers instead of the clipboard text.

Hello

Did you find any solution ? 

Reason: