Reputation: 7
Hi I am writing a code for a plants vs zombies trainer in C++ only when I tried to launch it, it failed with this error:
error: cannot convert 'LPCWSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* FindWindowA(LPCSTR, LPCSTR)'|
This is my code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
system("color 0A");//change the colour of the text
cout << "PlantsVsZombies Trainer voor game of the year edition(gemaakt door Pjotr Slooff)" << endl;//display some (dutch) text
cout << "Open PlantsVsZombies en druk dan op enter" << endl;
system("Pause");//wait for the user to press enter
LPCWSTR Game = L"Plants vs. Zombies";
HWND hwnd = FindWindowA(0, Game);
if (hwnd == 0)
{
cout << "PlantsVsZombies is niet gevonden open het en probeer het dan opnieuw" << endl;
system("Pause");
}
else
{
DWORD process_ID;
GetWindowThreadProcessId(hwnd, &process_ID);
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, process_ID);
cout << "PlantsVsZombies is gevonden! Happy Hacking :D" << endl;
system("Pause");
cout << "Typ 1 voor Zon Cheat" << endl;
cout << "Typ: ";
int Option;
cin >> Option;
if (Option > 1)
{
cout << "Er is maar 1 optie" << endl;
system("Pause");
}
if (Option == 1)
{
cout << "Hoeveel Zon wil je?" << endl;
cout << "Typ: ";
int Zon;
cin >> Zon;
DWORD newdatasize = sizeof(Zon);
if (WriteProcessMemory(hProcess, (LPVOID)00005560, &Zon, newdatasize, NULL))
{
cout << "De Zon is toegevoegd" << endl;
system("Pause");
}
else
{
cout << "Er was een error tijdens het toevoegen van de Zon" << endl;
system("Pause");
}
}
}
main();
return 0;
}
I find this very annyoing and I can't fix it so I will be really grateful for who answers my question
Upvotes: 0
Views: 2130
Reputation: 3929
Your problem is that windows API's usually come in two flavours :
1) Ansi/Ascii (legacy) which uses char
as basetype for character
- API functions ending with an A
- use LPSTR
and LPCSTR
2) Unicode (native) which uses wchar_t
as basetype for character
- API functions ending with a W
- use LPWSTR
and LPCWSTR
You must decide which one to use with the appropiate argument types.
You can avoid many of these issues by using the 'neutral' versions of the API's:
These lack the A or W at the end of the names and are macro's that will be defined to either one of the actual API's
depending on the definition of the macro UNICODE
.
The neutral version use the macro's TCHAR
,LPTSTR
and LPCTSTR
.
You should include tchar.h which gives you an API for manipulating 0-terminated TSTR data.
There's plenty info on this topic to be found by googling it.
Upvotes: 2
Reputation: 50016
Instead of FindWindowA(0, Game);
use FindWindowW(0, Game);
, or simply FindWindow
FindWindowA
accepts as in error LPCSTR arguments, and you are passing LPCWSTR
.
Upvotes: 5