The output from this only returns the letter 'C' in the path string.

Help!

Wizzsm.

std::string GetServiceConfig()
{
HKEY keyHandle;
char rgValue [1024];
LPCTSTR regPath = L"SYSTEM\\CurrentControlSet\\Services\\ApirbiService";
LPCTSTR regReq = L"ImagePath";
DWORD size1 = sizeof(rgValue);
DWORD Type;
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath,0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS)
{
    RegQueryValueEx( keyHandle, regReq, NULL, &Type, (LPBYTE)rgValue,&size1); 
}
RegCloseKey(keyHandle);
std::string path(rgValue);
printf(" Binary path: %s\n", path);
return path;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

String path can't just be placed in a printf like that, it has to be c-string format: path.c_str() Why not just use cout since everything is taken care of for you, if you're not willing to format it.

And you might want to read the forum rules - IMMEDIATELY! Pay attention to that part about "code tags".

1. You got UNICODE string so convert it in (almost) ordinar C-string before pass it to std::string constructor.

char cstrValue[1024];
...
if (WideCharToMultiByte(
    CP_ACP, 0,
    rgValue,-1,cstrValue,sizeof cstrValue,
    0,0))
{
    // you got it! You may printf cstrValue
}

2. Don't pass std::string in printf directly, std::string object is not a pointer to string contents so your program has 100% chance to crash. Use c_str() member function:

printf("Path %s\n",path.c_str());

Why printf? cout << path << endl;

Thank you so much for your solutions. I actually tried the WideCharToMultiByte() converter, I am not sure why, but it returned only the first char also.

Well I looked at the returned string and found the entire string was, in fact fully located inside the char rgValue[1024]. Obviously due to MS need to support multilingual codesets. I Simply peeled off every other char to reassemble the utf8 string I needed.

globally defined

char dirpath[1024];

BOOL GetServiceConfig()
{
	HKEY keyHandle;
	char rgValue [1024];
	LPCTSTR regPath = L"SYSTEM\\CurrentControlSet\\Services\\ApirbiService";
	LPCTSTR regReq = L"ImagePath";
	DWORD size1 = sizeof(rgValue);
	DWORD Type;
	if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, regPath,0, KEY_QUERY_VALUE, &keyHandle) == ERROR_SUCCESS)
	{
		RegQueryValueEx( keyHandle, regReq, NULL, &Type, (LPBYTE)rgValue,&size1); 
	}
	RegCloseKey(keyHandle);

	int ndx=0;
	int szRgVal=sizeof(rgValue);
	for(int i=0;i<szRgVal;i++)
	{
		dirpath[ndx++]=rgValue[i];
		i++;
	}
	int maxStr=strlen(dirpath);
	dirpath[maxStr-18]=(char)'\0';
	printf(" Binary path: %s %i\n", dirpath, strlen(dirpath) );

	return true;
}

Thank you for the

printf()

notes. Why MS decided to create even more

std::string

strings makes me want to laugh.

Wizzsm.

Sorry about the messy bracketing. I am new here (duh:)!

printf()

Wizzsm.

This question has already been answered. Start a new discussion instead.