Sure, this is how I'd do it:
From the C++ side, this is the source of the DLL:
#include <time.h>
extern "C" __declspec(dllexport) void GetCurrentTime(char* StringBuffer, int BufferSize)
{
struct tm* tm1;
time_t caltime;
caltime = time(NULL);
tm1 = localtime(&caltime);
strftime(StringBuffer, BufferSize, "%I:%M:%S", tm1);
}
Note that the string buffer comes as a parameter and out function only fills the buffer.
Now what the declaration says:
__declspec(dllexport) - This tells the compiler to export this function off the DLL. There are other ways of marking functions as exported, but this is the easiest one (although it's Visual C++ specific).
extern "C" - This tells the compiler to use a C naming convention for the function. This is only necessary if you're using C++ for writing your DLL. C++ naming convention is different, it uses so called "decorated names". It means the name of the function contains encoded information about it's parameters and other information. If you would't use the extern "C" specifier, your function would be exported as something like "?GetCurrentTime@@YAXPADH@Z".
The
DllMain function generated by the wizard denotes an entry point of the DLL library. This function gets called whenever the DLL is being loaded or attached to a new process (and also when it's being detached). You can use this function to initialize your DLL's internal data. But you don't need to write this function. If you don't, the compiler will provide a default (empty) DllMain function.
Now, from the WME script it would look like this:
external "wme_dll.dll" cdecl GetCurrentTime(string, int);
var Str = new String(200);
GetCurrentTime(Str, 200);
actor.Talk("Oh, it's " + Str + " ! Time to lunch !");
Ok, it a bit more complicated than your original code, but it's safe :-) You create the string buffer in the WME script and pass the buffer to the DLL function to fill it.
Note the "cdecl" calling convention. It's C default. If you don't specify it, WME will default to "stdcall" convention and you'll get warnings in the WME log file.
I have uploaded the DLL project source at:
http://dead-code.org/misc/wme_dll.zip