• Advertisement

Archived

This topic is now archived and is closed to further replies.

bstr vs. int8*

This topic is 5044 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

Consider the following script:
t_img *tga;
bstr origname = "reflect.tga";
tga = img_new_tga ( bstr_to_int8(origname) );
and the functions:
t_img* img_new_tga (char *fname)
{
//blah blah

}
char* bstr_to_int8 ( asBSTR& str )
{
return (char*)str;
}
This works fine. But it doesnt work if I do this:
tga = img_new_tga ( "reflect.tga" );
or this
tga = img_new_tga ( bstr_to_int8("reflect.tga") );
Is this the expected behaviour? and if it is, is there a way to eliminate the need for the "bstr_to_int8" function. Typecasting in script doesnt work, it says that "it cant implicitly typecast from bstr to int8*". Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Try registering


t_img* img_new_tga (char *fname)
{
//blah blah
}


like this:


engine->RegisterGlobalFunction("t_img *img_new_tga(bstr)", asFUNCTION(img_new_tga), asCALL_CDECL);


This should let you write your script like this:


t_img *tga;
tga = img_new_tga("reflect.tga");


BSTR is typedefed as (unsigned char *) so that it can be sent to functions taking a (char *) directly.

---

As to your other question. The behaviour is expected, since your bstr_to_int8() function takes a non const reference. Simply declare the parameter as const and you should be able to call it with a constant directly.

---

Currently the bstr needs to be handled with care. This is because AngelScript has to manage the memory allocated for the strings. Thus there are a few rules that you ought to follow or you''ll get memory leaks:

- When receiving an asBSTR by value, you must free the memory before returning from the function. Call asBStrFree()
- When returning an new asBSTR to the script engine it must be allocated with asBStrAlloc()
- When receiving an asBSTR & or an asBSTR * you don''t have to free the memory since the script engine is still holding a reference to it
- If you receive an asBSTR *, and need to change the string it points to call asBStrAlloc() for the new string, and asBStrFree() on the old string.

In a future version I will break out the implementation of asBSTR from the script engine so that each application can decide how they are handled. Of course I will make the functions needed to make the asBSTR work as normal available to the public for free.


__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library

Share this post


Link to post
Share on other sites
Ahhh, ok. This,

AS_API asBSTR AS_CALL asBStrAlloc(asUINT length)
{
unsigned char *str = new unsigned char[length+4+1];
*((asUINT*)str) = length; // Length of string

str[length+4] = 0; // Null terminated


return str + 4;
}

explains it. Thanks a lot.

Share this post


Link to post
Share on other sites

  • Advertisement