How can translate const char to const wchar_t? I have already climbed a lot of sites, have not found an answer ...

#include "windows.h" #include "tlhelp32.h" #define dllx extern "C" __declspec(dllexport) //--------------------------------------------------------------------------- // find is process processName running? bool _IsProcessRun( const char * processName ) { HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); PROCESSENTRY32 pe; pe.dwSize = sizeof( PROCESSENTRY32 ); Process32First( hSnapshot, &pe ); while( true ) { if( wcscpy( pe.szExeFile, processName ) == 0 ) return true; if( !Process32Next( hSnapshot, &pe ) ) return false; } } // return value to gml dllx double IsProcessRun( char *process ) { return (int) _IsProcessRun( process ); } 

1 answer 1

you can use mbstowcs in the code it will be something like this

 #include <cstdlib> #include <cwchar> #include <memory> #include <string> std::wstring to_wstring(const char* str) { std::unique_ptr<wchar_t[]> tmp = nullptr; size_t sz, len; len = mbstowcs(nullptr, str, 0); // получить размер sz = len + 1; tmp.reset(new wchar_t[sz]); // выделить память mbstowcs(tmp.get(), str, sz); // перекодировать return std::wstring(tmp.get()); } std::wstring to_wstring(const std::string& str) { return to_wstring(str.c_str()); } bool _IsProcessRun( const char * processName ) { HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 ); PROCESSENTRY32 pe; pe.dwSize = sizeof( PROCESSENTRY32 ); Process32First( hSnapshot, &pe ); std::wstring name = to_wstring(processName); while( Process32Next( hSnapshot, &pe ) ) { if( name == pe.szExeFile ) return true; } return false; } 
  • the result was this code: pp.vk.me/c633417/v633417732/2c4e2/p6qyFNusOMo.jpg and these beautiful messages in red pp.vk.me/c633417/v633417732/2c4da/PqQ9Qud2DN8.jpg - SweetCetea wephea we need to write a message.
  • or is something wrong? .. - SweetCelestia
  • #define _CRT_SECURE_NO_WARNINGS should be added in front of all #include in the file stdafx.h - Maxim Timakov
  • Redesigned your search function process - Maxim Timakov
  • one
    @SweetCelestia It is better to really use mbstowcs_s ( en.cppreference.com/w/c/string/multibyte/mbstowcs ), at least if you have to work in a multi-threaded environment: mbstowcs stores the state and will be fun to call it from different functions and from different threads. Or use mbsrtowcs - in it the state is stored outside and you control it. - Monah Tuk