PocketMatrix forums

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

Using the registry!

Hi all, again, just a quick Q,

does anyone know a simple way to place a bool value into the registry, and or read it!

HKLM/software/blah-company/name/key

etc....

-thanks...
I have no idea if this will work on CE, but under 2k et al, I'd use:

LONG RegSetValue(
HKEY hKey, // handle to key
LPCTSTR lpSubKey, // subkey name
DWORD dwType, // information type
LPCTSTR lpData, // value data
DWORD cbData // size of value data
);

to set, and:

LONG RegQueryValue(
HKEY hKey, // handle to key to query
LPCTSTR lpSubKey, // subkey name
LPTSTR lpValue, // string buffer
PLONG lpcbValue // size of returned string
);

to get registry values.
Sorry, I didn't think I had the PPC MSDN stuff installed here, but I do.

So, here's a really hacky function that reads a STRING key from the registry (should be easy to adapt to your needs)... tested only on W2k 'cos I don't have my PPC with me or the emulator installed:

Code:

long getRegistryString(char **string,const char *key,const char *subkey, DWORD *dwLength)
{
long l_return;
DWORDdwType;
HKEYh_hkey;
longl_reg_return;

*string=malloc(*dwLength);

if (*string==NULL)
{
return ERROR_MEMORY_ALLOC_FAIL;
}

if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,key,0,KEY_QUERY_VALUE, &h_hkey)!= ERROR_SUCCESS)
{
free(*string);
return ERROR_REGISTRY_READ_FAIL;
}

l_reg_return=RegQueryValueEx(h_hkey,subkey,NULL,&dwType,*string,dwLength);

if (l_reg_return==ERROR_MORE_DATA)
{
// attempt to redeclare entry with enough memory allocated
free(*string); // dump the existing declare
string=malloc(*dwLength);
if (string!=NULL)
{
l_reg_return=RegQueryValueEx(h_hkey,subkey,NULL,&dwType,*string,dwLength);
}
}

RegCloseKey(h_hkey);

if (*string==NULL)
{
return ERROR_MEMORY_ALLOC_FAIL;
}

if (l_reg_return!=ERROR_SUCCESS || dwType!=REG_SZ)
{
free(*string);
return ERROR_REGISTRY_READ_FAIL;
}

// We assume here that we have indeed retrieved the correct string
// the calling function takes responsibility for freeing string
return ERROR_SUCCESS;
}


Call it something like:

Code:

DWORD dw_length=0;
char *c_filename=NULL;

l_filename_read=getRegistryString(&c_filename, TEXT("SOFTWARE\\MyCompany"), TEXT("MyData"), &dw_length);



To set the data you want the:
Code:

LONG RegSetValueEx(
HKEY hKey,
LPCWSTR lpValueName,
DWORD Reserved,
DWORD dwType,
const BYTE *lpData,
DWORD cbData );


(Open, set, and close the key similar to the above routine).

You probably want the REG_BINARY data type, or a REG_DWORD.

Hope that helps,

Refractor