How to pass function pointer parameter into CreateThread() wrapper function as exposed in processthreadsapi.mqh

 
I'm experimenting with the multi-threading code in a Script environment.

I have the following code.

#include <WinAPI\windef.mqh>
#include <WinAPI\processthreadsapi.mqh>
#include <WinAPI\handleapi.mqh>

#import "kernel32.dll"
int WaitForSingleObject(HANDLE hHandle, int dwMilliseconds);
#import

typedef int (*ThreadFuncPtr)();

int ThreadFunction() {
   Print("called from ThreadFunction: ", GetCurrentThreadId());
   return 0;
}

void OnStart() {
   HANDLE thread1, thread2;
   int threadId1, threadId2;
   
   ThreadFuncPtr funcPtr = ThreadFunction;
   
   thread1 = CreateThread(NULL, 0, (PVOID)funcPtr, NULL, 0, threadId1);   // <------- compile error "'(long)' - invalid cast operation"
   thread2 = CreateThread(NULL, 0, (void*)funcPtr, NULL, 0, threadId2);   // <------- compile error "'(void*)' - invalid cast operation" and "parameter conversion not allowed"
   
   WaitForSingleObject(thread1, UINT_MAX);
   WaitForSingleObject(thread2, UINT_MAX);
   
   CloseHandle(thread1);
   CloseHandle(thread2);
   
   Print("All done");
}

Also I have to typedef ThreadFunction in order to satisfy compilation a little more.
If I didn't use typedef a type as a function pointer then pass into CreateThread() function, then error of

'ThreadFunction' - function call missing, open parenthesis expected

will occur.

The only compile error now is those two lines.
Any suggestion on how to properly pass thread function into CreateThread() function here?

 
Wasin Thonkaew:
I'm experimenting with the multi-threading code in a Script environment.

I have the following code.

Also I have to typedef ThreadFunction in order to satisfy compilation a little more.
If I didn't use typedef a type as a function pointer then pass into CreateThread() function, then error of

will occur.

The only compile error now is those two lines.
Any suggestion on how to properly pass thread function into CreateThread() function here?

Hi Wasin,

Before I realised that doing what you trying to do does not work, I spent a few weeks trying to find a way, but sadly not doable.  The reason for this is that MQL5 does not have real pointers like C/C++.  MQL5 has pseudo pointers - so the win32 API function CreateThread() will not work.

All the best,

Shep

Reason: