C++ no suitable conversion function from INPUT to LPINPUT exists - c++

i'm trying to get the coordinates of the mouse, then move the mouse relative to those coordinates.
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
int main() {
SHORT f5;
POINT pt{};
MOUSEINPUT _mi{};
INPUT mm{};
mm.type = 0;
for (;;) {
f5 = GetAsyncKeyState(0x74);
if (f5 > 0) {
GetCursorPos(&pt);
_mi.dx = pt.x + 100;
_mi.dy = pt.y + 100;
mm.mi = _mi;
SendInput(1, mm, sizeof(mm));
}
Sleep(50);
}
return 0;
}
I'm getting an error saying that no suitable conversion from INPUT to LPINPUT exists, but I also have no idea how to create a "LPINPUT" struct.

SendInput() expects a pointer to an array of INPUT structures. Since you are passing only 1 INPUT, give it the address of your INPUT as a pointer by prefixing the structure with the & operator, just as you did with GetCursorPos():
SendInput(1, &mm, sizeof(mm));

Related

exception thrown with random numbers in c++

I'm trying to make a game which requires pseudo random numbers to set the enemies to random spots. So I tried including the stdlib header file and used srand before getting random numbers for the enemies using rand, my code until the first srand looks like this:
#include "pch.h"
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <time.h>
int screenWidth = 70, screenHeight = 80;
class Enemy {
public:
int column, lives;
Enemy() {};
Enemy(int nColumn, int nLives) {
column = nColumn;
lives = nLives;
}
};
int main()
{
wchar_t *screen = new wchar_t[screenWidth * screenHeight];
HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(hConsole);
DWORD bytesWritten = 0;
int x = 0, y = 0, width = 10, height = 10;
std::wstring canvas = L"";
wchar_t *title = new wchar_t[8];
wsprintf(title, L"retro shooter");
SetConsoleTitle(title);
int enemiesLength = screenWidth / width;
Enemy* enemies = new Enemy[enemiesLength];
srand(time(NULL)); // It throws the error at this line
for (int i = 0; i < enemiesLength; i++) {
if (rand() % 2) enemies[i] = Enemy(i, 1);
}
// the code doesn't end here that's why I didn't put out the closing curly bracket
The code above gives me the error/exception:
Exception thrown at 0x76EDE496 (ntdll.dll) in retroShooter.exe: 0xC0000005: Access violation reading location 0x183A0FA2.
I have tried using vectors but the exception is the same.
I have also tried include-ing cstdlib like: #include <cstdlib> but the exception is the exact same.
This exception is thrown after it has been compiled and it isn't marked as an error in visual studio
This is robably the issue:
wchar_t *title = new wchar_t[8]; // 8 is a lot shorter
wsprintf(title, L"retro shooter"); // than 14

C++ How to remove vertical scrollbar?

I am trying to make a fun program where it display random numbers, but I need to remove the scrollbar so it looks more convincing. I managed to make the program full screen but I can't remove the vertical scrollbar. Screenshot
Code:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
There are many ways, one of them is manipulating the size of the internal buffer of the console to have the same size of the window and then using ShowScrollBar function to remove the scrolls.
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
using namespace std;
int main() {
SetConsoleDisplayMode(GetStdHandle(STD_OUTPUT_HANDLE), CONSOLE_FULLSCREEN_MODE, 0);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hstdout, &csbi);
csbi.dwSize.X = csbi.dwMaximumWindowSize.X;
csbi.dwSize.Y = csbi.dwMaximumWindowSize.Y;
SetConsoleScreenBufferSize(hstdout, csbi.dwSize);
HWND x = GetConsoleWindow();
ShowScrollBar(x, SB_BOTH, FALSE);
int output;
bool done = false;
system("color a");
while (!done) {
output = 1 + (rand() % (int)(1000 - 1 + 1));
cout << output;
}
}
Another way is to rely on conio.h or another C/C++ header/library which implements user interface functions.

SendInput strings?

So I've been trying to make a program that sends a string of keystrokes over to the currently open window and whenever I run the code, it doesn't send whatever I want it to send it sends something completely different(i.e sending bob comes up as 22 or 2/2)
#include <iostream>
#include <vector>
#include <Windows.h>
int SendKeys(const std::string &msg);
int main() {
Sleep(5);
while(true) {
Sleep(500);
SendKeys("iajsdasdkjahdjkasd");
}
std::cin.get();
return 0;
}
int SendKeys(const std::string & msg)
{
std::vector<INPUT> bob(msg.size());
for(unsigned int i = 0; i < msg.size(); ++i)
{
bob[i].type = INPUT_KEYBOARD;
bob[i].ki.wVk = msg[i];
std::cout << bob[i].ki.wVk << std::endl;
auto key = SendInput(1, &bob[i], sizeof(INPUT) /* *bob.size() */);
}
return 0;
}
(forgive the horrible formatting)
The virtual key codes does not generally correspond to the ASCII alphabet.
If you read e.g. this MSDN reference for virtual key-codes you will see that e.g. lower-case 'a' (which has ASCII value 0x61) corresponds to VK_NUMPAD1 which is the 1 key on the numeric keyboard.
The upper-case ASCII letters do correspond to the correct virtual key codes, so you need to make all letters upper-case when assigning to bob[i].ki.wVk. For all other symbols and characters you need to translate the character to the virtual key code.

C++ 2Dimensional array

I'm currently learning c++,and the code went so far,until
i made array variables to call a function instead of string,but something went wrong,and i can't figure out what it is.Problem is,it couts correctly only first 2 letters,then it couts the rest as └└└└ symbols.
Here's the code:
#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
using namespace std;
int land(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
char H = 72;
cout<<H;
}
int player(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
char X = 88;
cout<<X;
}
/*previously,i declared a string inside worldgen,and generated 2d array inside the for loop,but when i changed variables to call a function,first letters were X and H,but then it went └└└└└└└└└└└└└└└└└└└└└└└ for all the remaining characters.What's wrong here? */
int worldgen(int dimX,int dimY,int x,int y){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
system("TITLE MyTitleText");
int H = land();
int X = player();
string world[dimX][dimY];
for(int c = 0;c<dimY;c++){
for(int count = 0;count<dimX;count++){
world[count][c] = H;
world[x][y] = X;
cout<<world[count][c];
}
cout<<endl;
}
}
int main(){
HANDLE hConsole;
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
worldgen(70,15,10,10);
cin.get();
}
Neither land() nor player() return anything so neither H nor X are getting any meaningful data are are just garbage data that was uninitialized.
I'm surprised this is even compiling since you have two functions that are supposed to return ints but aren't set to return anything.

Program that returns a list of drive letters

In the example of my computer the desired output should be: "C: E: F: H: N:" . I know it's possible, but what is the simpliest way to do that? Pottering in QueryDosDevice output
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <fstream>
#include <iostream>
const int REPORT_LENGTH = 5000;
int main(void)
{
TCHAR targetPath[REPORT_LENGTH];
std::ofstream oFile;
oFile.open("dos device query.txt");
QueryDosDevice(NULL,targetPath,REPORT_LENGTH);
for(int i=0; i<REPORT_LENGTH;i++)
if (targetPath[i]=='\0')(targetPath[i]='\n');
for(int i=0; i<REPORT_LENGTH; i++)
oFile<<static_cast<char>(targetPath[i]);
oFile.close();
return 0;
}
would be a huge waste of time and resources. Also function GetLogicalDriveStrings has betrayed me a lot.
#include <Windows.h>
int main()
{
TCHAR buffer[50];
GetLogicalDriveStrings(50,buffer);
MessageBox(0,buffer,"Drives in the system",MB_OK);
return 0;
}
It shows only the "C:\" volumine.
Example with GetLogicalDrives, albeit not with concatenating to a string (which is left as an exercise to the OP and the readers ;)):
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
int __cdecl _tmain(int argc, _TCHAR *argv[])
{
// Get the bit mask of drive letters
DWORD drives = ::GetLogicalDrives();
// Go through all possible letters from a to z
for(int i = 0; i < 26; i++)
{
// Check if the respective bit is set
if(drives & (1 << i))
{
// ... and if so, print it
_tprintf(TEXT("Drive %c: exists\n"), _T('A') + i);
}
}
return 0;
}
GetLogicalDriveStrings() is the way to go, you just have to use to correctly. You are assuming it returns a single string containing all of the drive strings, but that is not true. It returns an array of strings, one for each drive, so you have to loop through the array instead:
#include <windows.h>
int main()
{
TCHAR buffer[(4*26)+1] = {0};
GetLogicalDriveStrings(sizeof(buffer) / sizeof(TCHAR), buffer);
for (LPTSTR lpDrive = buffer; *lpDrive != 0; lpDrive += 4)
MessageBox(NULL, lpDrive, "Drive in the system", MB_OK);
return 0;
}