• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Windows dll string return

Ondona

Member
Hi there im trying to create a function in visual studio and compile it as a dll (using visual studio 2019) then call that function from within GMS2.

I am able to get integers to work and return correctly but strings dont seem to return correctly

these are two functions that i've ust made to test if it will take and return the values

C++:
#define DevDLL extern "C" __declspec(dllexport)

DevDLL double sum(double a, double b)
{
    return a + b;
}
DevDLL char* txttest()
{
    return (char*)"testing";
}
the sum() funciton works as expects but when i try the txttest() it seems to have a hissy fit and return a random string of characters like this "(!ļæ½"

i dont really have much understanding/experience in c/c++ so more then likely im doing something wrong but can't seem to find any examples of code that returns a string to be used inside GMS2.
 
I'm not a C++ programmer either by any stretch but I found myself with the exact same problem. Take my explanation with a grain of salt.
The problem seems to be that GameMaker doesn't know the size of the the string being returned. With doubles, it always knows the universal size of a double (and what address spaces to check). Since strings vary in size (just arrays of chars), we need to somehow provide GameMaker with information about where it ends in memory. If we don't, it starts reading from unintended memory addresses, hence the garbled text.
C++ has the terminating character \0 to tell when a string has ended.
GML:
GMEXPORT char* txttest() {
    return (char*)"testing\0";
}
I don't know how safe/robust this is. There are probably specific string data types that might do this for you automatically.
Either way, this should allow you to shoot strings back into GM. šŸ‘
 
Top