Problem with string to function argument

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2008
Posts: 1
Reputation: Prm753 is an unknown quantity at this point 
Solved Threads: 0
Prm753 Prm753 is offline Offline
Newbie Poster

Problem with string to function argument

 
0
  #1
Aug 15th, 2008
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.

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Problem with string to function argument

 
0
  #2
Aug 15th, 2008
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.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Problem with string to function argument

 
0
  #3
Aug 15th, 2008
  1. while ( !feof(ep) )
  2. {
  3. fgets(registryHive, MAX_PATH, ep);
Note that this is not a good idea.

My suggestion:
  1. while ( fgets(registryHive, MAX_PATH, ep) ) {
Yes, it does the same thing.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Problem with string to function argument

 
0
  #4
Aug 15th, 2008
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...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1542 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC