none
How to read any string value from registry??? RRS feed

  • Question

  • Hello Friends, 
    I am able to read any hex and simple string value from registry after studying Bruce Eitman link

     http://embedded101.com/Articles/Embedded101Article/tabid/75/ArticleId/10/language/en-US/Windows-CE-Reading-a-String-from-the-Registry.aspx

    and learning from discussion in my own posted link as shown below:-

    http://social.msdn.microsoft.com/Forums/en-US/a933683c-8dcb-4e2d-9f54-10585bee3c52/retailmsg-displays-error-in-reading-registry-value?forum=winembplatdev

    The above link is reading hex and string value from registry. But now, the problem is that the same code is unable to read Dll="Test.dll" value from registry.. There above link code fails..... Why its not reading "Test.dll" from registry ?????????????

    My code is as follows:

    DWORD TST_Init(
      LPCTSTR regPath,
      DWORD dwBusContext
    )
    {
    	HKEY	hKey;
    	DWORD	result;
    	//DWORD Type	=	REG_DWORD;
    	DWORD Type = REG_SZ;
    	TCHAR *MemAlloc = NULL;
    	DWORD Data;
    	DWORD NoBytes;
    	DWORD DataSize	=	sizeof(DWORD);
    	DWORD lcl_error;
    ////	DWORD Type;
    ////
    ////
    ////DWORD cbValue = sizeof(DWORD);
    ////UCHAR RegDataCompare[4]={0};
    ////
    ////DWORD Data=0;
    	DRIVERSHELL_CONTEXT *pDriverContext;
    
    
    	RETAILMSG(TRUE,(TEXT("TST_Init(): START\r\n")));
    
    		////pDriverContext = LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		pDriverContext = (DRIVERSHELL_CONTEXT *)LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		if(!pDriverContext == NULL)
    		{
    			hKey = OpenDeviceKey((LPCTSTR)regPath);// This fxn opens the device registery key,
    				
    			if(!hKey)
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to open registery\r\n")));
    				return FALSE;
    				exit;
    			}
    			else
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Registery key open successfully\r\n")));
    				//result = RegQueryValueEx(hKey, TEXT("Index"), NULL, &Type, (LPBYTE)&Data, &DataSize);
    
    
    				result = RegQueryValueEx(hKey, TEXT("Prefix"), NULL, &Type, NULL, &NoBytes);
    
    				if(NoBytes > 0)
    				{
    					MemAlloc = (TCHAR *)malloc(NoBytes);
    					if(MemAlloc != NULL)
    					{
    						result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, LPBYTE(MemAlloc), &NoBytes);
    						if(result == ERROR_SUCCESS)
    						{
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Registry values read successfully.\r\n"))); 
    							pDriverContext->StrngVal = MemAlloc;
    
    							RETAILMSG(TRUE, (TEXT("TEST_Init(): Registery value is -- %s"),pDriverContext->StrngVal));
    							RegCloseKey(hKey);
    
    						}
    						else
    						{
    							lcl_error = GetLastError();
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): -- %d --%s \r\n"),lcl_error, lcl_error));
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in reading registry values.\r\n")));
    							return FALSE;
    							exit;
    						}
    
    					}
    					else
    					{
    						RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to allocate memory to bytes\r\n")));
    						exit;
    					}
    				}
    					
    				///*result = RegQueryValueEx(hKey, TEXT("Order"), NULL, &Type, (LPBYTE)&Data, &DataSize);*/
    				///*/*result = RegQueryValueEx(hKey,TEXT("Dll"),NULL,&Type,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/*/
    				///*result = RegQueryValueEx(hKey,TEXT("Order"),NULL,NULL,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/
    					//memcpy(&Data,RegDataCompare,sizeof(DWORD));
    							}								// with pointer to the registery
    		}
    		else
    		{
    			RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in allocating memory.\r\n")));
    			return FALSE;
    			exit;
    		}
    
    	
    	
    	RETAILMSG(TRUE,(TEXT("TST_Init(): END\r\n")));
    	return TRUE;
    
    }

    Header file

    typedef struct _DRIVERSHELL_CONTEXT
    {
                    DWORD Instance;
    				TCHAR *StrngVal;
                    
    } DRIVERSHELL_CONTEXT;

    Regvalues in .reg

    [HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Test]
    	"Prefix"="TST"
    	"Dll"="Test.dll"
    	"Index"=dword:1
    	"Order"=dword:10
    	

    Same code is reading hex, simple string values but not reading Test.dll value ????
    Wednesday, March 26, 2014 9:26 AM

