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. Issues with pointers [modified]

Issues with pointers [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 3 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 there, I am basically having trouble acquiring the pointed value of a pointer. Below is the code I am working with.

    //--- main start ---
    
    int main( void ){
    
    	char* RegLocation = "";
    
    
    	char rspath[255];
    	strcpy(rspath,"");
    
    	
    	if(GetRegistryValue(HKEY_LOCAL_MACHINE,"SOFTWARE\\Red Storm Entertainment\\Rogue Spear","InstallationPath",rspath)){
    		
    		lstrcat(rspath, "\\");
    		lstrcpy(RegLocation, rspath);
    	
    		if(retrieveLogFileLocation(RegLocation)){
    			
    			if(GenerateStats(RegLocation, hOutput)){
    				//Continue on
    			}
    		}
    	}
    	
    
    	return 0;
    }
    
    
    
    BOOL GetRegistryValue(HKEY key, const char* path, const char* name, char* value)
    {
    	HKEY hKey;
    
    	strcpy(value,"");
    	if (RegOpenKeyEx(key, path, 0, KEY_EXECUTE, &hKey) == ERROR_SUCCESS)
    	{
    		char szBuf[MAX_PATH];
    		DWORD dwLen=MAX_PATH;
    		
    		if (RegQueryValueEx(hKey, name, NULL, NULL, (unsigned char*) szBuf, &dwLen) == ERROR_SUCCESS)
    		{
    			strcpy(value,szBuf);
    			RegCloseKey(hKey);
    			return TRUE;
    		}
    
    		RegCloseKey(hKey);		
    		hKey = NULL;
    	}
    
    	return FALSE;
    }
    
    bool retrieveLogFileLocation(LPSTR lszRSPath){
    	char lszValue[255];
    	char search[255];
    	
    	
    	lstrcpy(search,lszRSPath);
    	lstrcpy(lszValue,lszRSPath);
    	lstrcat(search, "*.log");
    
    	WIN32_FIND_DATA wfd;
    	HANDLE hFind = FindFirstFile(search, &wfd);	
    
    	
    	if (hFind != INVALID_HANDLE_VALUE) {
    		do 
    		{	
    			string foo = wfd.cFileName;			
    			
    			if (foo.find("RSResults") != -1)
    			{		
    
    				lstrcat(lszValue,wfd.cFileName);
    				lstrcpy(lszRSPath, lszValue);
    
    				FindClose(&wfd);
    				return TRUE;		
    			}
    		} while (FindNextFile(hFind, &wfd));
    		FindClose(&wfd);
    	}
    
    	return FALSE;
    
    }
    
    bool GenerateStats(LPSTR lszLogFile){
    
    	HANDLE hLogFile = NULL;
    	bool pSuccess = FALSE;
    
    	
    	cout << "Value pointed is " << lszLogFile << endl;
            cout << "Value dereferenced is " << *lszLogFile << endl;
    
    	//Open log file
    	hLogFile = CreateFile(lszLogFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    	
    	if(hLogFile != INVALID_HANDLE_VALUE){
    		cout << "Able to create file" << endl;
    		//Lock log file
    		DWORD dwFileSize, dwRead;
    		dwFileSize = GetFileSize(hLogFile, NULL);
    
    		if(LockFile(hLogFile, 0, 0, dwFileSize, 0))
    		{			
    			
    			
    			HANDLE hHeapFile;
    			LPVOID lpvHeapFile;
    				
    			//Create File Heap
    			hHeapFile = HeapCreate(0x00040000, dwFileSize, 0);
    			
    
    			if((DWORD)hHeapFile != STATUS_NO_MEMORY)
    			{					
    				
    				lpvHeapFile = HeapAlloc(hHeapFile, HEAP_ZERO_MEMORY, 0);
    				
    
    				//P
    
    J D 2 Replies Last reply
    0
    • C capricious_001

      Hi there, I am basically having trouble acquiring the pointed value of a pointer. Below is the code I am working with.

      //--- main start ---
      
      int main( void ){
      
      	char* RegLocation = "";
      
      
      	char rspath[255];
      	strcpy(rspath,"");
      
      	
      	if(GetRegistryValue(HKEY_LOCAL_MACHINE,"SOFTWARE\\Red Storm Entertainment\\Rogue Spear","InstallationPath",rspath)){
      		
      		lstrcat(rspath, "\\");
      		lstrcpy(RegLocation, rspath);
      	
      		if(retrieveLogFileLocation(RegLocation)){
      			
      			if(GenerateStats(RegLocation, hOutput)){
      				//Continue on
      			}
      		}
      	}
      	
      
      	return 0;
      }
      
      
      
      BOOL GetRegistryValue(HKEY key, const char* path, const char* name, char* value)
      {
      	HKEY hKey;
      
      	strcpy(value,"");
      	if (RegOpenKeyEx(key, path, 0, KEY_EXECUTE, &hKey) == ERROR_SUCCESS)
      	{
      		char szBuf[MAX_PATH];
      		DWORD dwLen=MAX_PATH;
      		
      		if (RegQueryValueEx(hKey, name, NULL, NULL, (unsigned char*) szBuf, &dwLen) == ERROR_SUCCESS)
      		{
      			strcpy(value,szBuf);
      			RegCloseKey(hKey);
      			return TRUE;
      		}
      
      		RegCloseKey(hKey);		
      		hKey = NULL;
      	}
      
      	return FALSE;
      }
      
      bool retrieveLogFileLocation(LPSTR lszRSPath){
      	char lszValue[255];
      	char search[255];
      	
      	
      	lstrcpy(search,lszRSPath);
      	lstrcpy(lszValue,lszRSPath);
      	lstrcat(search, "*.log");
      
      	WIN32_FIND_DATA wfd;
      	HANDLE hFind = FindFirstFile(search, &wfd);	
      
      	
      	if (hFind != INVALID_HANDLE_VALUE) {
      		do 
      		{	
      			string foo = wfd.cFileName;			
      			
      			if (foo.find("RSResults") != -1)
      			{		
      
      				lstrcat(lszValue,wfd.cFileName);
      				lstrcpy(lszRSPath, lszValue);
      
      				FindClose(&wfd);
      				return TRUE;		
      			}
      		} while (FindNextFile(hFind, &wfd));
      		FindClose(&wfd);
      	}
      
      	return FALSE;
      
      }
      
      bool GenerateStats(LPSTR lszLogFile){
      
      	HANDLE hLogFile = NULL;
      	bool pSuccess = FALSE;
      
      	
      	cout << "Value pointed is " << lszLogFile << endl;
              cout << "Value dereferenced is " << *lszLogFile << endl;
      
      	//Open log file
      	hLogFile = CreateFile(lszLogFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
      	
      	if(hLogFile != INVALID_HANDLE_VALUE){
      		cout << "Able to create file" << endl;
      		//Lock log file
      		DWORD dwFileSize, dwRead;
      		dwFileSize = GetFileSize(hLogFile, NULL);
      
      		if(LockFile(hLogFile, 0, 0, dwFileSize, 0))
      		{			
      			
      			
      			HANDLE hHeapFile;
      			LPVOID lpvHeapFile;
      				
      			//Create File Heap
      			hHeapFile = HeapCreate(0x00040000, dwFileSize, 0);
      			
      
      			if((DWORD)hHeapFile != STATUS_NO_MEMORY)
      			{					
      				
      				lpvHeapFile = HeapAlloc(hHeapFile, HEAP_ZERO_MEMORY, 0);
      				
      
      				//P
      
      J Offline
      J Offline
      Justin Tay
      wrote on last edited by
      #2

      Didn't look through the whole code since I see similar errors littered all over.

      char* RegLocation = "";
      :
      lstrcpy(RegLocation, rspath);

      You should ask yourself where RegLocation is pointing to, and how much memory is available at that location. You should also ask yourself if modifying a string literal makes sense. Any reason why you are not using std::string or CString?

      1 Reply Last reply
      0
      • C capricious_001

        Hi there, I am basically having trouble acquiring the pointed value of a pointer. Below is the code I am working with.

        //--- main start ---
        
        int main( void ){
        
        	char* RegLocation = "";
        
        
        	char rspath[255];
        	strcpy(rspath,"");
        
        	
        	if(GetRegistryValue(HKEY_LOCAL_MACHINE,"SOFTWARE\\Red Storm Entertainment\\Rogue Spear","InstallationPath",rspath)){
        		
        		lstrcat(rspath, "\\");
        		lstrcpy(RegLocation, rspath);
        	
        		if(retrieveLogFileLocation(RegLocation)){
        			
        			if(GenerateStats(RegLocation, hOutput)){
        				//Continue on
        			}
        		}
        	}
        	
        
        	return 0;
        }
        
        
        
        BOOL GetRegistryValue(HKEY key, const char* path, const char* name, char* value)
        {
        	HKEY hKey;
        
        	strcpy(value,"");
        	if (RegOpenKeyEx(key, path, 0, KEY_EXECUTE, &hKey) == ERROR_SUCCESS)
        	{
        		char szBuf[MAX_PATH];
        		DWORD dwLen=MAX_PATH;
        		
        		if (RegQueryValueEx(hKey, name, NULL, NULL, (unsigned char*) szBuf, &dwLen) == ERROR_SUCCESS)
        		{
        			strcpy(value,szBuf);
        			RegCloseKey(hKey);
        			return TRUE;
        		}
        
        		RegCloseKey(hKey);		
        		hKey = NULL;
        	}
        
        	return FALSE;
        }
        
        bool retrieveLogFileLocation(LPSTR lszRSPath){
        	char lszValue[255];
        	char search[255];
        	
        	
        	lstrcpy(search,lszRSPath);
        	lstrcpy(lszValue,lszRSPath);
        	lstrcat(search, "*.log");
        
        	WIN32_FIND_DATA wfd;
        	HANDLE hFind = FindFirstFile(search, &wfd);	
        
        	
        	if (hFind != INVALID_HANDLE_VALUE) {
        		do 
        		{	
        			string foo = wfd.cFileName;			
        			
        			if (foo.find("RSResults") != -1)
        			{		
        
        				lstrcat(lszValue,wfd.cFileName);
        				lstrcpy(lszRSPath, lszValue);
        
        				FindClose(&wfd);
        				return TRUE;		
        			}
        		} while (FindNextFile(hFind, &wfd));
        		FindClose(&wfd);
        	}
        
        	return FALSE;
        
        }
        
        bool GenerateStats(LPSTR lszLogFile){
        
        	HANDLE hLogFile = NULL;
        	bool pSuccess = FALSE;
        
        	
        	cout << "Value pointed is " << lszLogFile << endl;
                cout << "Value dereferenced is " << *lszLogFile << endl;
        
        	//Open log file
        	hLogFile = CreateFile(lszLogFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
        	
        	if(hLogFile != INVALID_HANDLE_VALUE){
        		cout << "Able to create file" << endl;
        		//Lock log file
        		DWORD dwFileSize, dwRead;
        		dwFileSize = GetFileSize(hLogFile, NULL);
        
        		if(LockFile(hLogFile, 0, 0, dwFileSize, 0))
        		{			
        			
        			
        			HANDLE hHeapFile;
        			LPVOID lpvHeapFile;
        				
        			//Create File Heap
        			hHeapFile = HeapCreate(0x00040000, dwFileSize, 0);
        			
        
        			if((DWORD)hHeapFile != STATUS_NO_MEMORY)
        			{					
        				
        				lpvHeapFile = HeapAlloc(hHeapFile, HEAP_ZERO_MEMORY, 0);
        				
        
        				//P
        
        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        capricious_001 wrote:

        char rspath[255]; strcpy(rspath,"");

        Use this instead:

        char rspath[255] = {0};

        capricious_001 wrote:

        lstrcat(rspath, "\\");

        What if rspath already ends with a backslash? Use PathAddBackslash() instead. Why mix strcpy() with lstrcpy()?

        capricious_001 wrote:

        LPSTR lpcsLogFile; lstrcpy(lpcsLogFile, lszLogFile); //or this lstrcpy(lpcsLogFile, static_cast(lszLogFile));

        This will not work since lpcsLogFile is a pointer that is not pointing to allocated memory.


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

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

        C 1 Reply Last reply
        0
        • D David Crow

          capricious_001 wrote:

          char rspath[255]; strcpy(rspath,"");

          Use this instead:

          char rspath[255] = {0};

          capricious_001 wrote:

          lstrcat(rspath, "\\");

          What if rspath already ends with a backslash? Use PathAddBackslash() instead. Why mix strcpy() with lstrcpy()?

          capricious_001 wrote:

          LPSTR lpcsLogFile; lstrcpy(lpcsLogFile, lszLogFile); //or this lstrcpy(lpcsLogFile, static_cast(lszLogFile));

          This will not work since lpcsLogFile is a pointer that is not pointing to allocated memory.


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

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

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

          Thanks a lot guys, That worked. This is the change I made. char rspath[255] = {0}; char* RegLocation = rspath; As well as removed lstrcat(rspath, "\\"); and replaced it with PathAddBackslash(). Even though that hkey value will most likely never have a backslash I put it in just in case. As well I will keep that function just in case I need it for the future so thank you for that David. And yes strcpy() was actually lstrcpy() I just forgot the l. btw what were the other errors that I made hfry? Just so it doesnt bites me in the ass when I'm done this module. Thanks, Robbie -- modified at 14:11 Tuesday 20th June, 2006

          J 1 Reply Last reply
          0
          • C capricious_001

            Thanks a lot guys, That worked. This is the change I made. char rspath[255] = {0}; char* RegLocation = rspath; As well as removed lstrcat(rspath, "\\"); and replaced it with PathAddBackslash(). Even though that hkey value will most likely never have a backslash I put it in just in case. As well I will keep that function just in case I need it for the future so thank you for that David. And yes strcpy() was actually lstrcpy() I just forgot the l. btw what were the other errors that I made hfry? Just so it doesnt bites me in the ass when I'm done this module. Thanks, Robbie -- modified at 14:11 Tuesday 20th June, 2006

            J Offline
            J Offline
            Justin Tay
            wrote on last edited by
            #5

            Fixing RegLocation to point to some allocated memory would fix the problems. It was all the same type of error, basically when using strcpy / strcat etc you need to ensure that the destination buffer has enough memory to store the resultant string.

            C 1 Reply Last reply
            0
            • J Justin Tay

              Fixing RegLocation to point to some allocated memory would fix the problems. It was all the same type of error, basically when using strcpy / strcat etc you need to ensure that the destination buffer has enough memory to store the resultant string.

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

              Yup I fixed that and everything is running more smoothly!

              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