Shoutbox

[solved] CreateThread/GetCallbackPtr issues - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [solved] CreateThread/GetCallbackPtr issues (/showthread.php?tid=87948)

[solved] CreateThread/GetCallbackPtr issues by Danny22 on 12-24-2008 at 07:18 PM

Hello,

I'm having issues when trying to use the CreateThread WinAPI, and I hope someone here can assist me.
Seems like the callback function is never called for some reason, as the message box never shows, but CreateThread doesn't seem to fail.

Example code:

code:
var thread_id = Interop.Allocate(4);
var func = Interop.GetCallbackPtr("ExampleCallback");
var thread = Interop.Call("kernel32.dll", "CreateThread", null, 0, func, null, 0, thread_id);
if (!thread)
{
    Debug.Trace("Could not create thread.");
    return;
}

Callback function:
code:
function ExampleCallback(Param)
{
    Interop.Call("user32.dll", "MessageBoxW", 0, "It works!", "", 0);
    return 1;
}

Any ideas?
RE: CreateThread issues by matty on 12-24-2008 at 07:29 PM

The GetCallbackPtr has to actually be a callback not one just defined by the user.

For instance MonitorEnumProc.

The reason for this is because Patchou had to define these functions within Messenger Plus! itself therefore it will not allow you to define a function and get a pointer to it.


RE: CreateThread issues by Danny22 on 12-24-2008 at 07:32 PM

I see. Thank you very much for your quick reply.


RE: CreateThread issues by Eljay on 12-24-2008 at 07:41 PM

quote:
Originally posted by matty
The reason for this is because Patchou had to define these functions within Messenger Plus! itself therefore it will not allow you to define a function and get a pointer to it.

Not quite sure what you are on about here matty :P GetCallbackPtr was meant for exactly that purpose really.

However, the reason the OP's code doesn't work is that CreateThread needs an asynchronous callback to work, and unfortunately, Plus! only supports synchronous callbacks due to technical limitations of the scripting engine.
RE: [solved] CreateThread issues by matty on 12-24-2008 at 07:46 PM

quote:
Originally posted by Eljay
However, the reason the OP's code doesn't work is that CreateThread needs an asynchronous callback to work, and unfortunately, Plus! only supports synchronous callbacks due to technical limitations of the scripting engine.
Wrong it fully supports asynchronous call backs. However because they are defined within Messenger Plus! each call to the same function will return the same pointer. Therefore 2 scripts calling the same pointer can cause issues. You cannot get a callback pointer to a function that hasn't been defined within Messenger Plus!.

We will discuss this later on MSN :P


Looks like I am talking out of my ass again... I could have swore it was defined internally in Messenger Plus!... if not what am I thinking of...
RE: [solved] CreateThread issues by Danny22 on 12-24-2008 at 07:48 PM

Thanks, Eljay and matty.
Is it possible to accomplish this by moving the code into a DLL?


RE: [solved] CreateThread issues by Eljay on 12-24-2008 at 08:12 PM

The way I see it, Plus! has one internal (native) function that is used as a callback. When you call GetCallbackPtr, Plus! stores the name of the function and the script engine object for the script that called it. This is why the callback must be used immediately and synchronously, because other scripts calling GetCallbackPtr would screw everything up.

Patchou did post somewhere a more technical explanation as to why he can't support asynchronous callbacks, but I can't find it :P

quote:
Originally posted by Danny22
I guess I can accomplish this by moving the code into a DLL?
Thanks Eljay and matty.

Most likely yes, maybe if you explained what you were trying to accomplish we can help out.
RE: RE: [solved] CreateThread issues by Danny22 on 12-24-2008 at 08:29 PM

quote:
Originally posted by Eljay
Most likely yes, maybe if you explained what you were trying to accomplish we can help out.

Thanks. I appreciate it.
I installed VC++ (Express 2008) and I'll give it a try.

My code has a long loop and calls to the Sleep WinAPI to control the speed. The sleep/delay should be pretty quick (10 ms), so I cannot use Plus! timers as I see it.
However, this would stall everything else in WLM/Plus!, so I need it to run in a different thread.
It is supposed to do smooth animation/movement on a custom window.

I am not sure how you can help but I'll let you know how it goes. :)
RE: [solved] CreateThread issues by Mnjul on 12-24-2008 at 08:39 PM

matty and Eljay: you both are both wrong and right :p
CookieRevised's reply to [Resource] Subclassing ActiveXObjects (and [Release] TabbedStatusIcon too!)


RE: [solved] CreateThread issues by Danny22 on 12-25-2008 at 12:19 AM

I gave it a shot and made a simple example DLL.
It exports a function which calls CreateThread (the callback function is in the DLL, too, of course), which I can call in my script. Seems to work well. :D
Thanks, everyone!


RE: [solved] CreateThread issues by Matti on 12-25-2008 at 09:32 AM

quote:
Originally posted by Mnjul
matty and Eljay: you both are both wrong and right :p
CookieRevised's reply to [Resource] Subclassing ActiveXObjects (and [Release] TabbedStatusIcon too!)
So, if I understand this correctly, Plus! declares a number of static functions which all accept a certain number of numeric arguments. Then, based on the function passed through GetCallbackPtr, Plus! determines the amount of arguments the passed JScript function accepts and returns the pointer of the static function with the same amount of arguments...

This means, for two scripts attempting to use a function pointer for an asynchronous call, two scripts requesting a function pointer with the same amount of arguments are much more likely to interrupt each other than two scripts requesting a function pointer with a different amount of arguments.

Am I right about this? Is this what we should know about using GetCallbackPtr? Or did I go wrong somewhere? :P
RE: [solved] CreateThread/GetCallbackPtr issues by Pinecone on 12-26-2008 at 10:15 AM

I could be wrong, but my understanding about knowing if a function was called with the right number of arguments or not, is checking for a stack imbalance after the call, hence why the plus documentation says to only call functions with stdcall convention.

Edit: i just realised that what i said doesn't have much relevance... :P