TheBaker
asked on
Reading a registry value into an LPSTR
I'm writing a DLL that needs to return the value of a registry to VB. Problem is, it needs to return it as an LPSTR (for compatibility).
Any one tell me how?
Any one tell me how?
ASKER
no, perhaps i didn't make myself clear. I can return an LPSTR to VB, but I'm having problems reading the value from the registry into a LPSTR variable. It's that I need help with
@TheBaker:
> no, perhaps i didn't make myself clear. I can return an LPSTR to VB, but I'm
> having problems reading the value from the registry into a LPSTR variable.
> It's that I need help with
If you query a string using RegQueryValue or RegQueryValueEx, then the value will be ANSI or UNICODE depending on whether or not you call the ANSI version (RegQueryValueExA) or the UNICODE version (RegQueryValueExW). Since you want a LPSTR, then you need to call the ANSI version to get an ANSI string (RegQueryValueExA), or you can call the UNICODE version, and then convert to an ANSI string.
Hope That Helps,
Dex*
> no, perhaps i didn't make myself clear. I can return an LPSTR to VB, but I'm
> having problems reading the value from the registry into a LPSTR variable.
> It's that I need help with
If you query a string using RegQueryValue or RegQueryValueEx, then the value will be ANSI or UNICODE depending on whether or not you call the ANSI version (RegQueryValueExA) or the UNICODE version (RegQueryValueExW). Since you want a LPSTR, then you need to call the ANSI version to get an ANSI string (RegQueryValueExA), or you can call the UNICODE version, and then convert to an ANSI string.
Hope That Helps,
Dex*
ASKER
cheers. Any chance of some code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers Dexstar (Dex*)
ASKER
I know I've already awarded the points, but if anyone could help me sort it out, there'll be more points in it! My code is shown below. I've tried todo what Dex* said, but it doesn't work (or I'm doing something wrong!
LPSTR __stdcall GetCustomFormat(int iCustom){
LPSTR lpReturn = "Coming Soon...";
HKEY hKey;
DWORD dwDisposition;
DWORD DataType;
DWORD DataCount=65;
RegCreateKeyEx(HKEY_CURREN T_USER, "Software\\MyApp", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
RegQueryValueExA(hKey, "Custom1", 0, &DataType,(unsigned char*)lpReturn, &DataCount);
RegCloseKey(hKey);
return lpReturn;
}
LPSTR __stdcall GetCustomFormat(int iCustom){
LPSTR lpReturn = "Coming Soon...";
HKEY hKey;
DWORD dwDisposition;
DWORD DataType;
DWORD DataCount=65;
RegCreateKeyEx(HKEY_CURREN
RegQueryValueExA(hKey, "Custom1", 0, &DataType,(unsigned char*)lpReturn, &DataCount);
RegCloseKey(hKey);
return lpReturn;
}
The main issue for this is memory... Who allocates and who frees it? The problem is that you don't know how much memory is even needed. The key there is to call RegQueryValueExA with a NULL param for the data. In that case, it will just fill in the size for you, and then you can allocate the memory. Like this:
RegQueryValueExA(hKey, "Custom1", 0, &DataType, NULL, &DataCount);
Then you can allocate the memory:
DataCount++; // Add one for NULL
LPSTR lpReturn = new char[DataCount];
RegQueryValueExA(hKey, "Custom1", 0, &DataType,(unsigned char*)lpReturn, &DataCount);
Then you can return lpReturn, but the caller is responsible for freeing the memory.
You should also check the return values for those API calls, and handle errors. Your problem could be one of those APIs are failing.
Dex*
RegQueryValueExA(hKey, "Custom1", 0, &DataType, NULL, &DataCount);
Then you can allocate the memory:
DataCount++; // Add one for NULL
LPSTR lpReturn = new char[DataCount];
RegQueryValueExA(hKey, "Custom1", 0, &DataType,(unsigned char*)lpReturn, &DataCount);
Then you can return lpReturn, but the caller is responsible for freeing the memory.
You should also check the return values for those API calls, and handle errors. Your problem could be one of those APIs are failing.
Dex*
ASKER
Cheers that works, but if the value = "" I get the following: Íýýýý
Any ideas how to check whether lpReturn = Íýýýý then change lpReturn to "".
I've tried:
if(lpReturn == "Íýýýý"){
lpReturn = "";
}
but it didn't work. I also tried:
if(lpReturn == NULL){
lpReturn = "";
}
but again, it didn't work.
Any ideas how to check whether lpReturn = Íýýýý then change lpReturn to "".
I've tried:
if(lpReturn == "Íýýýý"){
lpReturn = "";
}
but it didn't work. I also tried:
if(lpReturn == NULL){
lpReturn = "";
}
but again, it didn't work.
Okay, if the value is empty, then after the first call to RegQueryValueExA, then DataCount will be 0. So check for that.
if ( DataCount > 0 )
{
DataCount++; // Add one for NULL
LPSTR lpReturn = new char[DataCount];
RegQueryValueExA(hKey, "Custom1", 0, &DataType,(unsigned char*)lpReturn, &DataCount);
return lpReturn
}
else
{
// The value is empty
return NULL;
}
if ( DataCount > 0 )
{
DataCount++; // Add one for NULL
LPSTR lpReturn = new char[DataCount];
RegQueryValueExA(hKey, "Custom1", 0, &DataType,(unsigned char*)lpReturn, &DataCount);
return lpReturn
}
else
{
// The value is empty
return NULL;
}
ASKER
Cheers Dexstar. I'll give you 50 extra points for that help, but you'll need to tell me how I can give them to you since this post is closed.
Sorry, I have been out of the office sick for the past few days... :|
There are 2 ways to do it. One way it to post a "Points For Dexstar" question, and then post a link to this question in there. Then I'll post something and you accept my answer... The other way is to post a question in the Community Support forum, and ask them to re-open this question. Then you can change the points value, and re-accept an answer.
Info on "Points For" questions: https://www.experts-exchange.com/help.jsp#hi76
Info on re-opening this question: https://www.experts-exchange.com/help.jsp#hi75
I think the first way is easier because you don't need to involve the Moderators, but whatever you feel comfortable with.
There are 2 ways to do it. One way it to post a "Points For Dexstar" question, and then post a link to this question in there. Then I'll post something and you accept my answer... The other way is to post a question in the Community Support forum, and ask them to re-open this question. Then you can change the points value, and re-accept an answer.
Info on "Points For" questions: https://www.experts-exchange.com/help.jsp#hi76
Info on re-opening this question: https://www.experts-exchange.com/help.jsp#hi75
I think the first way is easier because you don't need to involve the Moderators, but whatever you feel comfortable with.
ASKER
Dexstar, get your extra point here: https://www.experts-exchange.com/questions/20808856/For-Dexstar-re-Reading-a-registry-value-into-an-LPSTR.html
An LPSTR as you know is just a pointer to a regular null-terminated string.