Solved

converting between different type of strings

Posted on 2014-10-15
3
174 Views
Last Modified: 2014-10-15
In one place I want to get the file path of the program as a std::string, so I use this function:

      char path[MAX_PATH];
      GetModuleFileName(NULL, path, MAX_PATH);

However the GetModuleFileName function will not take the path variable. It says "argument of type char * in incompatible with with parameter of type LPSWSTR

What's the right way to do this?
0
Comment
Question by:rustycp
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40382724
Either set your project to use MBCS instead of UNICODE (ALT+F7 to open your project's properties, locate 'Use character set' in the 'General' category) or use UNICODE arrays, e.g.

      wchar_t path[MAX_PATH];
      GetModuleFileName(NULL, path, MAX_PATH);
      // Note: you will need a std::wstring now

Open in new window


The 3rd alternative is TCHARs, which is a layer of types and functions that VC++ uses to map between MBCS and UNICODE, i.e.

#include <tchar.h>

// ...

      TCHAR path[MAX_PATH];
      GetModuleFileName(NULL, path, MAX_PATH);

Open in new window



See also http://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc and http://msdn.microsoft.com/en-us/library/bb415628.aspx ("TCHAR") regarding the latter.
0
 
LVL 2

Author Comment

by:rustycp
ID: 40382740
perfect thank you!
0
 
LVL 86

Expert Comment

by:jkr
ID: 40382761
You're welcome ;o)

As a side not, if you really want to convert between ANSI and UNICODE, see http://msdn.microsoft.com/en-us/library/5d7tc9zw.aspx ("wcstombs") and http://msdn.microsoft.com/en-us/library/k1f9b8cy.aspx ("mbstowcs")
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question