Re: Exporting a structure
From: Christopher J. Holland (msnews_at_microsoft.com)
Date: 06/30/04
- Next message: Sankar-R: "Creating uncontrollable windows service"
- Previous message: EunSub,Kim: "How can I change bg color in SDI?"
- In reply to: Christopher J. Holland: "Re: Exporting a structure"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 30 Jun 2004 00:00:15 -0700
I noticed a couple misinterpetations, so I am sending the updated version :)
//==========================================================================
//
//==========================================================================
A Structure is like a char array.
The only difference is that they are not all characters, but they could be.
A struct looks like this.
double float long int char bool BOOL
// Struct = Address where structure is located = Address of double
Struct
{
StructAddress = double Address
+8 = float Address
+12 = long Address
+16 = int Address
+20 = char Address
+21 = bool Address
+22 = BOOL Address
} //Total Size = 26.
The following gets a double pointer to the first element of the array.
double* d = (double*)GetArray(); // Cast the void pointer to a double
pointer.
We could have done something weird, like getting an int from a double.
by casting it out as an int*.
Now, we will get the int, the hard way.
char* c = (char*)GetArray();
//increment the pointer to point to the int.
c += sizeof(double) + sizeof(float) + sizeof(long);
//Change the # of bytes to read to equal the size of an int.
int* i = (int*)c;
int j = *i;
When you do MyStruct* ptrStruct = (MyStruct*)GetStruct;
It like saying, let me access 26 bytes starting at the address.
Now, you can copy the whole struct
MyStruct TempStruct;
TempStruct = *ptrStruct; //deference to copy
If there is a Struct size mismatch, like you forgot some variable,
or added one, well, you can imagine what is going on.
//==========================================================================
// Playing with pointers.
// Copy the code directly and step through it.
//==========================================================================
long *pLong;
long lLong1;
long lLong2;
char* pChar;
char* Char;
void* vPtr;
unsigned char cArray[8]= {0xB1,0x68,0xDE,0x3A,0x15,0xCD,0x5B,0x07};
// A pointer is just an address, typically it is a long or integer size.
// That is so it can point to all the memory or a large space.
// 4 Bytes or 0xFFFFFFFF = 4,294,967,295 address spaces
// Here is a pointer that points to zero address.
lLong1 = sizeof(void*);
vPtr = NULL;
// Here, the pointer is pointing to cArray.
// At this point, it doesn't know how many bytes to read,
// It is just equal to the address of the Array. 0x0012f420
vPtr = cArray;
// Now we cast it out as a long,
// or receive the number of bytes in a long
pLong = (long*)vPtr;
lLong1 = *pLong; //Dereference the pointer
// Here is a neat way.
// Cast it out as a long, then Dereference.
// It saves a step, but looks more cryptic
lLong1 = *(long*)vPtr;
pChar = (char*)vPtr;
pChar += 4 * sizeof(char);
pLong = (long*)pChar;
lLong2 = *pLong;
lLong2 = sizeof(char); //1
lLong2 = sizeof(int); //4
lLong2 = sizeof(long); //4
lLong2 = sizeof(double); //8
lLong2 = sizeof(float); //4
lLong2 = sizeof(bool); //1
lLong2 = sizeof(BOOL); //4
//The reader is left to figure out why lLong = 123456789
//and lLong2 = 987654321.
//Hint: One little, Two little, Three little
//==========================================================================
//
//==========================================================================
Good Luck,
--
Christopher J. Holland [!MVP]
"Christopher J. Holland" <msnews@microsoft.com> wrote in message
news:uuV29IjXEHA.3120@TK2MSFTNGP12.phx.gbl...
> Return the struct as a void*
> void* GetStruct(){ return &Struct; }
>
> Then in your class, cast it as your struct pointer.
> MyStruct* pStruct = (MyStruct*)GetStruct();
>
> Your got me thinking....
> I've been screwing around with structures and pointers a lot lately.
> I wrote down a bunch of stuff to help you with Structures and Pointers.
>
//==========================================================================
> =================
> //
>
//==========================================================================
> =================
> A Structure is like a char array.
> The only difference is that they are not all characters, but they could
> be...
> A struct looks like this.
> double float long int char int char bool double
>
> // Struct = Address of char.
> Struct
> {
> StructAddress = char Address
> +1 = int Address
> +5 = BOOL Address
> +9 = bool Address
> +10 = float Address
> +14 = double Address
> +22 = long Address
> char[5] = StructAddress + 26;
> } //Total Size = 31.
>
> The following gets a double pointer to the first element.
> double* d = (double*)GetArray(); // Cast the void to a double pointer.
> We could have done something weird, like getting an int from a double.
> by casting it out as an int*.
>
> Now, we will get the int, the hard way.
> char* c = (char*)GetArray();
> c += sizeof(double) + sizeof(float) + sizeof(long); //increment the
pointer
> to point to the int.
> int* i = (int*)c; //Change the number of bytes to read to equal the size
of
> an int.
>
> When you do MyStruct* ptrStruct = (MyStruct*)GetStruct;
> It like saying, let me access 31 bytes starting at the address.
>
> Now, you can copy the whole struct
> MyStruct TempStruct;
> TempStruct = *ptrStruct; //deference to copy
>
> If there is a Struct size mismatch, like you forgot some variable, or
added
> one,
> well, you can imagine what is going on.
>
//==========================================================================
> =================
> // Playing with pointers.
> // Copy the code directly and step through it.
>
//==========================================================================
> =================
> long *pLong;
> long lLong1;
> long lLong2;
> char* pChar;
> char* Char;
> void* vPtr;
> unsigned char cArray[8]= {0xB1,0x68,0xDE,0x3A,0x15,0xCD,0x5B,0x07};
>
> // A pointer is just an address, typically it is a long or integer size.
> // That is so it can point to all the memory or a large space.
> // 4 Bytes or 0xFFFFFFFF = 4,294,967,295 address spaces
> // Here is a pointer that points to zero address.
> lLong1 = sizeof(void*);
> vPtr = NULL;
>
> // Here, the pointer is pointing to cArray.
> // At this point, it doesn't know how many bytes to read,
> // It is just equal to the address of the Array. 0x0012f420
> vPtr = cArray;
>
> // Now we cast it out as a long,
> // or receive the number of bytes in a long
> pLong = (long*)vPtr;
> lLong1 = *pLong; //Dereference the pointer
>
> // Here is a neat way.
> // Cast it out as a long, then Dereference.
> // It saves a step, but looks more cryptic
> lLong1 = *(long*)vPtr;
>
> pChar = (char*)vPtr;
> pChar += 4 * sizeof(char);
> pLong = (long*)pChar;
> lLong2 = *pLong;
>
> lLong2 = sizeof(char); //1
> lLong2 = sizeof(int); //4
> lLong2 = sizeof(long); //4
> lLong2 = sizeof(double); //8
> lLong2 = sizeof(float); //4
> lLong2 = sizeof(bool); //1
> lLong2 = sizeof(BOOL); //4
>
> The reader is left to figure out why lLong = 123456789 and lLong2 =
> 987654321.
> Hint: One little, Two little, Three little
>
//==========================================================================
> =================
> //
>
//==========================================================================
> =================
> Good Luck,
> --
> Christopher J. Holland [!MVP]
> "raykos" <raykos@discussions.microsoft.com> wrote in message
> news:F604725F-1D6F-4AFA-B4CB-5278043A136A@microsoft.com...
> > Hello all,
> >
> > I'm working on creating a regular Dll. This Dll will have variables
> that will consist of at least 5 strings, 2 integers, 3 booleans and maybe
a
> char buffer. What I want
> > to do is have these variables populated by the client using the Dll.
So,
> instead of exporting all of these variables, would it be easier to export
a
> structure and then have the client populate the structure?
> > However, I don't know how to do this? Does anyone perhaps have a code
> snippet of how to do this, both at the Dll end and in the client?
> >
> > Any help appreciated.....
> >
> > TIA,
> >
> > Ray K.
> >
>
>
- Next message: Sankar-R: "Creating uncontrollable windows service"
- Previous message: EunSub,Kim: "How can I change bg color in SDI?"
- In reply to: Christopher J. Holland: "Re: Exporting a structure"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|