Solved

converting between different type of strings

Posted on 2014-10-15
3
176 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 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