I'm trying to get the path of my executable on my windows machine.
std::string get_exe_path_dir()
{
wchar_t path[MAX_PATH];
GetModuleFileName( NULL, path, MAX_PATH );
PathRemoveFileSpec(path);
std::wstring ws(path);
std::string str(ws.begin(), ws.end());
return str.substr(0, str.find_last_of('/'));
}
But now I get following error message:
cannot convert argument 2 from 'wchar_t [260]' to 'LPSTR'
I tried casting but it didn't work.
You are using explicit wchar_t instead of TCHAR, so you should use explicit Unicode version GetModuleFileNameW and PathRemoveFileSpecW instead of GetModuleFileName and PathRemoveFileSpec.
Related
BOOL CALLBACK callback(HWND hwnd, LPARAM param) {
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
if (pid == param) {
TCHAR classNameBuf[MAX_PATH];
GetClassName(hwnd, classNameBuf, MAX_PATH);
std::string className(&classNameBuf[0]);
if (className != ("MSCTFIME UI") && className != ("IME") && className != ("ConsoleWindowClass")) {
window_handle = hwnd;
return false;
}
}
return true;
}
When I try to compile my project and run it, it gives these errors.:
Error E0289 no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list
And this one
Error C2664 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::basic_string(std::initializer_list<_Elem>,const _Alloc &)': cannot convert argument 1 from 'TCHAR *' to 'std::initializer_list<_Elem>'
Can someone point out what I am doing wrong?
You are calling the TCHAR-based GetClassName() macro, and you have your project set to define UNICODE, thus TCHAR is mapped to wchar_t, not char. You can't create a std::string from a wchar[] array (well, not the way you are trying to, anyway).
So, either:
change your project settings to use the MBCS charset instead of UNICODE, so that TCHAR maps to char, and GetClassName maps to GetClassNameA().
don't use TCHAR at all, use GetClassNameA() directly, eg:
CHAR classNameBuf[MAX_PATH];
GetClassNameA(hwnd, classNameBuf, MAX_PATH);
std::string className(classNameBuf);
if (className != "MSCTFIME UI" && ...)
if you really want to use TCHAR (which you shouldn't - this is not the '90s), you can use std::basic_string<TCHAR> instead, just be sure to wrap your string literals with the TEXT() macro, eg:
TCHAR classNameBuf[MAX_PATH] = {};
GetClassName(hwnd, classNameBuf, MAX_PATH);
std::basic_string<TCHAR> className(classNameBuf);
if (className != TEXT("MSCTFIME UI") && ...)
you can just avoid std::(basic_)string altogether and use lstrcmp() instead to match the same TCHAR encoding that GetClassName() uses, eg:
TCHAR classNameBuf[MAX_PATH] = {};
GetClassName(hwnd, classNameBuf, MAX_PATH);
if (lstrcmp(classNameBuf, TEXT("MSCTFIME UI")) != 0 && ...)
I'm trying to compile simple lines of code but I'm getting C2664 Error code.
#include <TlHelp32.h>
PROCESSENTRY32 pe32 = { 0 };
if (wcscmp(pe32.something, something) == 0)
Error:
int wcscmp(const wchar_t *,const wchar_t *)': cannot convert argument 1 from 'CHAR [260]' to 'const wchar_t
The definition of wcscmp() is:
_Check_return_
_ACRTIMP int __cdecl wcscmp(
_In_z_ wchar_t const* _String1,
_In_z_ wchar_t const* _String2
);
I can't use PROCESSENTRY32W because then Process32First() breaks because it needs PROCESSENTRY32.
How could I change this to make it compilable?
Use PROCESSENTRY32W instead of PROCESSENTRY32.
Use Process32FirstW instead of Process32First.
This question already has answers here:
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
(4 answers)
Closed 5 years ago.
I keep getting compile errors with the line at the bottom
hFind = FindFirstFile(fileFilter.c_str()), &FindFileData);
The compiler keeps throwing error C2664 back at me, : cannot convert argument 1 from 'const char *' to 'LPCWSTR'
How do I create a LPCWSTR to a std::string to pass to into FindFirstFile?
Zhe section of code is for reference.
The actual code follows below.
using namespace std;
void GetFileListing(string directory, string fileFilter, bool recursively = true)
{
if (recursively)
GetFileListing(directory, fileFilter, false);
directory += "\\";
WIN32_FIND_DATA FindFileData;
HANDLE hFind ;
string filter = directory + (recursively ? "*" : fileFilter);
string Full_Name;
string Part_Name;
// the line causing the compile error
hFind = FindFirstFile(fileFilter.c_str()), &FindFileData);
The WinAPI data types are lovely short abbreviations. LPCWSTR is short for:
Long
Pointer to the start of
Const
Wide
STRing
As such it is a pointer (long pointers are history) to the first character of a const wide string (const wchar_t*), meaning you need to use std::wstring::c_str() instead of std::string::c_str().
Side note: just be sure to #define UNICODE everywhere you use the WinAPI, otherwise you'll get other errors about conversion to LPCSTR. Alternatively, explicitly use the W versions of the WinAPI functions where they exist.
im developin QT application, and i need to include pure C code. When i compile this code in code::blocks it was successful, maybe one warning, but when i try to compile it in QT creator, i get these 4 errors.
cannot convert 'char*' to 'WCHAR*' for argument '1' to 'UINT GetSystemDirectoryW(WCHAR*, UINT)'
cannot convert 'char*' to 'const WCHAR*' for argument '1' to 'HINSTANCE__* LoadLibraryW(const WCHAR*)'
cannot convert 'char*' to 'WCHAR*' for argument '1' to 'BOOL
cannot convert 'const char*' to 'const WCHAR*' for argument '2' to 'LONG RegQueryValueExW(HKEY__*, const WCHAR*, DWORD*, DWORD*, BYTE*, DWORD*)'
and the code is here>
char systemDirectory[MAX_PATH];
GetSystemDirectory(systemDirectory, MAX_PATH); //first error
char kbdLayoutFilePath[MAX_PATH];
kbdLibrary = LoadLibrary(kbdLayoutFilePath); //second error
char kbdName[KL_NAMELENGTH];
GetKeyboardLayoutName(kbdName); //third error
if(RegQueryValueEx(hKey, "Layout File", NULL, &varType, layoutFile, &bufferSize) != ERROR_SUCCESS) //fourth error
i also use snprintf function, so i cant just change the type from char to WCHAR, because then it wont compile the snprintf
snprintf(kbdKeyPath, 51 + KL_NAMELENGTH,
"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", kbdName);
So do you have any ideas how to fix it ? first i tried change type from char to WCHAR, but then the snprintf didnt work, so i tried to use swprinf, but with no success, since strangely it didnt find this function
int swprintf(wchar_t *wcs, size_t maxlen,
const wchar_t *format, ...);
but just this
int swprintf(wchar_t *wcs,
const wchar_t *format, ...);
so what are my option ? How to compile pure C code in c++ environment without any errors... or how to make the right type conversion.
You are compiling in Unicode mode. You could set your compile to multi-byte strings. The problem that is happening is those windows API functions are macros that check whether you are building Unicode or not and then call either the W or A version of the function (in your code there, the GetSystemDirectory is actually calling GetSystemDirectoryW. So, you can either change your compile to multi-byte strings....or you could explicitly change your api calls to call the A version (i.e. GetSystemDirectoryA)
You are compiling your project with the UNICODE or _UNICODE define. Check your project settings and remove the define if necessary. To remove the define, you might need to disable unicode support for the whole project.
Change over from char to WCHAR and then to solve your swprintf problem just do this
#define swprintf _snwprintf
On Windows, the prototype of swprintf is
int swprintf( wchar_t *buffer,const wchar_t *format [,argument] ... );
But the ISO C Standard requires the following prototype for swprintf
int swprintf (wchar_t *, size_t, const wchar_t *, ...);
For this very reason, on Windows, _snwprintf is provided.
Read this for more details
http://msdn.microsoft.com/en-us/library/ybk95axf(v=vs.71).aspx
when I tried to compiling my project I got some errors that I can't solve.. anyway this is the one of the codes:
public:
void Init(HMODULE hModule, string Filename)
{
char szLoc[ MAX_PATH ];
GetModuleFileName(hModule, szLoc, sizeof( szLoc ) );
char* dwLetterAddress = strrchr( szLoc, '\\' );
*( dwLetterAddress + 1 ) = 0;
strcat( szLoc, Filename.c_str() );
__OutStream.open( szLoc, ios::app);
}
And the error is:
error C2664: 'GetModuleFileNameW' : cannot convert parameter 2 from 'char [260]' to 'LPWCH'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Thanks for help.. Regards, Messer
A lot of the "functions" of the Windows API are actually macroes to either the ANSI (A) or Unicode (W for wide) version of the function. Depending on your project settings, these macroes will be either DoSomeFunctionA or DoSomeFunctionW when you want to call DoSomeFunction. The portable way would be then to use TCHAR because it is defined as char for ANSI and wchar_t for Unicode.
If you don't want to compile with Unicode, you can change your project settings to Project Properties -> Configuration Properties -> General -> Character Set -> Use Multibyte Character Set.
If you do want to compile with Unicode, then you should append an A (ex: GetModuleFileNameA) to the necessary function names.