DLL function string parameter, only the first character sent

 

Hello guys,

I have the same problem with one single character.

I'm using one dll to read/write clipboard which has these exports: 

BOOL CopyTextToClipboard(LPCTSTR strText)

Puts text onto the Windows clipboard. Returns TRUE if successful or FALSE if it fails (I don't see why it would ever fail).

 

LPCTSTR GetTextFromClipboard()

Gets the contents of the Windows clipboard if there is text available, returns an empty string ("") if not there. 

 

I tried all combinations I could think of, I read the forum and searched the net, but now I'm stuck. And I don't have the source of dll. Just the result and description of functions.

What type combination should I use to get and set a whole text?

This is what is written right now in my code (I'm aware of the fact LPCTSTR is a pointer, but I don't know either how to give it a pointer, or why still it works and writes the first character into the clipboard.

#import "IRClipboardFunctions.dll"
//Returns TRUE or FALSE depending on whether or not there is text available on the Windows clipboard.
bool IsTextOnClipboard();

//Puts text onto the Windows clipboard. Returns TRUE if successful or FALSE if it fails (I don't see why it would ever fail).
bool CopyTextToClipboard(string strText);

//Gets the contents of the Windows clipboard if there is text available, returns an empty string ("") if not there.
string GetTextFromClipboard();

#import

 Can someone help me with this?

Thank you in advance. 

---- 

ps.

I've posted here too: https://www.mql5.com/en/forum/348

But because I didn't find the topic in General forum, I've created a new topic on this.

Sorry for double post, but I'm not sure anyone will read that topic now. 

BUG: Imported DLL Functions Receive Only First Character of Strings Passed By Value (MQL Build 237)
  • www.mql5.com
BUG: Imported DLL Functions Receive Only First Character of Strings Passed By Value (MQL Build 237).
 

Look at this code (https://www.mql5.com/en/code/104).

It might helps you.

Clipboard
  • votes: 11
  • 2010.04.27
  • MSDN | English Russian Spanish Portuguese
  • www.mql5.com
The script gets contents from the Windows Clipboard.
 
alexvd:

Look at this code (https://www.mql5.com/en/code/104).

It might helps you.

Thank you Alex.

I already did something. I don't think it's really stable, but I put here the code of what I did:

const int GHND = 0x0042;
const int CF_TEXT = 1;
const int CF_UNICODETEXT = 13;

#import "user32.dll"
bool IsClipboardFormatAvailable(int wFormat);
bool OpenClipboard(int hwnd);
int GetClipboardData(int uFormat); //1 = CF_TEXT = ANSI, 13 = CF_UNICODETEXT
int SetClipboardData(int uFormat, int hMem); //1 = CF_TEXT = ANSI, 13 = CF_UNICODETEXT
bool EmptyClipboard();
bool CloseClipboard();
int GetAncestor(int hWnd,int gaFlags);
#import "kernel32.dll"
int GlobalLock(int hMem);
bool GlobalUnlock(int hMem);
int GlobalAlloc(int wFlags, int dwBytes);
int GlobalFree(int hMem);
//void CopyMemory(int Destination, int Source, int Length);
//void MoveMemory(int Destination, int Source, int Length);
string lstrcpyW(int lpString1, string &lpString2);
string lstrcatW(int dst,string src);
#import
 
#import "shell32.dll"
int ShellExecuteW(int hwnd,const string Operation,const string File,const string Parameters,const string Directory,int ShowCmd);
#import 
//      Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
//         ByVal lpString2 As Any) As Long
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/*
int OnStart()
  {
   string s = "IONESCU MIHAIÉ";
   int code = writeClipboard(s,CF_TEXT);
   string clip= readClipboard(CF_TEXT);
   
// translate ANSI to UNICODE
   MessageBox("Cb Unicode: \n"+clip,"Clipboard");
   //string rez = ansiToUnicode(clip);
   //MessageBox("Cb Ansi: \n"+rez,"Clipboard");
   //s = "Ionescu Mihai Florin";
   //MessageBox("new s= \n"+s,"Clipboard");
   
   return(0);
  }
*/  
//+------------------------------------------------------------------+
string readClipboard(int CF){ //CF_TEXT=1, CF_UNICODETEXT=13
   string clip = "-1";
   int hMain=GetAncestor(ChartGetInteger(ChartID(),CHART_WINDOW_HANDLE),2);

   if(OpenClipboard(hMain))
     {
      int hglb=GetClipboardData(CF); 
      if(hglb!=0)
        {
         int lptstr=GlobalLock(hglb);

         if(lptstr!=0) { 
            clip=lstrcatW(lptstr,""); 
            GlobalUnlock(hglb); 
         }
        }
      CloseClipboard();
     }
   return clip;  
}

int writeClipboard(string clip, int CF){ //CF_TEXT=1, CF_UNICODETEXT=13
   int ret = 0;
   int hGlobalMemory, lpGlobalMemory, hClipMemory, X;
   //Allocate movable global memory.
   hGlobalMemory = GlobalAlloc(GHND, sizeof(string) * (StringLen(clip) + 1));
   
   //Lock the block to get a far pointer to this memory.
   lpGlobalMemory = GlobalLock(hGlobalMemory);
   
   //Copy the string to this global memory.
   lpGlobalMemory = lstrcpyW(lpGlobalMemory, clip);
   
   //Unlock the memory.
   if (GlobalUnlock(hGlobalMemory) != 0) {
      MessageBox("Could not unlock memory location. Copy aborted.","Clipboard");
      GlobalFree(lpGlobalMemory);
      return -3; //code for unlocking problem
   }
        
   int hMain=GetAncestor(ChartGetInteger(ChartID(),CHART_WINDOW_HANDLE),2);

   if(OpenClipboard(hMain))
     {
      X = EmptyClipboard();
      hClipMemory = SetClipboardData(CF, hGlobalMemory);
      
      if (CloseClipboard() == 0) {
         MessageBox("Could not close Clipboard", "Clipboard");
         return -2; //code for closing problem
      }
     }
   else {
      GlobalFree(lpGlobalMemory);
      return -1; //code for opening problem  
   } 
   GlobalFree(lpGlobalMemory);
   return 1;  //code for success
}
   
string ansiToUnicode(string clip) {
   uchar chA; 
   ushort chW; 
   string rez;
   for(int i=0; i<StringLen(clip); i++)
     {
      chW=StringGetCharacter(clip, i);
      chA=uchar(chW&255); rez=rez+CharToString(chA);
      chA=uchar(chW>>8&255); rez=rez+CharToString(chA);
     }
   return rez;
}

 

 
Did you find a solution for this ?