External function

Forums for specific tips, techniques and example code
bliu
Posts: 1
Joined: Wed Jan 12, 2011 9:08 am

External function

Postby bliu » Wed Jan 12, 2011 9:15 am

Hi,

I created one DLL, and calling from Jade as external function, the function returns the string type in Jade, but the output is just memory garbage.
Has anyone got any tips?

Thanks,
Bo

allistar
Posts: 156
Joined: Fri Aug 14, 2009 11:02 am
Location: Mount Maunganui, Tauranga

Re: External function

Postby allistar » Wed Jan 12, 2011 10:53 am

If you want to get a String from an external function, pass the String in as an output parameter with a length defined, like this:

In C++:

Code: Select all

extern "C" DllExport int copyDirectory(char *source, char *dest, char *error);
In Jade:

Code: Select all

copyDirectory(fromDir, toDir: String; error: String[512] output):Integer is copyDirectory in someLibraryName presentationClientExecution;
To put something into that String in C++, use something like "sprintf". The length needs to be defined in Jade as Jade allocates a memory buffer that large, the address of which is passed to the C++ function. If you write the end of that buffer the universe may implode. Some frameworks also pass through the allocated length of the buffer into the external call so a "safer" version of sprintf like "_snprintf" can be used to prevent buffer overflows.

Regards,
Allistar.

torrie
Posts: 92
Joined: Fri Aug 14, 2009 11:24 am

Re: External function

Postby torrie » Wed Jan 12, 2011 11:29 am

Jade requires a fixed length for the output parameters. If you have a variable length parameter, take a look at the "custom clipboard format" posting (https://forums.jadeworld.com/viewtopic. ... oard#p5296) it talks about one way of dealing with variable length return values (in that case from a clipboard function.)


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 6 guests