Page 1 of 1

External function

Posted: Wed Jan 12, 2011 9:15 am
by bliu
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

Re: External function

Posted: Wed Jan 12, 2011 10:53 am
by allistar
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.

Re: External function

Posted: Wed Jan 12, 2011 11:29 am
by torrie
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.)