Answers

  • Try following code

    DWORD TST_Init(
      LPCTSTR regPath,
      DWORD dwBusContext
    )
    {
    	HKEY	hKey;
    	DWORD	result;
    	//DWORD Type	=	REG_DWORD;
    	DWORD Type = REG_SZ;
    	TCHAR *MemAlloc = NULL;
    	DWORD Data;
    	DWORD NoBytes;
    	DWORD DataSize	=	sizeof(DWORD);
    	DWORD lcl_error;
    ////	DWORD Type;
    ////
    ////
    ////DWORD cbValue = sizeof(DWORD);
    ////UCHAR RegDataCompare[4]={0};
    ////
    ////DWORD Data=0;
    	DRIVERSHELL_CONTEXT *pDriverContext;
    
    
    	RETAILMSG(TRUE,(TEXT("TST_Init(): START\r\n")));
    
    		////pDriverContext = LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		pDriverContext = (DRIVERSHELL_CONTEXT *)LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		if(!pDriverContext == NULL)
    		{
    			hKey = OpenDeviceKey((LPCTSTR)regPath);// This fxn opens the device registery key,
    				
    			if(!hKey)
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to open registery\r\n")));
    				return FALSE;
    				exit;
    			}
    			else
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Registery key open successfully\r\n")));
    				//result = RegQueryValueEx(hKey, TEXT("Index"), NULL, &Type, (LPBYTE)&Data, &DataSize);
    
    
    				result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, NULL, &NoBytes);
    
    				if(NoBytes > 0)
    				{
    					MemAlloc = (TCHAR *)malloc(NoBytes);
    					if(MemAlloc != NULL)
    					{
    						result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, LPBYTE(MemAlloc), &NoBytes);
    						if(result == ERROR_SUCCESS)
    						{
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Registry values read successfully.\r\n"))); 
    							pDriverContext->StrngVal = MemAlloc;
    
    							RETAILMSG(TRUE, (TEXT("TEST_Init(): Registery value is -- %s"),pDriverContext->StrngVal));
    							RegCloseKey(hKey);
    
    						}
    						else
    						{
    							lcl_error = GetLastError();
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): -- %d --%s \r\n"),lcl_error, lcl_error));
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in reading registry values.\r\n")));
    							return FALSE;
    							exit;
    						}
    
    					}
    					else
    					{
    						RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to allocate memory to bytes\r\n")));
    						exit;
    					}
    				}
    					
    				///*result = RegQueryValueEx(hKey, TEXT("Order"), NULL, &Type, (LPBYTE)&Data, &DataSize);*/
    				///*/*result = RegQueryValueEx(hKey,TEXT("Dll"),NULL,&Type,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/*/
    				///*result = RegQueryValueEx(hKey,TEXT("Order"),NULL,NULL,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/
    					//memcpy(&Data,RegDataCompare,sizeof(DWORD));
    							}								// with pointer to the registery
    		}
    		else
    		{
    			RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in allocating memory.\r\n")));
    			return FALSE;
    			exit;
    		}
    
    	
    	
    	RETAILMSG(TRUE,(TEXT("TST_Init(): END\r\n")));
    	return TRUE;
    
    }

    Let me know the result. :)


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com

    Wednesday, March 26, 2014 10:48 AM

