User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,056 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,230 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 3954 | Replies: 3
Reply
Join Date: Jun 2005
Posts: 11
Reputation: aaron_tan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aaron_tan aaron_tan is offline Offline
Newbie Poster

help on data type conversions

  #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.

Private Function RegGetStringValue(ByVal hKey As Long, _
                                   ByVal sValue As String, _
                                   dwDataSize As Long) As String

   Dim sDataRet As String
   Dim dwDataRet As Long
   Dim success As Long
   Dim pos As Long
   
  'returns number of spaces based on dwDataSize
   sDataRet = Space$(dwDataSize)

  'get the size of the null terminating char
   dwDataRet = Len(sDataRet)
   
   success = RegQueryValueEx(hKey, sValue, _
                             ByVal 0&, dwDataSize, _
                             ByVal sDataRet, dwDataRet)
                             
   If success = ERROR_SUCCESS Then
      If dwDataRet > 0 Then
      
         pos = InStr(sDataRet, Chr$(0))
         RegGetStringValue = Left$(sDataRet, pos - 1)
         
      End If
   End If
End Function
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Posts: 11
Reputation: aaron_tan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aaron_tan aaron_tan is offline Offline
Newbie Poster

Problems with changing values returned by RegQueryValueEx

  #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?


DWORD RegGetStringValue(HKEY hKey,LPTSTR sValue,DWORD* dwDataSize)
{
	BYTE *DataRet=(BYTE*)malloc(sizeof(*dwDataSize));
	DWORD *DataRetSize=(DWORD*)malloc(sizeof(DWORD*));
	long success;

	DWORD* Test=(DWORD*)malloc(sizeof(DWORD*));

	success = RegQueryValueEx(hKey,(LPCTSTR)sValue,NULL,NULL,DataRet,DataRetSize);
		
	Test = (DWORD*)DataRet; //Tried type casting, but returns rubbish values

	if(success == ERROR_SUCCESS)
		return (DWORD)*DataRet;
}
Reply With Quote  
Join Date: Jun 2005
Posts: 11
Reputation: aaron_tan is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
aaron_tan aaron_tan is offline Offline
Newbie Poster

Re: Problems with changing values returned by RegQueryValueEx

  #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  
Join Date: Apr 2004
Posts: 3,450
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Problems with changing values returned by RegQueryValueEx

  #4  
Jun 8th, 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
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 11:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC