Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998200
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:29:46+00:00 2026-05-20T07:29:46+00:00

I’m having trouble trying to cast a LPCWSTR to an LPCSTR. Some Background I

  • 0

I’m having trouble trying to cast a LPCWSTR to an LPCSTR.

Some Background

I am programming a VC++ 2010 application, and have already run into the situation where I had to convert from type System::String^ to std::string. I used this code to do so:

private: std::string toss(System::String^ s)
    {
        msclr::interop::marshal_context context;
        return context.marshal_as<std::string>(s);
    }

Doing so, I thought I would solve all my problems. I was wrong. From this, I need to cast a std::string as an LPCSTR, to use with some Windows API.

After reading some of the existing questions on SO, I feel like I am stuck. I was told to use:

LPCSTR str = existingstr.c_str();

When I use this, however, I am given the error: Cannot convert from LPCWSTR to LPCSTR

Does anyone have a better idea of what I should do in this specific case, or how I can convert from LPCWSTR to LPCSTR?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:29:47+00:00Added an answer on May 20, 2026 at 7:29 am

    Edit: completely misread the question so disregard my earlier answer, sorry. If you need to later pass this string into a Windows API, do you have a reason for using std::string and not std::wstring?

    Additional edit: there’s some confusion on my part here. What is the type of existingstr and what is the signature of the Windows API function you’re calling? I think that would help clear things up. If existingstr is std::string, then .cstr() is compatible with LPCSTR (i.e. char const*). If existingstr is std::wstring, it is not. Is the API taking LPCSTR or LPCWSTR?

    You could use this instead:

    private: std::wstring toss(System::String^ s)
    {
       std::wstring result;
       {
           pin_ptr<wchar_t const> ptr = PtrToStringChars(s);
           result = ptr;
       }
       return result;
    }

    .c_str() on a std::wstring would result in a wchar_t const* (i.e. a LPCWSTR).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.