Solved

converting between different type of strings

Posted on 2014-10-15
3
166 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
Comment Utility
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
Comment Utility
perfect thank you!
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now