How to use FindFirstFile [duplicate] - c++

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.

Related

argument of type is incompatible with parameter of type error [duplicate]

This question already has answers here:
Cannot convert parameter from 'const char[20]' to 'LPCWSTR'
(3 answers)
'CreateDirectoryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' in OpenCV 2.4.5 and VS 2010
(3 answers)
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
(4 answers)
'HMODULE LoadLibraryA(LPCSTR)': cannot convert argument 1 from 'const _Elem *' to 'LPCSTR'
(2 answers)
Cannot convert const char[16] to LPCWSTR [closed]
(1 answer)
Closed 1 year ago.
uintptr_t gameModule = (uintptr_t)GetModuleHandle("client.dll");
Severity Code Description Project File Line Suppression State
Error C2664 'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char [11]' to 'LPCWSTR'
uintptr_t gameModule = (uintptr_t)GetModuleHandle("client.dll");
HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from
'const char [11]' to 'LPCWSTR'
"client.dll" is a char string (const char [11]).
According to the Windows API TCHAR model, GetModuleHandle is a preprocessor macro which is expanded to GetModuleHandleW in Unicode builds (the default build mode for Visual Studio C++ projects since VS 2005).
GetModuleHandleW requires a LPCWSTR string parameter, i.e. a const wchar_t*, which is a wchar-t string.
So, you have a mismatch in your GetModuleHandle call, as you passed a char string, but GetModuleHandle (which is expanded to GetModuleHandleW) requires a wchar_t string (LPCWSTR).
You can fix this error passing L"client.dll" instead of "client.dll"; in fact, L"client.dll" (note the L prefix) is a wchar_t string:
// Pass L"client.dll" instead of "client.dll"
uintptr_t gameModule = (uintptr_t)GetModuleHandle(L"client.dll");
Another option would be explicitly invoking the "ANSI" function GetModuleHandleA:
// Explicitly call GetModuleHandleA
uintptr_t gameModule = (uintptr_t)GetModuleHandleA("client.dll");
but I would stick with Unicode APIs.
You could even totally embrace the TCHAR model, and decorate your string literal with _T() or TEXT(), e.g.:
uintptr_t gameModule = (uintptr_t)GetModuleHandle(_T("client.dll"));
That would work in both ANSI and UNICODE builds.

Cannot convert argument from wchar_t[260] to LPSTR

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.

Why does it not let me execute this?

I'm new to C++ (1 week) , coding in general and I'm wondering how I can make this work, because I get errors whenever I compile & run. The idea is that the user inputs a gift code, and the program automatically opens it in a tab.
https://i.stack.imgur.com/4IKkm.png
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
string codes;
cout << "Input here: \n";
cin >> codes;
for (int i = 100; i > 0; i--)
{
string site;
site = "http://discord.gift/" << codes;
ShellExecute(NULL,
"open",
site,
NULL,
NULL,
SW_SHOWNORMAL);
Sleep(300);
}
}
Error message:
[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'LPCSTR {aka const char*}' for argument '3' to 'HINSTANCE__* ShellExecuteA(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, INT)'
[Error] no match for 'operator<<' (operand types are 'const char [21]' and 'std::string {aka std::basic_string<char>}')
There are three problems with your code:
you are using std::string without #include <string>. While it is permissible for standard headers to include each other, you should not rely on this. Always #include relevant headers that your code needs.
you can't use operator<< to concatenate a string literal with a std::string object. You need to use operator+ instead:
site = "http://discord.gift/" + codes;
the ShellExecute() function does not accept a std::string as a parameter value, it takes const char* pointers instead. A string literal implicitly decays into such a pointer, but std::string does not. However, std::string has a c_str() method to obtain such a pointer:
ShellExecute(NULL, "open", site.c_str(), NULL, NULL, SW_SHOWNORMAL);
Adding your code and errors in your question instead of just a pic would be a great idea.
Anyway, To add strings together you must use the + sign site = "http//..." + codes
Not sure if there is any other errors but I highly recommend to indent your code to make it easier to read

error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR' [duplicate]

This question already has answers here:
Cannot convert parameter from 'const char[20]' to 'LPCWSTR'
(3 answers)
cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}'
(4 answers)
cannot convert from 'const char *' to 'LPCTSTR'
(1 answer)
cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
(6 answers)
Closed 4 years ago.
I'm new to using Visual Studio and need to compile an application but I'm getting the error: error C2440: '=': cannot convert from 'const char *' to 'LPCWSTR' on line:
std::string open_file_dialog(
std::string title,
std::string filter)
{
char filename[MAX_PATH];
OPENFILENAME ofn;
ZeroMemory(&filename, sizeof(filename));
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = filter.c_str();
ofn.lpstrFile = filename;
I believe it might have to do with my project settings as the source code I am looking at should compile as it is. However, I'm not 100% sure. Can anyone help me out here?
What you need to know about the windows API is that it comes in 2 flavors ,namely UNICODE flavor and non-UNICODE flavor.
When you include windows.h
the flavor selected depends on whether UNICODE is defined.
Many if not most WINAPI structures and function are just macros that basically just add either a W or an A to the macro-name to get the real name of the thing you want.
The UNICODE flavor requires character types of wchar_t and non-UNICODE takes char types (or pointers to them).
So you either must use the non-macro names for structs and/or functions or adjust your usage of types to the required macro-definition (in your case use std::wstring in stead of std::string).
You probably have UNICODE activated so OPENFILENAME becomes OPENFILENAMEW, not OPENFILENAMEA which is why your ofn.lpstrFilter = filter.c_str(); fails.
lpstrFilter is a wchar_t* in the W version.
You should probably stick with UNICODE and change to use std::wstrings which is gets you the best access to the WinAPI. Some functions work differently (worse) in A (Ansi) mode.

When I run the C++ code, I always get Visual Studio error C2664

When I use this code
if (GetKeyNameText(Key << 16, NameBuffer, 127))
{
KeyName = NameBuffer;
GoodKeyName = true;
}
I get the following error
C2664 'int GetKeyNameTextW(LONG,LPWSTR,int)': cannot convert argument
2 from 'char [128]' to 'LPWSTR'
The NameBuffer says this:
Error: argument of type "char*" is incompatible with parameter of type
"LPWSTR"
Any tips?
You have UNICODE defined, which means all your functions and TCHAR and LPTSTR are defaulting to wide characters (wchar_t).
That means you can't use a narrow-character string (using char) without special care.
There is an easy solution, and that's to explicitly call the narrow-character version of the function: GetKeyNameTextA.
Another solution is to stop using char and change to TCHAR and related types, and use the T macro for string literals.
You might want to read more about UNICODE in the Windows API.