CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2008
    Posts
    10

    converting from LPWSTR to char *

    Hi, I am trying to convert an LPWSTR, specifically from an OPENFILENAME struct, to a char *.

    so I have my code it looks like

    OPENFILENAME ofn;

    ...

    char *fname = (char *)ofn.lpstrFile; //this is what I tried and it didnt work


    so pretty much I was wondering if there was some simple way of taking the file path from my OPENFILENAME struct and putting it in a useful char *.

  2. #2
    Join Date
    May 2002
    Posts
    1,435

    Re: converting from LPWSTR to char *

    You must have a UNICODE build if ofn.lpstrFile is LPWSTR. There is rarely a need for mixing char* and wchar_t* in the same build so can you explain why you need char*?

  3. #3
    Join Date
    Jul 2008
    Posts
    10

    Re: converting from LPWSTR to char *

    well, I am using the file path that I got from ofn.lpstrFile to open a file using fopen. The first parameter of fopen is a char *. I am used to working with C, so I am not sure if this is the best way of doing this in c++.

  4. #4
    Join Date
    Apr 2008
    Posts
    133

    Re: converting from LPWSTR to char *

    For wide char's with LPWSTR ..use _wfopen_s(..)

  5. #5
    Join Date
    May 2002
    Posts
    1,435

    Re: converting from LPWSTR to char *

    Virtually every c-runtime function, API function, etc. has a wide character equivalent. You should be using _wfopen() instead of fopen(). When developing your code you will quite often run into this problem. Assuming that you have help installed press F1 on fopen() (or any other function that takes a string parameter) and you will find the wide character equivalent.

  6. #6
    Join Date
    Jul 2008
    Posts
    10

    Re: converting from LPWSTR to char *

    Thanks for the info, I am using the _wfopen_s instead of fopen.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured