Convert int to LPWSTR

How i can convert a int to LPWSTR?
crudely, I think you can..

char str[1000];
sprint(str,"%d", theint);
and if memory serves the LPWSTR will take str directly without any additional casting.

you can also write a lot more code to get the int into a string object and peel the char* out of the string and feed it that. It may accept string directly, you can try it.

Last edited on
LPWSTR -> LONG POINTER WIDE STRING, i will need a const wchar_t not char.
1
2
3
int i=0;
char str[1000];
sprintf(str,"%d", i);


Errors:
$ gcc -shared -o dllmain.o -c dllmain.cpp
dllmain.cpp: In function 'LRESULT BSSSendMessageW(HWND, UINT, WPARAM, LPARAM)':
dllmain.cpp:25:34: error: cannot convert 'char*' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'int MessageBoxW(HWND, LPCWSTR, LPCWSTR, UINT)'
MessageBoxW(NULL,str,str,MH_OK);


this method didnt work for me..
Last edited on
Last edited on
Use MessageBoxA for the UTF-8 version of the function (char).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <windows.h>
#include <cstdlib>

int main()
{
    MessageBoxA( nullptr, "this is the message (narrow characters)", "const char*", 0 ) ;

    MessageBoxW( nullptr, L"this is the message (wide characters)", L"const wchar_t*", 0 ) ;

    const char msg[] = "Another message" ;
    const char caption[] = "Hello" ;

    MessageBoxA( nullptr, msg, caption, 0 ) ;

    wchar_t wmsg[ sizeof(msg) ] {} ;
    // http://en.cppreference.com/w/cpp/string/multibyte/mbstowcs
    std::mbstowcs( wmsg, msg, sizeof(msg) ) ;

    wchar_t wcaption[ sizeof(caption) ] {} ;
    std::mbstowcs( wcaption, caption, sizeof(caption) ) ;

    MessageBoxW( nullptr, wmsg, wcaption, 0 ) ;

    // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ff381407(v=vs.85).aspx
    const TCHAR msg3[] = TEXT( "third message" ) ;
    const TCHAR cap3[] = TEXT( "caption3" ) ;
    MessageBox( nullptr, msg3, cap3, 0 ) ; // note: no A or W suffix
}
Last edited on
Sorry i didnt explain well what i want to get. I was trying to say him that LPWSTR is the same than const wchar_t not const char i dont know why are you saying char will work with LPWSTR???? and i was trying to convert int to const wchar_t ... anything else ..
Last edited on
Run this (console application):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <windows.h>

int main( int argc, char* [] )
{
    // convert int argc to a string and display it in a message box

    std::cout << "invoking MessageBoxA with narrow character strings (UTF-8)\n" << std::flush ;
    const std::string msg = "argc == " + std::to_string(argc) ;
    MessageBoxA( nullptr, msg.c_str(), "const char*", 0 ) ;

    std::wcout << L"invoking MessageBoxW with wide character strings (UTF-16)\n" << std::flush ;
    const std::wstring wmsg = L"argc == " + std::to_wstring(argc) ;
    MessageBoxW( nullptr, wmsg.c_str(), L"const wchar_t*", 0 ) ;
}
sorry, missed the wide char, what they said use the wide char versions.
you would need the wide char strings or compiler settings also if you did it that way. Basically, what they said ^^^
I tried method JLBorges but i dont know why show me linking errors..:
$ gcc -o main.exe main.cpp
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0x9c): undefined reference to `std::cout'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0xa1): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0xa8): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::flush<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0xaf): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0xe7): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0xf1): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0x125): undefined reference to `std::wcout'
C:\msys32\tmp\ccGh8sC6.o:main.cpp:(.text+0x12a): undefined reference to `std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& std::operator<< <wchar_t, std::char_traits<wchar_t> >(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&, wchar_t const*)'......


Complete pastebin of errors...:
http://pastebin.com/rgNu30dp
Last edited on
This shouldn't be happening: those symbols are defined by the standard library. The problem is almost certainly your build configuration.

Above, you posted:
$ gcc -shared -o dllmain.o -c dllmain.cpp
gcc is for C code only, and does not link the C++ standard library. Use g++ instead.
Last edited on
Topic archived. No new replies allowed.