Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wrong value is written to Registry.

Status
Not open for further replies.

WillieBotha

Programmer
Jun 20, 2001
5
ZA
Hi,

I am new to this forums as well as to Visual C++ so hello to all. I seem to have a strange problem here. What I did is I created a dll which writes values to the registry. Two of the values are written correctly and the other is gibberish. The code is as follows - I will indicate also where the code changes to gibberish. The variable that changes is UName :

long SYNCID::RegCreate(const char datastring[10])
{
HKEY HK,K;
char ClassString[3] = "CS";
LPCTSTR SubKeyName = "M&MInfo";

LPSTR U_Name="UserName";
LPSTR U_Path="UserPath";
LPSTR U_Folder="UserFolder";

LPSTR UName="UserName";
LPSTR UPath="UserPath";
LPSTR UFolder="UserFolder";

CUserIDInfo cid;

int i=0;

unsigned long *lp=NULL;
unsigned long return_value;

RetUserPath(UName,UPath,UFolder);

//Here the Value of UName is as it should be nl. Willie Botha


//CREATE THE REGISTRY KEY
return_value=RegCreateKeyEx(HKEY_CURRENT_CONFIG,
SubKeyName,
0, //reserved
ClassString,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&HK,
lp) ;

//Here the Value of UName changed to 
if (return_value==ERROR_SUCCESS) {
SyncAddLogEntry("\n Info Key Created \n");
}
else
{
SyncAddLogEntry("\n Info Key Not Created \n");
}

SyncAddLogEntry("Testing This : ");
SyncAddLogEntry((const char *)UName);

// OPEN KEY
/********************************************************************/

return_value=RegCreateKeyEx(HK,
datastring,
0, //reserved
ClassString,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&K,
lp) ;

if (return_value==ERROR_SUCCESS) {
SyncAddLogEntry("\n Serial Key Created \n");
}
else
{
SyncAddLogEntry("\n Serial Key Not Created \n");
}

/*******************************************************************/
//CREATE AND SET THE VALUE
return_value=RegSetValueEx(K, // handle of key to set value for
U_Name, // address of value to set
0, // reserved
REG_SZ, // flag for value type
(const unsigned char *)UName,
//(const unsigned char *)datastring, // address of value data
strlen(UName) // size of value data
);

SyncAddLogEntry("Testing Again : ");
SyncAddLogEntry((const char *)UName);

if (return_value==ERROR_SUCCESS) {
SyncAddLogEntry("\n User Name Key Created");
}
else
{
SyncAddLogEntry("\n User Name Key Not Created");
}

return_value=RegSetValueEx(K, // handle of key to set value for
U_Path, // address of value to set
0, // reserved
REG_SZ, // flag for value type
(const unsigned char *)UPath,
//(const unsigned char *)datastring, // address of value data
strlen(UPath) // size of value data
);


if (return_value==ERROR_SUCCESS) {
SyncAddLogEntry("\n User Path Key Created");
}
else
{
SyncAddLogEntry("\n User Path Key Not Created");
}

return_value=RegSetValueEx(K, // handle of key to set value for
U_Folder, // address of value to set
0, // reserved
REG_SZ, // flag for value type
(const unsigned char *)UFolder,
//(const unsigned char *)datastring, // address of value data
strlen(UFolder) // size of value data
);

if (return_value==ERROR_SUCCESS) {
SyncAddLogEntry("\n User Folder Key Created");
}
else
{
SyncAddLogEntry("\n User Folder Key Not Created");
}

SyncAddLogEntry("\n");

return 0;
}

Thanks in advance,
Willie Botha
Programmer
Mead & McGrouther
South-Africa
 
Are you writing truncated data?
1. lp mustn't be a pointer. Do like:
long lp;
and use
.....&lp.
2.You must reset lp to needed value after each calling. John Fill
1c.bmp


ivfmd@mail.md
 
I have to declare it as unsigned long but what must I reset it to everytime.

Thanks for your help so far. Willie Botha
Programmer
Mead & McGrouther
South-Africa
 
I've written below coorrect code what affects only your registers. I didn't find errors on your code. Maybe some of other functions alter your values. Just debug all of them to see what happend.

int RegCreate(const char datastring[10])
{
HKEY K;
HKEY HK=HKEY_CURRENT_CONFIG;
char ClassString[3] = "CS";
LPCTSTR SubKeyName = "M&MInfo";

LPSTR U_Name="UserName";
LPSTR U_Path="UserPath";
LPSTR U_Folder="UserFolder";

LPSTR UName="UserName";
LPSTR UPath="UserPath";
LPSTR UFolder="UserFolderrrrrrrrrr";


int i=0;

unsigned long *lp=NULL;
unsigned long return_value;

return_value=RegCreateKeyEx(HKEY_CURRENT_CONFIG,
SubKeyName,
0, //reserved
ClassString,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&HK,
lp) ;


return_value=RegCreateKeyEx(HK,
datastring,
0, //reserved
ClassString,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&K,lp);
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
return_value=RegSetValueEx(
K,
U_Name,
0,
REG_SZ,
(const unsigned char *)UName,
//(const unsigned char *)datastring, // address of value data
strlen(UName) // size of value data
);
return_value=RegSetValueEx(K, // handle of key to set value for
U_Path, // address of value to set
0, // reserved
REG_SZ, // flag for value type
(const unsigned char *)UPath,
//(const unsigned char *)datastring, // address of value data
strlen(UPath) // size of value data
);
return_value=RegSetValueEx(K, // handle of key to set value for
U_Folder, // address of value to set
0, // reserved
REG_SZ, // flag for value type
(const unsigned char *)UFolder,
//(const unsigned char *)datastring, // address of value data
strlen(UFolder) // size of value data
);
return 0;
}
John Fill
1c.bmp


ivfmd@mail.md
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top