Solved

converting between different type of strings

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

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

736 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