PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Writing Registry in WM2005

Hi Everybody,

I just recently tried my software on a WM2005 device, and I'm finding that my code for writing/reading the windows registry isn't working.

I basically save off game state info when exiting the game, and I restore the state from the registry when the game is started. However, the game state is totally corrupted on a restart.

This code works perfectly on 2002/2003 devices. Any ideas what could be different about WM2005 in regards to the registry? Any help would be greatly appreciated! :D

Here's some of my code:
Code:

typedef struct {
int pieces[90];
int GameType;
} PreferenceType;

Code:

void MyGame::SavePrefs ()
{
HKEY pref_key ;

PreferenceType saveprefs;
int i;

for (i=0; i<90; i++) {
saveprefs.pieces[i] = pieces[i];
}
saveprefs.GameType = GameType;

RegCreateKeyEx (HKEY_LOCAL_MACHINE, TEXT ("Software\\MyGame"), 0, NULL, 0, 0, NULL, &pref_key, &tmp) ;

// Write registry struct
RegSetValueEx (pref_key, TEXT ("Data"), 0, REG_BINARY, (LPBYTE) &saveprefs, sizeof (saveprefs)) ;

RegCloseKey (pref_key) ;
return ;
}

Code:

void MyGame::LoadPrefs ()
{
HKEY pref_key;
DWORD tmp1, tmp2;

PreferenceType loadprefs;
int i=0;

if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT ("Software\\MyGame"), 0, 0, &pref_key) != ERROR_SUCCESS) {
return ;
}

// Load preferences struct
RegQueryValueEx (pref_key, TEXT ("Data"), NULL, &tmp1, (LPBYTE) &loadprefs, &tmp2) ;

for (i=0; i<90; i++) {
pieces[i] = loadprefs.pieces[i];
}
GameType = loadprefs.GameType;

RegCloseKey (pref_key);
return ;
}

Code:

bool MyGame::GameInit()
{
HKEY pref_key ;

if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT ("Software\\MyGame"), 0, 0, &pref_key) == ERROR_SUCCESS) {
// Preferences already exist
RegCloseKey (pref_key) ;
LoadPrefs() ;
}
}
Why don't you check the error codes when writing/reading the registry?

Maybe you don't have access to HKEY_LOCAL_MACHINE, not sure.
Thanks for your suggestions. Unfortunately, I still haven't been able to debug real-time on my WM2005 device. I get the message "The platform manager server is not available". I'm not sure what that means.

For grins, I changed HKEY_LOCAL_MACHINE to HKEY_CURRENT_USER, but the game state is still corrupted. :(
If you can't debug, then use MessageBox() to popup the error code. It's slower, but should allow you to troubleshot it.

Or even better, just log some info to a text file?
Actually, that's a very good idea (I hadn't thought of that). I'll give that a try!

Thanks again...
Okay, I used Messagebox() to verify that my registry data is being saved and loaded successfully. That being the case, the only other possibility (that I can think of) is that my datatypes, etc. are not correct/compatible for the structure that I am using to save my persistent data.

I am saving all persistent data as one large structure and as value type 'REG_BINARY'.

Once again, this code works perfectly on 2002/2003 devices, but not on WM2005.

Any thoughts?
Well, I finally found the problem. I don't really understand the solution, but it works.

I have some TCHAR arrays set up in my persistent structure (high score names) that gets saved to the registry. Before loading values into this array from the registry, I would zero them out by using a for-loop to assign 'NULL' to each element in the arrays.

For some reason, doing this caused all my registry data to load corrupted. I removed the NULL assignments and it's working now. Very strange.

8O
I had a similar problem. I was storing the registry data as "char" instead of "TCHAR", which worked perfectly fine for WM2003 and lower. This kept it from being legible in registry editors, since it would always display the data as TCHARs, which made it look like a bunch of rectangles. With WM2005, I had to change it all to TCHAR just to get it to work, and now the info is legible/editable in registry editors. Ah well.