Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Issue with strcpy [modified]

Issue with strcpy [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
help
12 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    capricious_001
    wrote on last edited by
    #1

    Hi guys, I have a function where I am passing a value by reference. Below is the prototype:

    bool retrieveLogFileLocation(LPSTR&);

    Now within the body of the function definition I have the following function as well.

    bool retrieveLogFileLocation(LPSTR& value){
    char[255] lszValue;
    HKEY hKey;
    LONG returnStatus;
    DWORD dwType = REG_SZ;
    DWORD dwSize =255;
    bool success = true;

    RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
    
    strcpy(value, lszValue);
    

    }

    The problem I am receiving is that whenever my program runs, it crashes instantly, and I know for sure it is an issue with strcpy(value, lszValue). I have tried in multiple ways through type-casting and such to resolve the issue, but I'm perplexed as to whats causing the problem. Any help would be appreciated :D Robbie -- modified at 4:53 Monday 19th June, 2006 PS: I only included bits and pieces of the function definition simply because the actual function definition is very long. :P

    L W _ D 4 Replies Last reply
    0
    • C capricious_001

      Hi guys, I have a function where I am passing a value by reference. Below is the prototype:

      bool retrieveLogFileLocation(LPSTR&);

      Now within the body of the function definition I have the following function as well.

      bool retrieveLogFileLocation(LPSTR& value){
      char[255] lszValue;
      HKEY hKey;
      LONG returnStatus;
      DWORD dwType = REG_SZ;
      DWORD dwSize =255;
      bool success = true;

      RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
      
      strcpy(value, lszValue);
      

      }

      The problem I am receiving is that whenever my program runs, it crashes instantly, and I know for sure it is an issue with strcpy(value, lszValue). I have tried in multiple ways through type-casting and such to resolve the issue, but I'm perplexed as to whats causing the problem. Any help would be appreciated :D Robbie -- modified at 4:53 Monday 19th June, 2006 PS: I only included bits and pieces of the function definition simply because the actual function definition is very long. :P

      L Offline
      L Offline
      Laxman Auti
      wrote on last edited by
      #2

      capricious_001 wrote:

      Any help would be appreciated

      May be the length of the receiving string variable is short as compared to the data you tring to copy into it. Knock out 't' from can't, You can if you think you can :cool:

      C 1 Reply Last reply
      0
      • C capricious_001

        Hi guys, I have a function where I am passing a value by reference. Below is the prototype:

        bool retrieveLogFileLocation(LPSTR&);

        Now within the body of the function definition I have the following function as well.

        bool retrieveLogFileLocation(LPSTR& value){
        char[255] lszValue;
        HKEY hKey;
        LONG returnStatus;
        DWORD dwType = REG_SZ;
        DWORD dwSize =255;
        bool success = true;

        RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
        
        strcpy(value, lszValue);
        

        }

        The problem I am receiving is that whenever my program runs, it crashes instantly, and I know for sure it is an issue with strcpy(value, lszValue). I have tried in multiple ways through type-casting and such to resolve the issue, but I'm perplexed as to whats causing the problem. Any help would be appreciated :D Robbie -- modified at 4:53 Monday 19th June, 2006 PS: I only included bits and pieces of the function definition simply because the actual function definition is very long. :P

        W Offline
        W Offline
        Weiye Chen
        wrote on last edited by
        #3

        Is there really a need to pass by reference cos i think it can work without it.

        1 Reply Last reply
        0
        • L Laxman Auti

          capricious_001 wrote:

          Any help would be appreciated

          May be the length of the receiving string variable is short as compared to the data you tring to copy into it. Knock out 't' from can't, You can if you think you can :cool:

          C Offline
          C Offline
          capricious_001
          wrote on last edited by
          #4

          hmm I didnt initialize the length of the referenced value variable to a certain length. Actually I dont quite understand what you mean lol. Robbie

          K 1 Reply Last reply
          0
          • C capricious_001

            Hi guys, I have a function where I am passing a value by reference. Below is the prototype:

            bool retrieveLogFileLocation(LPSTR&);

            Now within the body of the function definition I have the following function as well.

            bool retrieveLogFileLocation(LPSTR& value){
            char[255] lszValue;
            HKEY hKey;
            LONG returnStatus;
            DWORD dwType = REG_SZ;
            DWORD dwSize =255;
            bool success = true;

            RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
            
            strcpy(value, lszValue);
            

            }

            The problem I am receiving is that whenever my program runs, it crashes instantly, and I know for sure it is an issue with strcpy(value, lszValue). I have tried in multiple ways through type-casting and such to resolve the issue, but I'm perplexed as to whats causing the problem. Any help would be appreciated :D Robbie -- modified at 4:53 Monday 19th June, 2006 PS: I only included bits and pieces of the function definition simply because the actual function definition is very long. :P

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #5

            It's a problem with RegQueryValue(IMHO). You are not opening the registry before modifying it. Handle to an open key. The key must have been opened with the KEY_QUERY_VALUE access right. This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys: HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATA HKEY_PERFORMANCE_NLSTEXT HKEY_PERFORMANCE_TEXT HKEY_USERS Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

            C D 2 Replies Last reply
            0
            • _ _AnsHUMAN_

              It's a problem with RegQueryValue(IMHO). You are not opening the registry before modifying it. Handle to an open key. The key must have been opened with the KEY_QUERY_VALUE access right. This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys: HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATA HKEY_PERFORMANCE_NLSTEXT HKEY_PERFORMANCE_TEXT HKEY_USERS Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

              C Offline
              C Offline
              capricious_001
              wrote on last edited by
              #6

              Ya I created a handle to the open key and all of that is fine (closed the handle appropriately etc). Its just with the strcpy that I'm having the problems with. As well, either by passing via reference or returning LPSTR value, I am still receiving the same unhandled exception error. It still crashes when my program is loaded.

              W 1 Reply Last reply
              0
              • C capricious_001

                hmm I didnt initialize the length of the referenced value variable to a certain length. Actually I dont quite understand what you mean lol. Robbie

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #7

                An alternative way of doing this is to use a string class, e.g., CString. Here's some old code I dug out - look at the last if block - RegQueryValueEx call...

                // Retrieves Operator Console ID from "SOFTWARE\Cegelec AEG\CMS\Operator Console" subkey in registry
                // Looks for a value name called, e.g., "ID". May return something like "IB101"

                long OcipsAutoInterface::GetConsoleIDEx(LPCTSTR pszValueName, CString& strValueData)
                {
                HKEY hkOperatorConsole;
                long nResult; // return value - ERROR_SUCCESS or some value indicating failure
                unsigned long nSize;

                // Open subkey to Operator Console
                
                CString strSubKey;
                strSubKey.LoadString( IDS\_OP\_CONSOLE\_SUBKEY ); //e.g., "SOFTWARE\\Cegelec AEG\\CMS\\Operator Console "
                
                nResult = ::RegOpenKeyEx( HKEY\_LOCAL\_MACHINE, 
                						  LPCTSTR( strSubKey ),
                						  0, 
                						  KEY\_EXECUTE, // Permission for read access
                						  &hkOperatorConsole );
                
                if ( ERROR\_SUCCESS == nResult )
                {
                	// Search for value name and get size of value data
                	nResult = ::RegQueryValueEx( hkOperatorConsole, pszValueName, NULL, NULL, NULL, &nSize );
                	
                	if ( ERROR\_SUCCESS == nResult )
                	{
                		// Retrieve value data
                		
                		// ...first return a writeable buffer for the data
                		LPTSTR pszValueData = strValueData.GetBuffer( nSize );
                
                		//...then get the data
                		nResult = ::RegQueryValueEx ( hkOperatorConsole, pszValueName, NULL, NULL, LPBYTE( pszValueData ), &nSize );
                		strValueData = pszValueData;
                	}
                }
                
                VERIFY( ERROR\_SUCCESS == ::RegCloseKey( hkOperatorConsole ) );
                
                return nResult;
                

                } // End GetConsoleIDEx()

                Kevin

                C 1 Reply Last reply
                0
                • K Kevin McFarlane

                  An alternative way of doing this is to use a string class, e.g., CString. Here's some old code I dug out - look at the last if block - RegQueryValueEx call...

                  // Retrieves Operator Console ID from "SOFTWARE\Cegelec AEG\CMS\Operator Console" subkey in registry
                  // Looks for a value name called, e.g., "ID". May return something like "IB101"

                  long OcipsAutoInterface::GetConsoleIDEx(LPCTSTR pszValueName, CString& strValueData)
                  {
                  HKEY hkOperatorConsole;
                  long nResult; // return value - ERROR_SUCCESS or some value indicating failure
                  unsigned long nSize;

                  // Open subkey to Operator Console
                  
                  CString strSubKey;
                  strSubKey.LoadString( IDS\_OP\_CONSOLE\_SUBKEY ); //e.g., "SOFTWARE\\Cegelec AEG\\CMS\\Operator Console "
                  
                  nResult = ::RegOpenKeyEx( HKEY\_LOCAL\_MACHINE, 
                  						  LPCTSTR( strSubKey ),
                  						  0, 
                  						  KEY\_EXECUTE, // Permission for read access
                  						  &hkOperatorConsole );
                  
                  if ( ERROR\_SUCCESS == nResult )
                  {
                  	// Search for value name and get size of value data
                  	nResult = ::RegQueryValueEx( hkOperatorConsole, pszValueName, NULL, NULL, NULL, &nSize );
                  	
                  	if ( ERROR\_SUCCESS == nResult )
                  	{
                  		// Retrieve value data
                  		
                  		// ...first return a writeable buffer for the data
                  		LPTSTR pszValueData = strValueData.GetBuffer( nSize );
                  
                  		//...then get the data
                  		nResult = ::RegQueryValueEx ( hkOperatorConsole, pszValueName, NULL, NULL, LPBYTE( pszValueData ), &nSize );
                  		strValueData = pszValueData;
                  	}
                  }
                  
                  VERIFY( ERROR\_SUCCESS == ::RegCloseKey( hkOperatorConsole ) );
                  
                  return nResult;
                  

                  } // End GetConsoleIDEx()

                  Kevin

                  C Offline
                  C Offline
                  capricious_001
                  wrote on last edited by
                  #8

                  There isnt any other way? Because there is a lot of code I have to change if I change the type of that one variable. I'd prefer keeping value as a LPSTR type.

                  K 1 Reply Last reply
                  0
                  • C capricious_001

                    Ya I created a handle to the open key and all of that is fine (closed the handle appropriately etc). Its just with the strcpy that I'm having the problems with. As well, either by passing via reference or returning LPSTR value, I am still receiving the same unhandled exception error. It still crashes when my program is loaded.

                    W Offline
                    W Offline
                    Weiye Chen
                    wrote on last edited by
                    #9

                    I just did a test using your code(with some modifications in bold) except without the reference and it works fine.:

                    void somefunc()
                    {
                       char cBuffer[400];
                       retrieveLogFileLocation(cBuffer);
                    }

                    bool retrieveLogFileLocation(LPSTR value)
                    {
                        char lszValue[255];
                        HKEY hKey = HKEY_LOCAL_MACHINE;
                        LONG returnStatus;
                        DWORD dwType = REG_SZ;
                        DWORD dwSize =255;
                        bool success = true;

                    RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);

                    strcpy(value, lszValue);

                    return true;
                    }

                    I set hKey just for testing purposes. It could be a problem with your key.

                    Last modified: Monday, June 19, 2006 4:57:58 AM --

                    1 Reply Last reply
                    0
                    • C capricious_001

                      There isnt any other way? Because there is a lot of code I have to change if I change the type of that one variable. I'd prefer keeping value as a LPSTR type.

                      K Offline
                      K Offline
                      Kevin McFarlane
                      wrote on last edited by
                      #10

                      Well, in that case, as Laxman says, you need to carefully check the sizes of your source and destination buffers. For future code though I recommend using string objects (string or CString) instead of C-style strings. You'll have fewer problems of this sort. Kevin

                      1 Reply Last reply
                      0
                      • _ _AnsHUMAN_

                        It's a problem with RegQueryValue(IMHO). You are not opening the registry before modifying it. Handle to an open key. The key must have been opened with the KEY_QUERY_VALUE access right. This handle is returned by the RegCreateKeyEx or RegOpenKeyEx function, or it can be one of the following predefined keys: HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_PERFORMANCE_DATA HKEY_PERFORMANCE_NLSTEXT HKEY_PERFORMANCE_TEXT HKEY_USERS Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        _AnShUmAn_ wrote:

                        You are not opening the registry before modifying it.

                        While it's true that the registry key must be opened prior to use, it's certainly not being modified.


                        "The largest fire starts but with the smallest spark." - David Crow

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • C capricious_001

                          Hi guys, I have a function where I am passing a value by reference. Below is the prototype:

                          bool retrieveLogFileLocation(LPSTR&);

                          Now within the body of the function definition I have the following function as well.

                          bool retrieveLogFileLocation(LPSTR& value){
                          char[255] lszValue;
                          HKEY hKey;
                          LONG returnStatus;
                          DWORD dwType = REG_SZ;
                          DWORD dwSize =255;
                          bool success = true;

                          RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);
                          
                          strcpy(value, lszValue);
                          

                          }

                          The problem I am receiving is that whenever my program runs, it crashes instantly, and I know for sure it is an issue with strcpy(value, lszValue). I have tried in multiple ways through type-casting and such to resolve the issue, but I'm perplexed as to whats causing the problem. Any help would be appreciated :D Robbie -- modified at 4:53 Monday 19th June, 2006 PS: I only included bits and pieces of the function definition simply because the actual function definition is very long. :P

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          capricious_001 wrote:

                          bool retrieveLogFileLocation(LPSTR& value){

                          What is the size of the variable that value refers to?

                          capricious_001 wrote:

                          char[255] lszValue;

                          What's this?

                          capricious_001 wrote:

                          RegQueryValueEx(hKey, "InstallationPath", NULL, &dwType, (LPBYTE)&lszValue, &dwSize);

                          What does RegQueryValueEx() return?

                          capricious_001 wrote:

                          strcpy(value, lszValue);

                          Use strncpy() instead.


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups