Vroom ZOOM
Vroom ZOOM

Reputation: 31

const char* to WCHAR?

hi I need a WCHAR or some other LPCWSTR for the textout function. I have another function that outputs a string:

string run_TypeSt(char op_Sel) {
switch (op_Sel) {
case 'm'://model
{
    searchv_Str = "boatsmodel_s";
    return parseFilestr(42, true);

}
case 't'://type (this is going to be really tricky)
{

}
case 'i'://make
{
    searchv_Str = "boatsmake_s";
}
}

parseFilestr just returns a string:

string parseFilestr(int start, bool tweak) 
{

//lots of omitted code here
string info_Str = "hello";
return info_Str;//return the requested data
}

my header file looks like this:

#pragma once
#include <string>
    int parseFilenum(int start, bool tweak);
    int run_Type(char op_Sel);
    std::string parseFilestr(int start, bool tweak);
    std::string run_TypeSt(char op_Sel);

The execution looks like this:

#include <string>

using namespace std;

string var;

int APIENTRY wWinMain(...)
{
var = run_TypeSt('m');
}

LRESULT CALLBACK WndProc(...)
{
 case WM_PAINT:
        {

        TextOut(hdc, 5, 5, var, 6);// issue here, var is not compatible with TextOut. Solutions?
        break;
}

The stupid question is, how do I change var to a type that will work with TextOut? Or maybe there is an alternative to textout? Ive been busting my head over this and can't seem to figure it out. Thanks

Upvotes: 1

Views: 575

Answers (2)

Seva Alekseyev
Seva Alekseyev

Reputation: 61361

Use TextOutA instead. It takes an ANSI string (a [const] char *).

That said, read up on Unicode and the options that you have for widechar/char interaction. In the long run, the whole program should probably be written with widechars, except for the bits where you interact with multibyte (or single-byte) file and wire formats.

Upvotes: 2

JB - MSI
JB - MSI

Reputation: 19

Try this:

std::string FromWideString(const std::wstring& WIDESTRING)
{
   std::string result;
   size_t widelength = WIDESTRING.length();
   char* temp = new char[widelength+2];
   temp[widelength+1]=NULL;
   size_t res = wcstombs(temp, WIDESTRING.c_str(), widelength+1);
   if (res != (size_t) -1)
     result=temp;
   delete temp;
return(result);
  }

Upvotes: 1

Related Questions