Converting MSDN C++ functions to MQL4 functions! WINAPI, User32.dll... - page 2

 
TomQL:

HCURSOR Type.


Because I'd like to change the shape of the cursor once I am dragging a Rectangle or Canvas. 


It's an handle, so an int. Please read WinAPI documentation.
SetCursor function (Windows)
  • msdn.microsoft.com
Sets the cursor shape. Syntax Parameters hCursor [in, optional] A handle to the cursor. The cursor must have been created by the CreateCursor function or loaded by the LoadCursor or LoadImage function. If this parameter is NULL, the cursor is removed from the screen. Return value The return value is the handle to the previous cursor, if there...
 
Alain Verleyen:
It's an handle, so an int. Please read WinAPI documentation.

Does that mean that we can't use it in MQL4 ?

What does a handle mean ?

I've read the documentation but I understood pretty much nothing.

 

I did write an int before it and I saw no error. But the cursor didn't change !

 
Alain Verleyen:
It's an handle, so an int. Please read WinAPI documentation.

I also tried this but it gave the error :'WINAPI' - semicolon expected 


#import "user32.dll"


int HCURSOR WINAPI SetCursor(_In_opt_ HCURSOR hCursor);


#import



 
TomQL:

I did write an int before it and I saw no error. But the cursor didn't change !

Please show source code for a simple complete test case which did not work for you.

 
Stanislav Korotky:

Please show source code for a simple complete test case which did not work for you.


Actually I don't have a complete test case. I am just trying to make one thing work, which is mouse cursor change. 


I put this code on the Indicator's OnChartEvent(..):    

SetCursor ("C:\Windows\Cursors\aero_link_im.cur");


I'm very new to using external dll. The problem here is that the declaration of the handler doesn't work.


Can you please give me an idea of what should I do or what am I doing wrong ?

 

You should do something like this.

cursor.mqh

#import "user32.dll"
int SetCursor(int hCursor);
int LoadImageW(int instance, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad);
#import

#define IMAGE_CURSOR 2

#define LR_DEFAULTSIZE 0x00000040
#define LR_LOADFROMFILE 0x00000010

cursor.mq4

#property strict

const string Image = "path-to-your-program\\MQL4\\Images\\busy_i.cur";

#include <cursor.mqh>

void OnStart()
{
  int h = LoadImageW(0, Image,
    IMAGE_CURSOR,
    0,
    0,
    LR_DEFAULTSIZE | LR_LOADFROMFILE);
  Print("h=", h);
  int p = SetCursor(h);
  Print("p=", p);
  while(!IsStopped())
  {
    Sleep(1000);
  }
  SetCursor(p);
}

It works in the sense that correct handles are received.

The cursor is not displayed though, because the terminal is setting it on every mouse event according to its internal rules, hense your custom cursor is instantly overriden. So, i think, your requirement is not doable. You should reconsider your task and find another approach for what you want to achieve.

 
TomQL:

How about passing some types of parameters from Winapi to MQL4 ? How do we do that ?


Can you invoke MQL4 from Winapi?

 
Ex Ovo Omnia:

Can you invoke MQL4 from Winapi?


No, you can't invoke MQL4 from winapi, but you can have output parameters and return values in DLL functions. An example of this approach are indicators with BPNN.dll (Russian [more complete], English). See how some arrays are filled in the DLL and received in MQL.

#import "BPNN.dll"
string Train(
   double &inpTrain[], // Input training data (1D array carrying 2D data, old first)
   double &outTarget[],// Output target data for training (2D data as 1D array, oldest 1st)
   double &outTrain[], // Output 1D array to hold net outputs from training
   int    ntr,        // # of training sets
   int    UEW,        // Use Ext. Weights for initialization (1=use extInitWt, 0=use rnd)
   double &extInitWt[],// Input 1D array to hold 3D array of external initial weights
   double &trainedWt[],// Output 1D array to hold 3D array of trained weights
   int    numLayers,  // # of layers including input, hidden and output
   int    &lSz[],      // # of neurons in layers. lSz[0] is # of net inputs
   int    AFT,        // Type of neuron activation function (0:sigm, 1:tanh, 2:x/(1+x))
   int    OAF,        // 1 enables activation function for output layer; 0 disables
   int    nep,        // Max # of training epochs
   double maxMSE      // Max MSE; training stops once maxMSE is reached
);

string Test(
   double &inpTest[],  // Input test data (2D data as 1D array, oldest first)
   double &outTest[],  // Output 1D array to hold net outputs from training (oldest first)
   int    ntt,        // # of test sets
   double &extInitWt[],// Input 1D array to hold 3D array of external initial weights
   int    numLayers,  // # of layers including input, hidden and output
   int    &lSz[],      // # of neurons in layers. lSz[0] is # of net inputs
   int    AFT,        // Type of neuron activation function (0:sigm, 1:tanh, 2:x/(1+x))
   int    OAF         // 1 enables activation function for output layer; 0 disables
);
#import

Of course, this highly depend on DLL. In case of winapi, you are limited to functions available. In short, the answer is - it depends.

Edited, because during pasting the forum ate amperands.

 
Ex Ovo Omnia:

however you cannot pass some types of parameters from MQL4 to Winapi (e.g. string arrays, callback functions).

There's very little data which is impossible to exchange with Win32. For example, my socket code processes the complex hostent structure supplied by Winsock. It would similarly be possible to build a hostent and pass it to Winsock if necessary. String arrays are also perfectly possible. It's just not easy.

No, you indeed can't have callbacks into MQL code from Win32. No way of calling a function such as EnumWindows().

Since this forum is now a sub-area of mql5.com, I'll mention that MT5 introduces major headaches in this area because, in a 64-bit application, many Windows handles become 8 bytes long rather than 4. You need separate DLL imports for MQL5 code depending on whether it is running as 32-bit or 64-bit. And therefore you potentially need separate paths of run-time execution around each DLL call. The socket code mentioned above provides an example of this, basically having two different paths of execution in many areas for 64-bit MQL5 vs 32-bit MQL5 or MQL4.