2

By using the below code, I am getting the path "C:\ProgramFiles"

And then appending "\Test\myupdate.exe" to it.

After that I am storing this path in the "pwszTaskTrigger" structure variable like as below:

mcTskInfo.pwszTaskTrigger = strexepath;

But, When storing I am getting a warning message ("no suitable conversion function from "std::wstring" to "LPWSTR" exists"):

Below is the complete Code:

MCTASKINFO mcTskInfo = { 0 };
WCHAR szCommonFiles[MAX_PATH] = { 0 };
lRet = SHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL, 0, szCommonFiles);
std::wstring strexepath = L"";
    strexepath.append(szCommonFiles);  //szCommonFiles gives the path "C:\\ProgramFiles"
    strexepath.append(ADD_MCUPDTPATH);
    mcTskInfo.pwszTaskTrigger = strexepath;

#define ADD_MCUPDTPATH          L"\\Test\\myupdate.exe"


struct MCTASKINFO
{

LPWSTR pwszTaskTrigger; 

};

Here I should not change the structure variable pwszTaskTrigger from LPWSTR to LPCWSTR. Because this file is the include file.

How can I fix this issue without changing LPWSTR to LPCWSTR?

6
  • You can solve the error easily by getting a pointer to the first element of the string (as in &strexepath[0]). But that could lead to other errors if the object strexepath ends its life and gets destructed before the pointer does. Commented Sep 18, 2019 at 10:23
  • 2
    Why not using mcTskInfo.pwszTaskTrigger = strexepath.c_str();? Commented Sep 18, 2019 at 10:24
  • @Gupta that's const wchar_t *. Commented Sep 18, 2019 at 10:24
  • @Quentin What do you mean? In C++11, data() and c_str() are the same, no? Commented Sep 18, 2019 at 10:35
  • 1
    @Gupta In C++17, data() has an overload that returns a non-const pointer. Commented Sep 18, 2019 at 17:40

2 Answers 2

1

You can use:

mcTskInfo.pwszTaskTrigger = &strexepath[0];

Or, in C++17, be more descriptive and use data:

mcTskInfo.pwszTaskTrigger = strexepath.data();

But make extra sure that nothing writes more than the length of the string through that pointer, nor uses it after strexepath's lifetime has ended.

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

7 Comments

Non-const data() is available only since C++17, and this question has C++11 tag.
Hi @Quentin, As you mentioned I changed like: mcTskInfo.pwszTaskTrigger = strexepath.data();
But it is showing the error message: a value of type "const wchar_t*" cannot be assigned to an entity of type "LPWSTR"
@JohnPaulCoder that's because you are using C++11, which sklott pointed out to me. data has been updated since then, but you're restricted to using &strexepath[0] which has the same effect.
Thank you @Quentin, mcTskInfo.pwszTaskTrigger = &strexepath[0]; is working.
|
1

Use the following:

mcTskInfo.pwszTaskTrigger=const_cast<wchar_t*>(strexepath.c_str());

You can also use a C-style cast instead of const_cast. Then the line would look like:

mcTskInfo.pwszTaskTrigger = (LPWSTR)strupdatepath.c_str();

3 Comments

Can I use this way? mcTskInfo.pwszTaskTrigger = (LPWSTR)strupdatepath.c_str(); --- It is also working.
I added the possibility of using a C-style cast to my answer.
You don't need the cast: mcTskInfo.pwszTaskTrigger = &strexepath[0];, or mcTskInfo.pwszTaskTrigger = strexepath.data(); in C++17.

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.