0

Question: How to use string/char* variable as the path parameter to GetPrivateProfileInt method.

I m trying to use GetPrivateProfileInt given for windows. The following code runs perfeclty without any issue:

int x = GetPrivateProfileInt(L"x",L"y",1,L"This\\is\\the\\path");

But in my case, the path is being passed to the function. Something like this:

void fun(std::string path)
{
  //error const char* is incampatible with LPCWSTR.
  int x = GetPrivateProfileInt(L"x",L"y",1,path.c_str());
}

In some attempts given below, x is recieving the default value. i.e. the path is not being passed to the GetPrivateProfileInt method correctly.

Following are several other attempts made by me:

Attempt1:

// No error, default value is being read.
int x = GetPrivateProfileInt(L"x",L"y",1,(LPCTSTR)path.c_str());

Attempt2:

// No error, default value is being read.
int x = GetPrivateProfileInt(L"x",L"y",1,(wchar_t*)path.c_str());

Attempt3:

//_T() macro giving error.
// 'Ls' : undeclared identifier.identifier "Ls" is undefined.
LPCTSTR path_s = _T(path.c_str());
int x = GetPrivateProfileInt(L"x",L"y",1,path_s);

I went through the answers here but not able find out the solution.

1 Answer 1

1

There are two versions of the function, one takes UCS-2 characters (GetPrivateProfileIntW) and one takes char characters (GetPrivateProfileIntA). There are no versions which allow you to mix the parameters. Your options are to either change the appname and keyname parameters to single-byte to match your data

GetPrivateProfileIntA("x", "y", 1, path.c_str());

or to convert the last parameter to UCS-2 using MultibyteToWideChar, then call GetPrivateProfileIntW.

Pointer-casting is NOT a conversion of the character encoding, and will not work. The compiler type system is there to help you, and making it shut up with a cast is nearly always the wrong thing to do (exception: the return value of GetProcAddress does need a cast).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.