| | |
Problem with string to function argument
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Solved Threads: 0
Hi all,
I am trying to optimize one of my applications by cutting down on the number of function calls I am making. I have decided to do this by reading definitions out of a file and using them as the argument for the function.
For example, if I wanted to remove HKEY_LOCAL_MACHINE\Test, I would put that in my file and the code should get HKEY_LOCAL_MACHINE for pHive and Test for pKey.
Here is the definition of RemoveRegKeys:
My problem is, RegOpenKeyEx and SHDeleteKey always return 2 when I call them through RemoveRegKeys in the way that I have in the first code snippet. If I manually define a string, the function works fine. Is this a problem with the way I am reading the string out of the file, or am I corrupting the string somehow?
Thanks for your help.
If you need me to explain further, let me know.
Regards,
Paul
I am trying to optimize one of my applications by cutting down on the number of function calls I am making. I have decided to do this by reading definitions out of a file and using them as the argument for the function.
C++ Syntax (Toggle Plain Text)
char registryHive[MAX_PATH]; ZeroMemory ( registryHive, MAX_PATH ); FILE * ep = fopen( "test.txt", "r" ); while ( !feof(ep) ) { fgets(registryHive, MAX_PATH, ep); HKEY regHandle; char* pHive = registryHive; char* pKey = registryHive; while( *pKey != '\\' ) { pKey++; if ( *pKey == '\0' ) break; } *(pKey++) = '\0'; if ( strcmp(pHive, "HKEY_LOCAL_MACHINE") == 0 ) regHandle = HKEY_LOCAL_MACHINE; else if ( strcmp(pHive, "HKEY_CURRENT_USER") == 0 ) regHandle = HKEY_CURRENT_USER; RemoveRegKeys(regHandle, pKey, false); } fclose(ep);
For example, if I wanted to remove HKEY_LOCAL_MACHINE\Test, I would put that in my file and the code should get HKEY_LOCAL_MACHINE for pHive and Test for pKey.
Here is the definition of RemoveRegKeys:
C++ Syntax (Toggle Plain Text)
int RemoveRegKeys(HKEY regAddressOne, char *regAddressTwo, bool silent) { int doesExist; char buffer[255]; char output[255]; ZeroMemory(buffer, 255); ZeroMemory(output, 255); char logname[100]; GetEnvironmentVariable("HOMEDRIVE", logname, 100); strcat(logname, "\\JavaRa.log"); // FILE *fp; // fp = fopen(logname, "a"); HKEY hkey; int check = RegOpenKeyEx(regAddressOne, regAddressTwo, 0, KEY_SET_VALUE, &hkey); doesExist = SHDeleteKey(regAddressOne, regAddressTwo); RegCloseKey(hkey); char test[80]; sprintf(test, "key: %s\ncheck: %i\ndoesExist: %i", regAddressTwo, check, doesExist); MessageBox(0, test, 0, 0); if (doesExist == 0) { TCHAR szOne[MAX_PATH]; ZeroMemory(szOne, MAX_PATH); switch(globalLanguageChosen) { // ... } // fprintf(fp, "Found and removed: %s\r\n", regAddressTwo); sprintf(buffer, "%s %s\r\n", szOne, regAddressTwo); lstrcpy(output, buffer); if (silent != true) { MessageBox(0, output, "Output", MB_OK | MB_ICONINFORMATION); } } // fclose(fp); }
My problem is, RegOpenKeyEx and SHDeleteKey always return 2 when I call them through RemoveRegKeys in the way that I have in the first code snippet. If I manually define a string, the function works fine. Is this a problem with the way I am reading the string out of the file, or am I corrupting the string somehow?
Thanks for your help.
If you need me to explain further, let me know.
Regards,
Paul
•
•
•
•
Originally Posted by Prm753
I am trying to optimize one of my applications by cutting down on the number of function calls I am making. I have decided to do this by reading definitions out of a file and using them as the argument for the function.
If at first you don't succeed, keep on sucking until you do succeed.
C++ Syntax (Toggle Plain Text)
while ( !feof(ep) ) { fgets(registryHive, MAX_PATH, ep);
My suggestion:
C++ Syntax (Toggle Plain Text)
while ( fgets(registryHive, MAX_PATH, ep) ) {
dwk
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
Seek and ye shall find.
"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.
"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison
"The only real mistake is the one from which we learn nothing."
-- John Powell
![]() |
Similar Threads
- mysql_query(): supplied argument is not a valid MySQL-Link (PHP)
- Function & writing to file problem (Python)
- Pointers (archived tutorial) (C++)
- problem with exec function for child process (C)
- Error C2660 Function does not take 1 argument (C++)
- Program works... now I need to create a function, HELP! (C)
- getattr function (Python)
- Simple array/class problem (dot operator)??? (C++)
- using OCI to call a ORACLE stored function (C++)
Other Threads in the C++ Forum
- Previous Thread: hi i would like to know how to update files and search in datatype.
- Next Thread: Filing example
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






