Game Development Community

Need help with some c++.... please :)

by Vince Gee · in Torque 3D Professional · 01/21/2012 (1:52 am) · 5 replies

I can't figure out how to return a string from a console method, I have a console method that makes a call to the com interface.

BSTR *result = new BSTR();
	HRESULT hr = E_FAIL;
	hr = CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
	IComObjectPtr pIMud(__uuidof(ComObject));
	pIMud->Instance_Eval(_com_util::ConvertStringToBSTR(object),_com_util::ConvertStringToBSTR(evalstring),result);
	CoUninitialize();

It returns a BSTR in result which can be converted to a char* by calling

_com_util::ConvertBSTRToString(*result)

Now, how the devil do I get this char* of data into an object that I can pass back to the calling script from the console method? Thread safe would be a bonus!

Very grateful,

Vince

#1
01/21/2012 (6:53 am)
I believe something like this is what your looking for:

DefineEngineMethod( ArrayObject, getValue, const char*, ( S32 index ),,
   "Get the value of the array element at the submitted index.n"
   "@param index 0-based index of the array element to getn"
   "@return The value of the array element at the specified index, "
   "or "" if the index is out of rangen" )
{
   return object->getValueFromIndex( index ).c_str();
}

getValueFromIndex() returns a string
(string).c_str() being the base call to return the char* of the string.

so put your call to _com... in either a similar method with the object
type you want to be able to call it for or use a function...

//Functions
DefineEngineFunction( EWG_whirlpool, const char *, (const char* stringName),,
   "@returns the whirlpool hash of a string.nn")
{
   std::string output_hash = cryptoPackage->whirlpool(stringName);
   const char * done = (const char *)malloc(256);
   strcpy((char *)done, output_hash.c_str());
   return done;
}

*edit*
hmmm, looking back at this code, I have some extra working going on...
all you should need is:

return cryptoPackage->whirlpool(stringName).c_str();

Always looking at ways to improve my code... :)
#2
01/21/2012 (5:33 pm)
That looks familiar xD...
#3
01/21/2012 (5:45 pm)
hehe, yeah, never saw a need to reinvent the wheel,
I do like enhancing it though, as it works like a charm.
Your original resource got me through to 128/256 encryption.
I was quite pleased.

And this post made me relook and notice unneccessary calls.
Anything to boost a few milliseconds...

Let me restate:
Anything to improve my working coding base (includes my own and others *grin*)
#4
01/22/2012 (11:53 pm)
The Con namespace have a method right for that:
char * getReturnBuffer(const char *stringToCopy);
Usage:
char * localResult = /*your call here to store something, null-terminated*/
// 1 -- auto-copy contents of your string into console buffer to return into script:
return Con::getReturnBuffer(localResult);
// 2 -- retrieve buffer and manually build the string for returning:
char * returnBuffer = Con::getReturnBuffer(2048);
dSprintf(returnBuffer, "%s", localResult); // Use however you want
return returnBuffer;
#5
01/23/2012 (6:59 am)
Yep nothing wrong with using the returnbuffer, you forgot that you
have to tell the Lenght of the buffer, not use the buffer...

example:
char* ret = Con::getReturnBuffer( 1024 );
is the correct way... At least with your first example :)

*edit*
My bad, it has multiple versions, so you can use a (char*) to
do the copy in one line.. Inlcuding a version to work Directly with a [&String]