All replies

  • Try following code

    DWORD TST_Init(
      LPCTSTR regPath,
      DWORD dwBusContext
    )
    {
    	HKEY	hKey;
    	DWORD	result;
    	//DWORD Type	=	REG_DWORD;
    	DWORD Type = REG_SZ;
    	TCHAR *MemAlloc = NULL;
    	DWORD Data;
    	DWORD NoBytes;
    	DWORD DataSize	=	sizeof(DWORD);
    	DWORD lcl_error;
    ////	DWORD Type;
    ////
    ////
    ////DWORD cbValue = sizeof(DWORD);
    ////UCHAR RegDataCompare[4]={0};
    ////
    ////DWORD Data=0;
    	DRIVERSHELL_CONTEXT *pDriverContext;
    
    
    	RETAILMSG(TRUE,(TEXT("TST_Init(): START\r\n")));
    
    		////pDriverContext = LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		pDriverContext = (DRIVERSHELL_CONTEXT *)LocalAlloc(LMEM_FIXED, sizeof(DRIVERSHELL_CONTEXT));
    		if(!pDriverContext == NULL)
    		{
    			hKey = OpenDeviceKey((LPCTSTR)regPath);// This fxn opens the device registery key,
    				
    			if(!hKey)
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to open registery\r\n")));
    				return FALSE;
    				exit;
    			}
    			else
    			{
    				RETAILMSG(TRUE,(TEXT("TEST_Init(): Registery key open successfully\r\n")));
    				//result = RegQueryValueEx(hKey, TEXT("Index"), NULL, &Type, (LPBYTE)&Data, &DataSize);
    
    
    				result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, NULL, &NoBytes);
    
    				if(NoBytes > 0)
    				{
    					MemAlloc = (TCHAR *)malloc(NoBytes);
    					if(MemAlloc != NULL)
    					{
    						result = RegQueryValueEx(hKey, TEXT("Dll"), NULL, &Type, LPBYTE(MemAlloc), &NoBytes);
    						if(result == ERROR_SUCCESS)
    						{
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Registry values read successfully.\r\n"))); 
    							pDriverContext->StrngVal = MemAlloc;
    
    							RETAILMSG(TRUE, (TEXT("TEST_Init(): Registery value is -- %s"),pDriverContext->StrngVal));
    							RegCloseKey(hKey);
    
    						}
    						else
    						{
    							lcl_error = GetLastError();
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): -- %d --%s \r\n"),lcl_error, lcl_error));
    							RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in reading registry values.\r\n")));
    							return FALSE;
    							exit;
    						}
    
    					}
    					else
    					{
    						RETAILMSG(TRUE,(TEXT("TEST_Init(): Failed to allocate memory to bytes\r\n")));
    						exit;
    					}
    				}
    					
    				///*result = RegQueryValueEx(hKey, TEXT("Order"), NULL, &Type, (LPBYTE)&Data, &DataSize);*/
    				///*/*result = RegQueryValueEx(hKey,TEXT("Dll"),NULL,&Type,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/*/
    				///*result = RegQueryValueEx(hKey,TEXT("Order"),NULL,NULL,\
    				//		(PBYTE)RegDataCompare,&cbValue);*/
    					//memcpy(&Data,RegDataCompare,sizeof(DWORD));
    							}								// with pointer to the registery
    		}
    		else
    		{
    			RETAILMSG(TRUE,(TEXT("TEST_Init(): Error in allocating memory.\r\n")));
    			return FALSE;
    			exit;
    		}
    
    	
    	
    	RETAILMSG(TRUE,(TEXT("TST_Init(): END\r\n")));
    	return TRUE;
    
    }

    Let me know the result. :)


    Keshava G N Member - Technical (Software), iWave Systems, Bangalore <http://iwavesystems.com> mailto: keshavagn AT iwavesystems DOT com

    Wednesday, March 26, 2014 10:48 AM
  • @KeshavaGN

    That was just a silly mistake by slip of hand..... By the way thank you very much for supporting me...

    Wednesday, March 26, 2014 11:00 AM