Solved

converting between different type of strings

Posted on 2014-10-15
3
179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

615 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