RE: [Dev-C++] Flush or set valeu from a key of Registry.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Santiago P. C. <San...@yd...> - 2002-07-24 16:32:26
|
Hi,
To manage registry keys and values, you must first open a key and then
manage its values, SO...
You must have in mind that registry _KEYS_ and registry _VALUES_ are
different things!!!
(Take a look at the code bellow and check variables named: szKey and
szValueName)
Here you have a little C program that reads a DWORD value, increases it and
stores it again in the registry. Values are printed on the screen.
To handle a string you should modify a few values:
dwDataType must macht with REG_STRING,
dwDataSize must be high enough to store the retrieved value,
and few more...
Hope this helps!
Regards ,Santy
<CODE>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define MAX_VALUE 256
// FUNTIONs DEFINITIONs
void OutputString(char* szInfoStr);
void OutputValue(DWORD dwValue, char* lpStr = NULL);
// FUNCTIONs IMPLEMENTATION
void OutputString(char* lpStr)
{
if (lpStr != NULL)
printf("%s\n",lpStr);
}
void OutputValue(DWORD dwValue, char* lpStr)
{
char szStr[256];
if (lpStr != NULL)
sprintf(szStr,"[%s] Value: %d",lpStr,(int)dwValue);
else
sprintf(szStr,"Value: %d",(int)dwValue);
OutputString(lpStr);
}
void ShowSystemErrorMsg(DWORD dwError)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dwError,
(DWORD)MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf, (DWORD) 0,
NULL);
OutputString((char*)lpMsgBuf);
LocalFree(lpMsgBuf);
}
int main(int argc, char *argv[])
{
char szKey[] = "Whatever\\Your\\Key\\Path\\Looks\\Like";
char szValueName[] = "MyValue";
UINT uValue = 0;
HKEY hRegKey = NULL;
DWORD dwError = 0;
DWORD dwDataType = 0; // type buffer
DWORD dwDataSize = sizeof(DWORD); // size of data buffer
LPBYTE lpByteData = NULL; // data buffer
// Try to open key
dwError = (DWORD) RegOpenKeyEx(HKEY_CURRENT_USER, (LPCTSTR)szKey, 0,
KEY_ALL_ACCESS, &hRegKey );
if (dwError == ERROR_SUCCESS)
{
lpByteData = (LPBYTE) LocalAlloc(LPTR,(UINT)dwDataSize);
if (lpByteData != NULL)
{
// Get current value
dwError = (DWORD)
RegQueryValueEx(hRegKey,(LPCTSTR)szValueName,NULL,
(LPDWORD)&dwDataType,lpByteData,(LPDWORD)&dwDataSize);
if ( (dwError == ERROR_SUCCESS)&&(dwDataType == REG_DWORD) )
{
// Output retrieved data
uValue = (UINT) (*lpByteData);
OutputValue(uValue,"Previous Value");
// Increase current cycle
uValue = (++uValue%MAX_VALUE);
OutputValue(uValue,"Increased Value");
// Store new value for MyValue at registry
dwError = RegSetValueEx(hRegKey,(LPCTSTR)szValueName, 0,
REG_DWORD, (BYTE*)&uValue, dwDataSize);
if ( (dwError != ERROR_SUCCESS))
{
OutputValue(dwError,"RegSetValueEx - dwError");
ShowSystemErrorMsg(dwError);
}
// Flush Data anyway
RegFlushKey(hRegKey);
}
// Free dynamic memory
LocalFree(lpByteData);
}
// Close RegistryKey
RegCloseKey(hRegKey);
}
else
ShowSystemErrorMsg(dwError);
return 0;
}
</CODE>
-----Original Message-----
From: we...@ha... [mailto:we...@ha...]
Sent: Saturday, February 23, 2002 10:01 PM
To: dev...@li...
Subject: [Dev-C++] Flush or set valeu from a key of Registry.
Hi,
I want to flush (delete) a string value from registry.
I tryed to set it to 0 of this mode:
#include <windows.h>
int main(int argc, char *argv[])
{
HKEY hKey;
DWORD dwDisposition;
unsigned char *szBuf = (unsigned char *)strdup("0");
if (RegSetValueEx(HKEY_CURRENT_USER,
"\\Software\\Microsoft\\Security\\say\\", 0, REG_SZ, szBuf,
strlen((char*)szBuf)) != ERROR_SUCCESS)
return 1;
RegCloseKey(hKey);
return 0;
}
But it didn't set the value of:
\\HKEY_CURRENT_USER\\Software\\Microsoft\\Security\\say to "0". It crate a
key \\Software\\Microsoft\\Security\\say in \\HKEY_CURRENT_USER . :P
How to do to is set this value ??
And how to for example delete a key \\Software\\Microsoft\\Security\\say\\
??
Can someone gave examples ?? :)
Thkz and Best Regards.
[ ]'s
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf _______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--------------------------------------------------------------------------------------
This message and any files transmitted with it are confidential and intended solely
for the use of the individual or entity to whom they are addressed. No confidentiality
or privilege is waived or lost by any wrong transmission.
If you have received this message in error, please immediately destroy it and kindly
notify the sender by reply email.
You must not, directly or indirectly, use, disclose, distribute, print, or copy any
part of this message if you are not the intended recipient. Opinions, conclusions and
other information in this message that do not relate to the official business of
Ydilo Advanced Voice Solutions, S.A. shall be understood as neither given nor endorsed by it.
--------------------------------------------------------------------------------------
|