What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » [solved] CreateThread/GetCallbackPtr issues

Pages: (2): « First [ 1 ] 2 » Last »
[solved] CreateThread/GetCallbackPtr issues
Author: Message:
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
O.P. [solved] CreateThread/GetCallbackPtr issues
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?

This post was edited on 12-25-2008 at 10:36 AM by Danny22.
12-24-2008 07:18 PM
Profile PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: CreateThread issues
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.
12-24-2008 07:29 PM
Profile E-Mail PM Find Quote Report
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
O.P. RE: CreateThread issues
I see. Thank you very much for your quick reply.
12-24-2008 07:32 PM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: CreateThread issues
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.
12-24-2008 07:41 PM
Profile PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: [solved] CreateThread issues
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...

This post was edited on 12-24-2008 at 07:50 PM by matty.
12-24-2008 07:46 PM
Profile E-Mail PM Find Quote Report
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
O.P. RE: [solved] CreateThread issues
Thanks, Eljay and matty.
Is it possible to accomplish this by moving the code into a DLL?

This post was edited on 12-24-2008 at 07:58 PM by Danny22.
12-24-2008 07:48 PM
Profile PM Find Quote Report
Eljay
Elite Member
*****

Avatar
:O

Posts: 2949
Reputation: 77
– / Male / –
Joined: May 2004
RE: [solved] CreateThread issues
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.
12-24-2008 08:12 PM
Profile PM Find Quote Report
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
O.P. RE: RE: [solved] CreateThread issues
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. :)
12-24-2008 08:29 PM
Profile PM Find Quote Report
Mnjul
forum super mod
******

Avatar
plz wub me

Posts: 5396
Reputation: 58
– / Other / Flag
Joined: Nov 2002
Status: Away
RE: [solved] CreateThread issues
matty and Eljay: you both are both wrong and right :p
CookieRevised's reply to [Resource] Subclassing ActiveXObjects (and [Release] TabbedStatusIcon too!)
12-24-2008 08:39 PM
Profile PM Web Find Quote Report
Danny22
New Member
*


Posts: 11
Joined: Dec 2008
O.P. RE: [solved] CreateThread issues
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!

This post was edited on 12-25-2008 at 12:23 AM by Danny22.
12-25-2008 12:19 AM
Profile PM Find Quote Report
Pages: (2): « First [ 1 ] 2 » Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On