Little bit of C++ Help?

Miscellaneous Forums/General Discussion/Little bit of C++ Help?

I think I've found a bug with my TV3D wrapper, and it's been a few months since I wrote this section, and I'm struggling to remember who, what and why I did it as I did it.

I have a C struct and I'm writing functions to populate the struct from a BMax type and vice versa, because BMax struggles to deal with structs.

Here's the C wrapper section which populates for one particular Struct, which contains two strings ( char* )

extern "C" __cdecl void cTVTEXTURE_PopulateType(cTV_TEXTURE* Ha,int* Active,char* Name,char* Filename,int* Width,int* Height,int* Size,int* bitdepth,int* colorkeyed,int* RealWidth,int* RealHeight,cCONST_TV_TEXTURETYPE* TextureType) {
	*Active=Ha->Active;
    *Name=*Ha->Name;
    *Filename=*Ha->Filename;
    *Width=Ha->Width;
    *Height=Ha->Height;
    *Size=Ha->Size;
    *bitdepth=Ha->bitdepth;
    *colorkeyed=Ha->colorkeyed;
    *RealWidth=Ha->RealWidth;
    *RealHeight=Ha->RealHeight;
    *TextureType=Ha->TextureType;
}


extern "C" __cdecl void cTVTEXTURE_PopulateStruct(cTV_TEXTURE* Ha,int Active,char* Name,char* Filename,int Width,int Height,int Size,int bitdepth,int colorkeyed,int RealWidth,int RealHeight,cCONST_TV_TEXTURETYPE TextureType) {
	Ha->Active=Active;
    Ha->Name=Name;
    Ha->Filename=Filename;
    Ha->Width=Width;
    Ha->Height=Height;
    Ha->Size=Size;
    Ha->bitdepth=bitdepth;
    Ha->colorkeyed=colorkeyed;
    Ha->RealWidth=RealWidth;
    Ha->RealHeight=RealHeight;
    Ha->TextureType=TextureType;
}


I think the second one is ok, and I don't think it's ever needed anyway. I think the first one is wrong though. Looking at the way I've had to put a * in front of Ha in those two lines which deal with the strings, I think that's where I've messed up trying to get it to compile. It doesn't crash but I'm getting empty strings.

Have I messed that up? How should it be? Is it just a case of removing the * from in front of both sides in those two instances? I think that's right, but I'm feeling very rusty with C++ and pointers always did do my head in pretty easy.

Got to love C++.

I assume the first one you are passing the C Struct cTVTexture and want to return the values from each member of the struct?

If so the Strings are wrong. You need to pass them back as pointers to pointers. ie. define them as char** on the paramter list.

Remebmer as string in C/C++ is defined as char* so to pass a char* you need to use char**.

Then the line.

*Name=*Ha->FileName is would just be

*Name=Ha->FileName

Now in blitz remember to take the var ptr of the Byte Ptr you want to accept the string pointer into.

So when you call it should look like

..(Ha,varptr(ActiveIntVar), varptr(FileNameBytePtrVar),

Hope that helps


Doug Stastny

Hi,

Thanks for the help. I have it compiling, but still it's not giving me a string, so perhaps I haven't quite followed your explanation.

I changed char* to char** for both the strings I want to return. And I changed the Extern definiton to use :Byte Ptr instead of $Z ( C String )

And this is the amended calling code :

Method PopulateType()
		Local NamePtr:Byte Ptr=Varptr(Name)
		Local FileNamePtr:Byte Ptr=Varptr(FileName)
		cTVTEXTURE_PopulateType(WrapperHandle,Varptr(Active),Varptr(NamePtr),Varptr(FileNamePtr),Varptr(Width),Varptr(Height),Varptr(Size),Varptr(bitdepth),Varptr(colorkeyed),Varptr(RealWidth),Varptr(RealHeight),Varptr(TextureType))
	End Method


I think this must be where the problem lies. Is this not what you meant?

I think your close but I am thinking your trying to get the CHAR* to a BMAX String you need to do a bit more work.

local NamePtr : Byte Ptr
local FileNamePtr: Byte Ptr
cTVTEXTURE_PopulateType(WrapperHandle,Varptr(Active),Varptr(NamePtr),Varptr(FileNamePtr)...
Name=String.FromCString(NamePtr)
FileName=String.FromCString(FileNamePtr)


Give this a try

Doug Stastny

Brilliant! That indeed does it. Thanks heaps for that, all that pointers to pointers stuff makes my head spin :)