Sign in to follow this  

asBSTR and bstr

This topic is 4922 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble making my script file read a *bstr from my host program. I define the bstr like so: asBSTR *timeStr; I register the bstr like so: r = engine->RegisterObjectProperty("CGame", "bstr timeStr", offsetof(CGame, timeStr)); assert( r >= 0 ); Then I have a function that returns a Time String:
asBSTR* TimeString()
{
	char buffer[100];
	if (game.min < 10)
	{
		if (game.sec < 10) sprintf(buffer, "0%d'0%d", game.min, game.sec);
		else sprintf(buffer, "0%d'%d", game.min, game.sec);
	} else {
		if (game.sec < 10) sprintf(buffer, "%d'0%d", game.min, game.sec);
		else sprintf(buffer, "%d'0%d", game.min, game.sec);
	}
	return (asBSTR*)buffer;
}


Then every frame I make game.timeStr = TimeString(); inside of my host. When the player dies I want a script file to print the time that he played. So I do this: bstr s = "Time: "; s += game.timeStr; And it crashes my progam. Any advice?

Share this post


Link to post
Share on other sites
Here's an example of how I am returning bstr to script code. At least this doesn't crash, hopefully this is correct regarding memory usage as well.


static asBSTR script_Object_getName(Object *o)
{
char *name = o->getName();
asBSTR res = asBStrAlloc(strlen(name));
strcpy((char *)res, name);
return res;
}


Share this post


Link to post
Share on other sites
The problem is the bstr has an extra dword at the start with the size of the string. You have to use BstrAlloc() to create an asBSTR

Share this post


Link to post
Share on other sites
Sign in to follow this