DLL Function returning a string

Ok, I'm pretty new to the C++ area but not to programming. I program in various other languages and some of the basics are the same but a lot of it is not. for example in my world strings are variables that hold text and with C++ it appears strings are Objects pointing to a memory location that hold the text. what I need is to take a function that takes std::string and returns a char * for example.

1
2
3
4
5
char * myFunction(std::string INMSG)
{
// add text message "some text here" and join with INMSG
// and then return the combined text in char *
}


any help with this is appreciated
thanks
Last edited on
std::string are variables that hold text.
¿what do you want a char* for?
Well i've tried to return the std::string and it causes the external application to crash. i've tried returning char * and it didn't crash at all but I couldn't build the string in the function. i've been able to return an int and a double but not a string. Oh i'm loading the DLL with a program called MetaTrader 4. am I doing this the wrong way?
When you return the char*, are you taking steps to ensure that the memory it points to is not being released for other use (i.e. written over by something else)?
here is what works for me and i'll show what doesn't work for me
1
2
3
4
5
6
7
8
	char* XPortMsg(char * inmsg)
	{ 
		char * retVal = "Something here: "; // Returns "Something here:" in MetaTrader 4
		//char * retVal = "Something here: " + inmsg; Throws error in MetaTrader 4
		
		return retVal;
		
	}
Ok so what I want is something pretty simple

function outline is like this
1
2
3
4
5
6
7
8
9
10
	char* XPortMsg(string inmsg)
	{ 
//		// take the string and perform operations
		// assemble a string for example
		char* retVal = "USER: ";
		// then I want to take the string and 
		// assemble it like
		char* retVal2 = retVal + inmsg.c_str();
		return retVal2;
	}
ok so i've made some progress on my issue and i'm able to pass a string but.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char* XPortMsg(string inmsg)
	{ 
		char* s = "DLL Received: ";
		string m = string(s);

		char* p = new char[inmsg.length()+1]; 
		memcpy(p, inmsg.c_str(), inmsg.length()+1);
		string sa = m + p;

		char* ret = new char[sa.length()+1];
		memcpy(ret, sa.c_str(), sa.length()+1);
		
		return ret;
	}

in my program MetaTrader 4 the return is not consistent. the returning value is always

"DLL Received: " with a trailing character like a or d or t for example
"DLL Received: d"

or it returns what is expected

"DLL Received: Calling DLL" which is correct.

anyone know why or how I can fix it
ok, so I got it working the following is the code in case anyone else is interested



1
2
3
4
5
6
7
8
9
10
char* XPortMsg(char* inmsg)
	{
		char* mHeader = "DLL Received ";
		// retrieve the string and assign to variable
		char* msgHeader = new char[strlen(inmsg)+1];
		memcpy(msgHeader,inmsg,strlen(inmsg)+1);
		// end retrieve

		return msgHeader;
	}
Topic archived. No new replies allowed.