Using Callbacks with externals to Rev

Are you developing an External using the LiveCode Externals SDK?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Using Callbacks with externals to Rev

Post by n.allan » Fri Jul 11, 2008 8:32 am

My external hooks into an SDK on Win32
The C++ SDK has a function:

SetEventCallack(callback);

//The prototype for the callback is:
int (*eventhandler)(int hwnd,int msg,int wp,int lp);
//I can get hwnd using the windowID property in rev

When using my external from a rev stack window, the C++ sdk seems to trap keyboard and mouse messages so this event callback sent to rev could be just what I need.

Has anyone used Callbacks from an external or even SendCardMesage in the externals sdk? Could anyone at rev enlighten me as to how to use them?

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Post by trevordevore » Fri Jul 11, 2008 1:27 pm

I've used SendCardMessage a couple of times. Below you will find a handler based off of an example Tuviah Snyder passed along to me a few years back.

This example sends 1 parameter with the message which is why the global is created. The reason the parameter value is stored in a global is so that any quotes or commas in the value of the parameter being passed don't mess things up (send command evaluate everything you pass to it). Right now the code resets gDispatchRevolutionMessage to 0 but I think it could probably just delete the global using 'delete global gDispatchRevolutionMessage'.

Since you are looking to handle a callback in the external you could just make a call to DispatchRevolutionMessage in your callback handler.

Example:

Code: Select all

DispatchRevolutionMessage("SomethingChanged", "parameter 1 value");

Code: Select all

// Sends a message with variable to Revolution.  The variable will be handled
// for commas and quotes.
//
// Thanks to Tuviah Snyder
//
void DispatchRevolutionMessage(char *messagename, char *tmessage)
{
	int			retvalue = 0;
	char		mcmessage[1024];
	if (!messagename || !tmessage) return;

	SetGlobal("gDispatchRevolutionMessage",tmessage,&retvalue);
	snprintf(mcmessage, 1024, "global gDispatchRevolutionMessage;try;send \"%s gDispatchRevolutionMessage\" to current card of stack the topstack;catch errno;end try;put 0 into gDispatchRevolutionMessage",messagename);
    if (strlen(mcmessage) < 1023) SendCardMessage(mcmessage, &retvalue);
}
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Sat Jul 12, 2008 1:27 pm

Thanks for the reply.

In the example you gave 1024 bytes was allocated to char mcmessage.

I can see that in one of the tutorials that SetVariableEx buffer has to be constructed then free()d. Can I assume that you need to free() SendCardMessage buffer also? or does rev take over the memory management of that variable?

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Post by trevordevore » Sat Jul 12, 2008 2:01 pm

For SetVariableEx you are usually allocating memory dynamically so that memory has to be freed. In DispatchRevolutionMessage the size of mcmessage is known at compile time and the memory will be taken care of automatically.
Trevor DeVore
ScreenSteps - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

n.allan
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 153
Joined: Mon Mar 12, 2007 12:06 pm

Post by n.allan » Sat Jul 12, 2008 4:47 pm

Thanks Trevor

Post Reply

Return to “Building Externals”