943,955 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3247
  • C++ RSS
Aug 15th, 2008
0

Problem with string to function argument

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  1. char registryHive[MAX_PATH];
  2. ZeroMemory ( registryHive, MAX_PATH );
  3.  
  4. FILE * ep = fopen( "test.txt", "r" );
  5.  
  6. while ( !feof(ep) )
  7. {
  8. fgets(registryHive, MAX_PATH, ep);
  9.  
  10. HKEY regHandle;
  11.  
  12. char* pHive = registryHive;
  13. char* pKey = registryHive;
  14.  
  15. while( *pKey != '\\' )
  16. {
  17. pKey++;
  18. if ( *pKey == '\0' )
  19. break;
  20. }
  21. *(pKey++) = '\0';
  22.  
  23. if ( strcmp(pHive, "HKEY_LOCAL_MACHINE") == 0 )
  24. regHandle = HKEY_LOCAL_MACHINE;
  25. else if ( strcmp(pHive, "HKEY_CURRENT_USER") == 0 )
  26. regHandle = HKEY_CURRENT_USER;
  27.  
  28. RemoveRegKeys(regHandle, pKey, false);
  29. }
  30.  
  31. 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)
  1. int RemoveRegKeys(HKEY regAddressOne, char *regAddressTwo, bool silent)
  2. {
  3. int doesExist;
  4. char buffer[255];
  5. char output[255];
  6.  
  7. ZeroMemory(buffer, 255);
  8. ZeroMemory(output, 255);
  9.  
  10. char logname[100];
  11.  
  12. GetEnvironmentVariable("HOMEDRIVE", logname, 100);
  13. strcat(logname, "\\JavaRa.log");
  14.  
  15. // FILE *fp;
  16. // fp = fopen(logname, "a");
  17.  
  18. HKEY hkey;
  19. int check = RegOpenKeyEx(regAddressOne, regAddressTwo, 0, KEY_SET_VALUE, &hkey);
  20. doesExist = SHDeleteKey(regAddressOne, regAddressTwo);
  21. RegCloseKey(hkey);
  22.  
  23. char test[80];
  24. sprintf(test, "key: %s\ncheck: %i\ndoesExist: %i", regAddressTwo, check, doesExist);
  25. MessageBox(0, test, 0, 0);
  26.  
  27. if (doesExist == 0)
  28. {
  29. TCHAR szOne[MAX_PATH];
  30. ZeroMemory(szOne, MAX_PATH);
  31.  
  32. switch(globalLanguageChosen)
  33. {
  34. // ...
  35. }
  36.  
  37. // fprintf(fp, "Found and removed: %s\r\n", regAddressTwo);
  38. sprintf(buffer, "%s %s\r\n", szOne, regAddressTwo);
  39. lstrcpy(output, buffer);
  40.  
  41. if (silent != true)
  42. {
  43. MessageBox(0, output, "Output", MB_OK | MB_ICONINFORMATION);
  44. }
  45.  
  46. }
  47. // fclose(fp);
  48. }

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Prm753 is offline Offline
1 posts
since Aug 2008
Aug 15th, 2008
0

Re: Problem with string to function argument

Quote 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.
You've probably already considered this, but file operations are notorious for being very slow. Going by what you've shown, I don't think replacing a few function calls with an I/O bound performance drain will be an improvement.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 15th, 2008
0

Re: Problem with string to function argument

C++ Syntax (Toggle Plain Text)
  1. while ( !feof(ep) )
  2. {
  3. fgets(registryHive, MAX_PATH, ep);
Note that this is not a good idea.

My suggestion:
C++ Syntax (Toggle Plain Text)
  1. while ( fgets(registryHive, MAX_PATH, ep) ) {
Yes, it does the same thing.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 15th, 2008
0

Re: Problem with string to function argument

The last char in the registryHive buffer after fgets() call is '\n' if it was a well-formed text file.
Of course, it's not a part of registry key...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: hi i would like to know how to update files and search in datatype.
Next Thread in C++ Forum Timeline: Serial Comm read problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC