Problems with changing values returned by RegQueryValueEx

Reply

Join Date: Jun 2005
Posts: 11
Reputation: aaron_tan is an unknown quantity at this point 
Solved Threads: 0
aaron_tan aaron_tan is offline Offline
Newbie Poster

help on data type conversions

 
0
  #1
Jun 7th, 2005
I desperately need help on data type conversions. I am working on converting a set of codes in Visual Basic to C but i'm faced with a lot of problems dealing with strings. Can anyone help me out here? Below is the code I have in Visual Basic.

  1. Private Function RegGetStringValue(ByVal hKey As Long, _
  2. ByVal sValue As String, _
  3. dwDataSize As Long) As String
  4.  
  5. Dim sDataRet As String
  6. Dim dwDataRet As Long
  7. Dim success As Long
  8. Dim pos As Long
  9.  
  10. 'returns number of spaces based on dwDataSize
  11. sDataRet = Space$(dwDataSize)
  12.  
  13. 'get the size of the null terminating char
  14. dwDataRet = Len(sDataRet)
  15.  
  16. success = RegQueryValueEx(hKey, sValue, _
  17. ByVal 0&, dwDataSize, _
  18. ByVal sDataRet, dwDataRet)
  19.  
  20. If success = ERROR_SUCCESS Then
  21. If dwDataRet > 0 Then
  22.  
  23. pos = InStr(sDataRet, Chr$(0))
  24. RegGetStringValue = Left$(sDataRet, pos - 1)
  25.  
  26. End If
  27. End If
  28. End Function
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 11
Reputation: aaron_tan is an unknown quantity at this point 
Solved Threads: 0
aaron_tan aaron_tan is offline Offline
Newbie Poster

Problems with changing values returned by RegQueryValueEx

 
0
  #2
Jun 8th, 2005
<< moderator edit: merged threads >>

I manage to get the value from the registry with the function below but its in LPBYTE. I need to change the value to an int format so that i can perform arithmetic processes on it. I tried type casting it using a DWORD pointer(in this case, the pointer name is Test), but it returned rubbish values, which was not the value from the registry. Can anyone please help?


  1. DWORD RegGetStringValue(HKEY hKey,LPTSTR sValue,DWORD* dwDataSize)
  2. {
  3. BYTE *DataRet=(BYTE*)malloc(sizeof(*dwDataSize));
  4. DWORD *DataRetSize=(DWORD*)malloc(sizeof(DWORD*));
  5. long success;
  6.  
  7. DWORD* Test=(DWORD*)malloc(sizeof(DWORD*));
  8.  
  9. success = RegQueryValueEx(hKey,(LPCTSTR)sValue,NULL,NULL,DataRet,DataRetSize);
  10.  
  11. Test = (DWORD*)DataRet; //Tried type casting, but returns rubbish values
  12.  
  13. if(success == ERROR_SUCCESS)
  14. return (DWORD)*DataRet;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 11
Reputation: aaron_tan is an unknown quantity at this point 
Solved Threads: 0
aaron_tan aaron_tan is offline Offline
Newbie Poster

Re: Problems with changing values returned by RegQueryValueEx

 
0
  #3
Jun 8th, 2005
By the way, the registry value i'm querying is of REG_SZ, which is a string value
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problems with changing values returned by RegQueryValueEx

 
0
  #4
Jun 9th, 2005
I had been messing with this a bit from the original, but I wasn't exactly sure of what it was the VB was doing. Could you provide an example of what the function is intended to do? Something along the lines of...
[Background information about function usage.]

The function
  1. DWORD RegGetStringValue(HKEY hKey,LPTSTR sValue,DWORD* dwDataSize)
is used to retrieve a string from the registry. The example on MSDN is not sufficient because [...].

I have already done a [...] to get the hKey that is passed to this function. An example value would be [...]. The sValue is where the string will go, and dwDataSize is a pointer to a variable containing its maximum length.

I realize that the calling code must free() the pointer returned by this function. An example usage would be like this.
   /* setup code */
   retval = RegGetStringValue(hKey, "", 1);
   /* cleanup code */
More information will make the question easier to answer correctly.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC