Archived

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

Korndog

Working with void pointers to read data

Recommended Posts

Korndog    122
Hi, I''ve been searching Microsoft''s site for a while on how to read data using the LPVOID address that LockResource returns, but I found nothing. There''s some example called RCDATA.EXE that doesn''t exist anymore, I guess, and that''s about. All I know is that converting it to a char* will let me increment the variable, but.... there''s a better way to work with this, right? Thanks int i; char str[80]; HRSRC hFound = FindResource(NULL, "txtCartman", RT_RCDATA); HGLOBAL hCartman = LoadResource(NULL, hFound); LPVOID lpBuff = LockResource(hCartman); char *pChar = (char *) lpBuff; for (i = 0; i < 9; i++) { str = *pChar; pChar++; } str[i] = ''\0'';

Share this post


Link to post
Share on other sites
Korndog    122
Yeah, but this was just a simple example.... I''m really wanting to just load Targas and MD2 models from resources and the like
Thanks for replay though; I''d really like to know how most people are using the void pointer to read data

Share this post


Link to post
Share on other sites
crazy166    122
just cast the pointer to the appropriate type. that''s the way to work with void pointers. you just have to make sure that what you''re casting it to makes sense.
in your example of loading images you''ll probably cast to BYTE* or something.
i don''t store anything in my executable, it''s all in external packaging, so i don''t ever use the load/lock functions.

crazy166
some people think i'm crazy, some people know i am

Share this post


Link to post
Share on other sites
felisandria    739
Void pointers, fun...

The thing to remember with void * is the reason it''s legal. It''s legal because pointer length is always defined (as big enough to hold a memory address). This does not mean that the size of the data pointed to is defined, it just means that the amount of space needed to point at the start of a block of memory is defined. It is your responsibility to correctly interpret the true type of the pointer so that the size of whatever''s being pointed to can be correctly calculated. If you have a void * that''s supposed to be pointing at a long and you cast it on the other side to a char*, then you can get some really bizarre results.

I''ve mostly used void* to read files of various types and extract what I believe should be in the file. For instance, I can read an entire MP3 into a void * buffer, then start extracting the header of one of the blocks by what I want them to be... if I know the next few bytes must contain characters I extract them as characters, if I know they must be a long telling me the size of the block, I extract as a long. void* can also be useful in cross-platform work and in APIs.

So... determine what type you are expecting, and use that... be it a char, long, short, int, even a BYTE.

-fel

Share this post


Link to post
Share on other sites