1. Structs Question
- Posted by euman at bellsouth.net Jul 13, 2001
- 384 views
Hello, This regards structs in Windows I need help with the 2nd line tvinsert.hInsertAfter,TVI_ROOT tvinsert.item.imask,TVIF_TEXT item obviously is in the form tvinsert[0], tvinsert[1] etc like sequence operations in eu. I cant figure out how to do this, I looked at win32lib and found how it's put together there but it just seems odd. Does anyone have a quick fix for this? Below is some code that might help you help me. </snip> hImageList = c_func(xImageList_Create,{16,16,ILC_COLOR16,2,10}) hBitmap = LoadImage("list.bmp", IMAGE_BITMAP) junk = c_func(xImageList_Add,{hImageList,hBitmap,NULL}) c_proc(xDeleteObject, {hBitmap}) junk = c_func(SendMessage,{TreeView1,TVM_SETIMAGELIST,0,hImageList}) tvstruct = allocate_struct( SIZEOF_tvstruct ) tvinsert = allocate_struct( SIZEOF_TVITEM ) poke4( tvstruct + TVINSERTSTRUCT_hParent,NULL) poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_ROOT) --item = peek( {1, SIZEOF_TVITEM}) --poke( tvstruct + TVINSERTSTRUCTITEM_mask[1], item ) poke4( tvinsert + TVITEM_mask,or_all({TVIF_TEXT+TVIF_IMAGE+TVIF_SELECTEDIMAGE})) Parent = allocate_string("Parent Item") poke4( tvinsert + TVITEM_pszText,Parent) poke4( tvinsert + TVITEM_iImage,0) poke4( tvinsert + TVITEM_iSelectedImage,1) free(Parent) hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) poke4( tvstruct + TVINSERTSTRUCT_hParent,hParent) poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_LAST) Child1 = allocate_string("Child One") poke4(tvinsert + TVITEM_pszText,Child1) junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) free(Child1) Child2 = allocate_string("Child Two") poke4(tvinsert + TVITEM_pszText,Child2) junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) free(Child2) free(tvinsert) free(tvstruct) <snip\> TIA, Euman euman at bellsouth.net
2. Re: Structs Question
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 14, 2001
- 429 views
----- Original Message ----- From: <euman at bellsouth.net> To: "EUforum" <EUforum at topica.com> Subject: Structs Question > > This regards structs in Windows > > I need help with the 2nd line > > tvinsert.hInsertAfter,TVI_ROOT > tvinsert.item.imask,TVIF_TEXT > > item obviously is in the form tvinsert[0], tvinsert[1] etc > like sequence operations in eu. Well actually it isn't. In the 'C' struct, "item" has an offset of 8 bytes from the beginning of "tvinsert", and "imask" has an offset of 0 bytes from the beginning of "item". Thus "imask" has an offset of 8 bytes from the beginning of "tvinsert". If you use consants like these below, you can see how "item" is just a section of "tvinsert". constant TVINSERTSTRUCT_hParent = 0, TVINSERTSTRUCT_hInsertAfter = 4, TVINSERTSTRUCTITEM_mask = 8, TVINSERTSTRUCTITEM_hItem = 12, TVINSERTSTRUCTITEM_state = 16, TVINSERTSTRUCTITEM_stateMask = 20, TVINSERTSTRUCTITEM_pszText = 24, TVINSERTSTRUCTITEM_cchTextMax = 28, TVINSERTSTRUCTITEM_iImage = 32, TVINSERTSTRUCTITEM_iSelectedImage = 36, TVINSERTSTRUCTITEM_cChildren = 40, TVINSERTSTRUCTITEM_lParam = 44, SIZEOF_TVINSERTSTRUCT = 48 > I cant figure out how to do this, I looked at win32lib > and found how it's put together there but it just seems > odd. > > Does anyone have a quick fix for this? > It not broken so it doesn't need "fixing" > Below is some code that might help you help me. > > </snip> > > hImageList = c_func(xImageList_Create,{16,16,ILC_COLOR16,2,10}) > > hBitmap = LoadImage("list.bmp", IMAGE_BITMAP) > junk = c_func(xImageList_Add,{hImageList,hBitmap,NULL}) > c_proc(xDeleteObject, {hBitmap}) > > junk = c_func(SendMessage,{TreeView1,TVM_SETIMAGELIST,0,hImageList}) > > tvstruct = allocate_struct( SIZEOF_tvstruct ) > tvinsert = allocate_struct( SIZEOF_TVITEM ) > > poke4( tvstruct + TVINSERTSTRUCT_hParent,NULL) > poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_ROOT) > > --item = peek( {1, SIZEOF_TVITEM}) > --poke( tvstruct + TVINSERTSTRUCTITEM_mask[1], item ) I can see you copied this code from win32lib. The difference is that in win32lib, the "offset" data is a sequence that tells the library not only the offset from the beginning of the structure, but also how long the field is, and what type of data is in it. The routine that you copied from is passed the address of a TVITEm structure and the lines commented out abaove, just copy that TVITEM to the new TVINSERT structure. > > poke4( tvinsert + TVITEM_mask,or_all({TVIF_TEXT+TVIF_IMAGE+TVIF_SELECTEDIMAGE})) > > Parent = allocate_string("Parent Item") > poke4( tvinsert + TVITEM_pszText,Parent) > poke4( tvinsert + TVITEM_iImage,0) > poke4( tvinsert + TVITEM_iSelectedImage,1) > free(Parent) > In the code above, where you refer to the tvitem embedded in the tvinsert struct, just add the offset of the beginning of the tvitem to the parameters. Thus... constant TVINSERT_tvitem = 8 poke4( tvinsert + 8 + TVITEM_mask,or_all({TVIF_TEXT+TVIF_IMAGE+TVIF_SELECTEDIMAGE})) Parent = allocate_string("Parent Item") poke4( tvinsert + TVINSERT_tvitem + TVITEM_pszText,Parent) poke4( tvinsert + TVINSERT_tvitem + TVITEM_iImage,0) poke4( tvinsert + TVINSERT_tvitem + TVITEM_iSelectedImage,1) free(Parent) > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > poke4( tvstruct + TVINSERTSTRUCT_hParent,hParent) > poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_LAST) > > Child1 = allocate_string("Child One") > poke4(tvinsert + TVITEM_pszText,Child1) or poke4(tvinsert + TVINSERT_tvitem + TVITEM_pszText,Child1) > junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > free(Child1) > > Child2 = allocate_string("Child Two") > poke4(tvinsert + TVITEM_pszText,Child2) aka... poke4(tvinsert + TVINSERT_tvitem + TVITEM_pszText,Child2) > junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > free(Child2) > > free(tvinsert) > free(tvstruct) > > <snip\> > --- cheers, Derek.
3. Re: Structs Question
- Posted by euman at bellsouth.net Jul 14, 2001
- 378 views
Thanks Derek for takeing a look! I already tried setting this up as below and it works showing the parent and children and the pszText of the children and the order of the tree is correct but, the parent (above) pszText does not show, in other words the parent doesnt have a name. I thought I might be missing something here. This is where Im looseing the text name for the root when I send this message. hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) and this doesnt make any since to me. Here it is again with changes>... hImageList = c_func(xImageList_Create,{16,16,ILC_COLOR16,2,10}) hBitmap = LoadImage("list.bmp", IMAGE_BITMAP) junk = c_func(xImageList_Add,{hImageList,hBitmap,NULL}) c_proc(xDeleteObject, {hBitmap}) junk = c_func(SendMessage,{TreeView1,TVM_SETIMAGELIST,0,hImageList}) tvstruct = allocate_struct( SIZEOF_tvstruct ) poke4( tvstruct + TVINSERTSTRUCT_hParent,NULL) poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_ROOT) poke4( tvstruct + TVINSERTSTRUCTITEM_mask,or_all({TVIF_TEXT+TVIF_IMAGE+TVIF_SELECTEDIMAGE})) iParent = allocate_string("Parent Item") poke4( tvstruct + TVINSERTSTRUCTITEM_pszText,iParent) poke4( tvstruct + TVINSERTSTRUCTITEM_iImage,0) poke4( tvstruct + TVINSERTSTRUCTITEM_iSelectedImage,1) free(iParent) hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) poke4( tvstruct + TVINSERTSTRUCT_hParent, hParent) poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_LAST) Child1 = allocate_string("Child One") poke4(tvstruct + TVINSERTSTRUCTITEM_pszText,Child1) junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) free(Child1) Child2 = allocate_string("Child Two") poke4(tvstruct + TVINSERTSTRUCTITEM_pszText,Child2) junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) free(Child2) free(tvstruct) Euman euman at bellsouth.net
4. Re: Structs Question
- Posted by euman at bellsouth.net Jul 14, 2001
- 374 views
Never mind I figured it out. if I free(iParent) before I send this message hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) there wont be any text for the root item. Now how in hek is this? I poked the text into the structure then free'd the allocated_string before I sent the message and it doesnt work and when I move the free() after the message......hmmm Im at a loss. (beats me) but it works now. Euman euman at bellsouth.net > > Thanks Derek for takeing a look! > > I already tried setting this up as below and it works showing > the parent and children and the pszText of the children and > the order of the tree is correct but, > the parent (above) pszText does not show, in other words > the parent doesnt have a name. > > I thought I might be missing something here. > > This is where Im looseing the text name for the root when I send > this message. > > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > > and this doesnt make any since to me. > > Here it is again with changes>... > > > hImageList = c_func(xImageList_Create,{16,16,ILC_COLOR16,2,10}) > > hBitmap = LoadImage("list.bmp", IMAGE_BITMAP) > junk = c_func(xImageList_Add,{hImageList,hBitmap,NULL}) > c_proc(xDeleteObject, {hBitmap}) > > junk = c_func(SendMessage,{TreeView1,TVM_SETIMAGELIST,0,hImageList}) > > tvstruct = allocate_struct( SIZEOF_tvstruct ) > > poke4( tvstruct + TVINSERTSTRUCT_hParent,NULL) > poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_ROOT) > > poke4( tvstruct + > TVINSERTSTRUCTITEM_mask,or_all({TVIF_TEXT+TVIF_IMAGE+TVIF_SELECTEDIMAGE})) > > iParent = allocate_string("Parent Item") > poke4( tvstruct + TVINSERTSTRUCTITEM_pszText,iParent) > poke4( tvstruct + TVINSERTSTRUCTITEM_iImage,0) > poke4( tvstruct + TVINSERTSTRUCTITEM_iSelectedImage,1) > free(iParent) > > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > > poke4( tvstruct + TVINSERTSTRUCT_hParent, hParent) > poke4( tvstruct + TVINSERTSTRUCT_hInsertAfter,TVI_LAST) > > Child1 = allocate_string("Child One") > poke4(tvstruct + TVINSERTSTRUCTITEM_pszText,Child1) > junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > free(Child1) > > Child2 = allocate_string("Child Two") > poke4(tvstruct + TVINSERTSTRUCTITEM_pszText,Child2) > junk = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > free(Child2) > > free(tvstruct) > > Euman > euman at bellsouth.net
5. Re: Structs Question
- Posted by Derek Parnell <ddparnell at bigpond.com> Jul 14, 2001
- 387 views
----- Original Message ----- From: <euman at bellsouth.net> To: "EUforum" <EUforum at topica.com> Subject: Re: Structs Question > Never mind I figured it out. > if I free(iParent) before I send this message > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > there wont be any text for the root item. Yep, that's the problem alright. Same with Child1 and child2. > Now how in hek is this? > > I poked the text into the structure then free'd the allocated_string before > I sent the message and it doesnt work and when I move the free() > after the message......hmmm Im at a loss. (beats me) but it works now. Well, actually you didn't poke the TEXT into the structure. All you do is poke the ADDRESS of the text into the structure. > iParent = allocate_string("Parent Item") The allocate_string() reserves a place in RAM for you text, copies the text in the sequence to that spot, and returns to your program the location (address) reserved for you. > poke4( tvstruct + TVINSERTSTRUCTITEM_pszText,iParent) This poke4() copies the address returned to the struct. An address is 4-bytes long and that's what the poke4() function does - copies 4-byte to an RAM location. > free(iParent) This tells Windows that you no longer need the RAM location that was reserved for your text and gives it back to Windows. After this, your text may or may not be in the RAM still, it all depends on how Windows uses that RAM again after you gave it back. You cannot use or expect to use RAM that you have given back to Windows. > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) However, you then send the message to Windows to insert an item. The Windows code that does this doesn't know you gave back the RAM and will attempt to use the address you poked into the structure. But by this time, the spot could have been used by anything else and destroyed your text. If you notice, in win32lib, we always release_mem() after sending messages. ----- Cheers, Derek.
6. Re: Structs Question
- Posted by euman at bellsouth.net Jul 14, 2001
- 386 views
Pretty good lesson I think....> Euman euman at bellsouth.net > > > Never mind I figured it out. > > if I free(iParent) before I send this message > > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > > there wont be any text for the root item. > > Yep, that's the problem alright. Same with Child1 and child2. > > > Now how in hek is this? > > > > I poked the text into the structure then free'd the allocated_string > before > > I sent the message and it doesnt work and when I move the free() > > after the message......hmmm Im at a loss. (beats me) but it works now. > > Well, actually you didn't poke the TEXT into the structure. All you do is > poke the ADDRESS of the text into the structure. > > > iParent = allocate_string("Parent Item") > > The allocate_string() reserves a place in RAM for you text, copies the text > in the sequence to that spot, and returns to your program the location > (address) reserved for you. > > > poke4( tvstruct + TVINSERTSTRUCTITEM_pszText,iParent) > > This poke4() copies the address returned to the struct. An address is > 4-bytes long and that's what the poke4() function does - copies 4-byte to an > RAM location. > > > free(iParent) > > This tells Windows that you no longer need the RAM location that was > reserved for your text and gives it back to Windows. After this, your text > may or may not be in the RAM still, it all depends on how Windows uses that > RAM again after you gave it back. > > You cannot use or expect to use RAM that you have given back to Windows. > > > hParent = c_func(SendMessage,{TreeView1,TVM_INSERTITEM, 0, tvstruct}) > > However, you then send the message to Windows to insert an item. The Windows > code that does this doesn't know you gave back the RAM and will attempt to > use the address you poked into the structure. But by this time, the spot > could have been used by anything else and destroyed your text. > > If you notice, in win32lib, we always release_mem() after sending messages. > > ----- > Cheers, > Derek.