Saving an ennumerated device in DX7

Started by
6 comments, last by NoNseNse 23 years, 9 months ago
OK, I modified the code of the D3DIM Tutorial ''ennumerate'', and wanted to use it as a config tool. So it''s just the dialog, the user should select his desired configuration and the the program should save the GUIDS of the devices in a file. That''s the code: bool bU3dH; .. GUID* pDeviceGUID; GetSelectedDriver( &pDriverGUID, &pDeviceGUID, &pMode, &Windowed, &bU3dH ); FILE *config; config = fopen("config.cfg","wb"); fwrite(&bU3dH, sizeof(bool), 1, config); fwrite(pDriverGUID, sizeof(GUID*), sizeof(pDriverGUID), config); fclose(confic); Now selecting the device works without problems, but when it comes to save the program crashes at both fwrite lines. If I leave the Bool variable without any value, the fwrite of the bool works. So what am I doing wrong? THX,, NoNseNse
Advertisement
I can''t see what''s wrpng on the first line but on the second fwrite it should be:
fwrite(pDriverGUID, sizeof(GUID), 1, config);
Ok, I figured a few things out. the problem is noth with the fwrite, but with the pointers to the GUIDS. I refreshed my knowledge about pointers, and tried to first take out the value of the adress pointed to - this should be the data I want to store. but when i use the following lines, I will get an access violation:

GUID *Device;
GUID buffer, *ptr_g;
/*here is the function which gets the devices*/
buffer = *Device; //<-- this is the line which doesn''t work
ptr_g = &buffer
fwrite(ptr_g, .....


Is it possible that you can''t take off the value of a GUID? But how am I supposed to save my configuration?

THX,, NoNseNse

Mr Cucumber is right. Do you understand how fwrite works?

    fwrite(pDriverGUID, sizeof(GUID), 1, config); [/source]This starts copying data at the address of pDriverGUID and stops when it reaches the address of pDriverGUID + sizeof(GUID).1 means it only copies the data once.And a GUID is a struct like this:[source]typedef struct _GUID { unsigned long Data1; unsigned short Data2; unsigned short Data3; unsigned char Data4[8];} GUID;[/source]If you want to print it out to text, you have to access all itsmembers individually:[source] char gui_string[64]; wsprintf(guid_string, "%08X-%04X-%04X-%02X%02X%02X%02X%02X%02X%02X%02X",          pDriverGUID->Data1,    pDriverGUID->Data2,    pDriverGUID->Data3,          pDriverGUID->Data4[0], pDriverGUID->Data4[1], pDriverGUID->Data4[2], pDriverGUID->Data4[3],          pDriverGUID->Data4[4], pDriverGUID->Data4[5], pDriverGUID->Data4[6], pDriverGUID->Data4[7]);    


But don''t save it as text, as you would have to convert it back
and this would be a waste of time. Just save it as binary.
If the "Device" pointer is NULL then you will get an acess violation.

Check it for null to see if that is the problem, lie this:

if(Device == NULL)
//Show a messagebox or something here to show thatsomething //was wrong

One more thing I see in your first message is exactly what you said, that there is something wrong with the guid pointer.

The line should be:
fwrite(*DriverGUID, sizeof(GUID), 1, config);

That might fix it.
quote:Original post by NoNseNse

I refreshed my knowledge about pointers


Time for another refresher, methinx

Here''s your test code again:

GUID *Device;
GUID buffer, *ptr_g;
buffer = *Device; //<-- this is the line which doesn''t work



Ok, line by line:
GUID *Device; - this makes a -pointer- called Device. It starts off uninitialised and invalid.
GUID buffer, *ptr_g; - this makes a GUID called buffer, which is ok, and another pointer called ptr_g, which again is uninitialised and invalid.
buffer = *Device; - this says "fill Buffer with whatever the Device pointer points to".

But your Device pointer doesn''t point to anything meaningful! So why copy it? In fact, it''s probably pointing to memory outside your program, hence the crash.

By the way, I''m not sure you can copy a GUID with ''=''. I think you need to use memcpy().

And one last point: doesn''t your first code sample just attempt to save a pointer to a GUID? Surely saving the pointer rather than the GUID itself is useless?
quote:
By the way, I''m not sure you can copy a GUID with ''=''. I think you need to use memcpy().


Since a GUID is a struct, you can use ''='' to copy it to another GUID struct.
But that will probably not get you anywhere. NoNseNse, just remember
that a GUID is not stored as a number or a string.
My mistake, I was thinking of my own system where I allocate a new GUID and copy in the data that way. The ''='' operator would work fine, too. After all, it should be fundamentally the same as a memcpy() on a struct, anyway. Duh again

This topic is closed to new replies.

Advertisement