plugin struct utilization::??

Need help running MacroQuest2? Ask your questions about how to get things to work on your computer.

Moderator: MacroQuest Developers

Claritone
Cheezily Banned
Cheezily Banned
Posts: 16
Joined: Thu Jun 03, 2004 2:27 pm

plugin struct utilization::??

Post by Claritone » Sat Sep 18, 2004 1:57 am

I am trying to better understand what takes place when I use something like this in my plugin:

((PCHARINFO)pCharData)->heading

I understand the concept, mostly. The first segment being the struct itself, the next being the defined location in eqgame.exe's memory. and heading being of course the name given to that specific variable.

What I seek to understand, is what MacroQuest is doing in the 2nd section, I am aware it is a pointer, what I want to do is learn how it is being understood by our compiler. I have tried following as much of MQ2's source by myself to no avail, thats a lot of coding. Is there something I can read up on?, is there any specific names to what is happening here?


// edit: perhaps I should've posted this in the Plugins directory

User avatar
Cr4zyb4rd
Plugins Czar
Posts: 1449
Joined: Tue Jul 20, 2004 11:46 am

Post by Cr4zyb4rd » Sat Sep 18, 2004 3:14 am

The 2nd section isn't doing any work whatsoever, as you said it's just a pointer. The "magic" happens in the first section. See, a lot of the data behaves like what you might know better as a "union", with the same memory being handled in different ways depending on how it's accessed. The first part (a "typecast") tells the compiler something like "whatever data happens to be here, treat it as though it were in the format specified by PCHARINFO"

Looking through the source, PCHARINFO is defined (in EQData.h) as a pointer to a struct of type _CHARINFO. The beginning of this struct looks something like this

Code: Select all

typedef struct _CHARINFO { 
/*0x0000*/   BYTE       Unknown0x0[0x4]; 
/*0x0004*/   BYTE		field_4;    
/*0x0005*/   BYTE		Padding0x5;    
/*0x0006*/   CHAR		Name[0x40];    
/*0x0046*/   CHAR		Lastname[0x20];    
so let's suppose I have a pointer, pJoebob which, for argument's sake, points to a character string that looks like this "I am the mighty Joebob! All your base are belong to us!" Now, suppose for some reason, I were to do ((PCHARINFO)pJoebob)->Name, in a strcpy() or whatever...this would point to "he mighty Joebob..." and so on, eventually hitting a null-character and returning a "meaningful" (although somewhat odd) string. If, however, I did ((PCHARINFO)pJoebob)->Lastname I'd be pointing at data well beyond the end of my "real" Joebob string, and frog-knows what might happen. In practice, we only use the typecast when we expect pCharData (whatever "type" of pointer the compiler thinks it is at the moment) to point to something in the _CHARINFO layout, and everything works out peachy.

I hope that makes some sort of sense. I'm relatively new myself, and perhaps not the best instructor. :)

Claritone
Cheezily Banned
Cheezily Banned
Posts: 16
Joined: Thu Jun 03, 2004 2:27 pm

Post by Claritone » Sun Sep 19, 2004 2:35 am

I got my situation settled...mostly. You're post helped me get on track, thanks.