Get text from clipboard

 

Hello 

I use this script for get text from windows clipboard.

When copy "HELLO" to clipboard, function returns a big number like 19522312522325488

How can I fix this code or convert this number to "HELLO"?

#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
int OnStart()
  {
   MessageBox(GetTextFromClipboard()," ");
   return(0);
  }
string GetTextFromClipboard()
{
   bool bReturnvalue = false;
   ushort clipString[265];
   ArrayInitialize(clipString,0);
   if (OpenClipboard(0) != 0) 
   {
      if (IsClipboardFormatAvailable(CF_TEXT) != 0
      || IsClipboardFormatAvailable(CF_OEMTEXT) != 0)
      {
         HANDLE hMem = GetClipboardData(CF_OEMTEXT);
         if(hMem != 0)
         {
            PVOID ptrMem=GlobalLock(hMem);
            if(ptrMem != 0)
            {
               lstrcpyW(clipString, ptrMem);
            }
            else
            {
               //Locking did not succeed
            }
            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.");
      }
      CloseClipboard();
   }

   return( ShortArrayToString(clipString) );
}
//+------------------------------------------------------------------+

Thanks

 

With the help of Alain's code

https://www.mql5.com/en/forum/321205

and chatgpt :) this works on MT5 for me (version 5 build 3559):


#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);
string                 lstrcpyW(string string1,PVOID string2);
#import

string GetTextFromClipboard()
{
   string Text = "";

   // Try grabbing ownership of the clipboard 
   if(OpenClipboard(0)!=0)
   {
      // Try getting the clipboard data
      HANDLE hClipboardData  = GetClipboardData(CF_UNICODETEXT);
      if(hClipboardData  != 0)
      {
         // Try locking the memory, so that we can copy from it
         PVOID ptrMem = GlobalLock(hClipboardData );
         if(ptrMem != 0)
         {
            // Copy the string from the global memory
            Text = lstrcpyW(Text, ptrMem);

            // Release ownership of the global memory
          GlobalUnlock(hClipboardData);
         }
      }
      // Always release the clipboard, even if the copy failed 
      CloseClipboard();
   }

   return (Text);
}

void OnStart()
{
   string test = GetTextFromClipboard();
}
solved
solved
  • 2019.08.30
  • www.mql5.com
Expert Advisors and Automated Trading: solved
 

I solved this problem with this change:

int GetClipboardData(int uFormat);

=>

long GetClipboardData(int uFormat);