To terminate a blocking Input from another thread, I tried to simulate a input event or more precisely a kayboard input using WriteConsoleInput function.
#include<Windows.h>
#include<conio.h>
#include<thread>
void KillBlockingIO() {
DWORD entityWritten;
INPUT_RECORD inputs;
inputs.EventType = KEY_EVENT;
inputs.Event.KeyEvent.bKeyDown = true;
inputs.Event.KeyEvent.uChar.AsciiChar = VK_RETURN;
inputs.Event.KeyEvent.wRepeatCount = 0;
inputs.Event.KeyEvent.dwControlKeyState = 0;
inputs.Event.KeyEvent.wVirtualKeyCode = 0;
inputs.Event.KeyEvent.wVirtualScanCode = 0;
// inputs.Event = { true, 0, 0, 0, VK_RETURN, 0 }; // same as above
Sleep(2000);
WriteConsoleInputA(GetStdHandle(STD_INPUT_HANDLE), &inputs, 1, &entityWritten);
}
int main()
{
std::thread t(KillBlockingIO);
char c = _getch();
printf("character recieved without typing: %c\n", c);
t.join();
}
It is working but I'm not sure, I've used WriteConsoleInput function property because members like wVirtualKeyCode, wVirtualScanCode and dwControlKeyState is set zero. No matter what value I pass, It is always going to have same result. It is also not very well Documented. I tried finding code examples but there is no such example.
what is purpose of these parameters and How to use WriteConsoleInput function properly?
Related
Im trying to create a GUI using the library Irrlicht in C++, however whilst wring my Gui Handler, I get SIGSEGV errors when trying to access any data outside the constructor.
My header looks like this:
#pragma once
#include <irrlicht.h>
#include "Structs.h"
#include <vector>
#include <string>
class GuiHandler
{
public:
GuiHandler(Structs::SAppContext *contIN);
~GuiHandler();
bool GuiEvent(const irr::SEvent& eventType);
struct RGBPicker
{
IGUIButton* sample;
IGUIStaticText* RGB[3];
IGUIEditBox* RGBValues[3];
int Colour[3];
};
RGBPicker bottomColour;
RGBPicker topColour;
int i;
}
The values are assigned to the 2 RGBPicker variables in the constructor in the cpp file:
GuiHandler::GuiHandler(Structs::SAppContext *contIN)
{
context = contIN;
int xpos = 185;
int ypos= 110;
//bottomColour.pickerTexture->fill(s)
bottomColour.sample = context->env->addButton(rect<s32>(128+xpos, 16+ypos, 198+xpos, 86+ypos), 0, 111, L"");
bottomColour.sample->setEnabled(false); //static box
bottomColour.RGBValues[0] = context->env->addEditBox(L"", rect<s32>(64+xpos, 64+ypos, 120+xpos, 84+ypos), true, 0, 113);
bottomColour.RGBValues[1] = context->env->addEditBox(L"", rect<s32>(64+xpos, 40+ypos, 120+xpos, 60+ypos), true, 0, 113);
bottomColour.RGBValues[2] = context->env->addEditBox(L"", rect<s32>(64+xpos, 16+ypos, 120+xpos, 36+ypos), true, 0, 113);
for(int i = 0; i < 3; i++)
{
bottomColour.RGBValues[i]->setMax(3);
bottomColour.RGBValues[i]->setText(L"0");
}
bottomColour.RGBValues[0]->setToolTipText(L"B1");
bottomColour.RGBValues[1]->setToolTipText(L"G1");
bottomColour.RGBValues[2]->setToolTipText(L"R1");
bottomColour.RGB[0] = context->env->addStaticText(L"B:", rect<s32>(48+xpos, 64+ypos, 66+xpos, 84+ypos), false, false, 0, -1,114);
bottomColour.RGB[1] = context->env->addStaticText(L"G:", rect<s32>(48+xpos, 40+ypos, 66+xpos, 60+ypos), false, false, 0, -1,114);
bottomColour.RGB[2] = context->env->addStaticText(L"R:", rect<s32>(48+xpos, 16+ypos, 66+xpos, 36+ypos), false, false, 0, -1,114);
std::cout<<i<<std::endl;
i=2;
std::cout<<i<<std::endl;
}
Whilst everything works within the header- accesing all the topColour, bottomColour and context statements, when in any other method within the class, they result in a SIGSEGV. Even printing out i with cout.
I know i must be doing very simple incorrectly however I can't work it out.
Thanks.
This version (based on this article) works:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
VOID
CALLBACK
MyWorkCallback(
PTP_CALLBACK_INSTANCE Instance,
PVOID Parameter,
PTP_WORK Work
)
{
// Instance, Parameter, and Work not used in this example.
UNREFERENCED_PARAMETER(Instance);
UNREFERENCED_PARAMETER(Parameter);
UNREFERENCED_PARAMETER(Work);
DWORD threadId = GetCurrentThreadId();
BOOL bRet = FALSE;
//
// Do something when the work callback is invoked.
//
{
_tprintf(_T("MyWorkCallback: ThreadId = %d Task performed.\n"), threadId);
}
return;
}
int main()
{
TP_CALLBACK_ENVIRON CallBackEnviron;
PTP_POOL pool = NULL;
PTP_CLEANUP_GROUP cleanupgroup = NULL;
PTP_WORK_CALLBACK workcallback = MyWorkCallback;
PTP_TIMER timer = NULL;
PTP_WORK work = NULL;
InitializeThreadpoolEnvironment(&CallBackEnviron);
pool = CreateThreadpool(NULL);
SetThreadpoolThreadMaximum(pool, 1);
SetThreadpoolThreadMinimum(pool, 3);
cleanupgroup = CreateThreadpoolCleanupGroup();
SetThreadpoolCallbackPool(&CallBackEnviron, pool);
SetThreadpoolCallbackCleanupGroup(&CallBackEnviron, cleanupgroup, NULL);
work = CreateThreadpoolWork(workcallback, NULL, &CallBackEnviron);
for (int i = 0; i < 10; ++i)
{
SubmitThreadpoolWork(work);
}
}
However, this version also works (with the same work function from above):
int main()
{
PTP_WORK = CreateThreadpoolWork(workcallback, NULLPTR, NULLPTR);
for (int i = 0; i < 10; ++i)
{
SubmitThreadpoolWork(work);
}
}
What are the differences between the two versions (except for the minimum and maximum thread count)?
Why would I use one version over another?
This is covered in the documentation for InitializeThreadpoolEnvironment:
Create a callback environment if you plan to call one of the following functions to modify the environment:
SetThreadpoolCallbackCleanupGroup
SetThreadpoolCallbackLibrary
SetThreadpoolCallbackPool
SetThreadpoolCallbackPriority
SetThreadpoolCallbackRunsLong
If you need the functionality provided by one or more of the listed functions, then you need to create a callback environment. If you don't, you don't.
If in doubt when starting a new project, use the simple approach to begin with, and see whether it meets your needs. You can always go back and switch to the more complex variant if and when it becomes necessary.
EDIT: by not working I mean that in my main array mA in main doesn't show any change to the elements within the array.
I have been checking my functions as I develop the headers and they have been working perfectly: Until I got to the final header MonitorArray.h.
mA.getScreen(i).checkScreen();
Didn't work and I couldn't work out why. So I created a new function within MonitorArray to do a similar job using the same function, and to my surprise it worked.
mA.pollScreens();
Which uses (Inside MonitorArray.h):
monitorArray[i].checkScreen();
Function getScreen:
ScreenArray MonitorArray::getScreen(int arrayPointer)
{
if (arrayPointer<0 || arrayPointer>=monitors)
{
return false;
}
else
{
return monitorArray[arrayPointer];
}
}
Function checkScreen and addArray:
void ScreenArray::checkScreen()
{
HDC dMonitor;
PixelArray pArray;
int lenX = 0, lenY = 0;
dMonitor = CreateDC(iMonitor.szDevice, iMonitor.szDevice, NULL, NULL);
lenX = (iMonitor.rcWork.right - iMonitor.rcWork.left) - 1;
lenY = (iMonitor.rcWork.bottom - iMonitor.rcWork.top) - 1;
pArray.setColour(0, GetPixel(dMonitor, 0, 0));
pArray...
...
...
addArray(&pArray);
ReleaseDC(NULL, dMonitor);
}
void ScreenArray::addArray(PixelArray* pA)
{
if (previousCheck(*pA))
{
arrayPosition = 0;
screenArray[arrayPosition] = *pA;
arrayPosition++;
}
else
{
screenArray[arrayPosition] = *pA;
arrayPosition++;
}
if (arrayPosition==11)
{
//Run screen saver on monitor
}
}
Why does running the command within the header file through a new function work but running the functions from main not?
Assuming that "didn't work" means "didn't affect the ScreenArray in my MonitorArray", it's because getScreen returns a copy of the array element
ScreenArray MonitorArray::getScreen(int arrayPointer)
while the new member function most likely works with the array directly.
You'll need to return a pointer to the array element instead:
ScreenArray* MonitorArray::getScreen(int arrayPointer)
{
if (arrayPointer<0 || arrayPointer>=monitors)
{
return NULL;
}
else
{
return &monitorArray[arrayPointer];
}
}
(BTW: the implicit conversion from bool to ScreenArray looks very odd.)
I have tried all the normal methods of faking keyboard actions (SendInput/SendKeys/etc) but none of them seemed to work for games that used DirectInput. After a lot of reading and searching I stumbled across Interception, which is a C++ Library that allows you to hook into your devices.
It has been a very long time since I worked with C++ (Nothing existed for C#) so I am having some trouble with this. I have pasted in the sample code below.
Does it look like there would be anyway to initiate key actions from the code using this? The samples all just hook into the devices and rewrite actions (x key prints y, inverts mouse axis, etc).
enum ScanCode
{
SCANCODE_X = 0x2D,
SCANCODE_Y = 0x15,
SCANCODE_ESC = 0x01
};
int main()
{
InterceptionContext context;
InterceptionDevice device;
InterceptionKeyStroke stroke;
raise_process_priority();
context = interception_create_context();
interception_set_filter(context, interception_is_keyboard, INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP);
/*
for (int i = 0; i < 10; i++)
{
Sleep(1000);
stroke.code = SCANCODE_Y;
interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
}
*/
while(interception_receive(context, device = interception_wait(context), (InterceptionStroke *)&stroke, 1) > 0)
{
if(stroke.code == SCANCODE_X) stroke.code = SCANCODE_Y;
interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
if(stroke.code == SCANCODE_ESC) break;
}
The code I commented out was something I tried that didn't work.
You need to tweak key states for UP and DOWN states to get key presses. Pay attention at the while loop that the variable device is returned by interception_wait, your commented out code would send events to what?? device is not initialized! Forget your code and try some more basic. Look at the line inside the loop with the interception_send call, make more two calls after it, but don't forget to change stroke.state before each call using INTERCEPTION_KEY_DOWN and INTERCEPTION_KEY_UP so that you fake down and up events. You'll get extra keys at each keyboard event.
Also, you may try use INTERCEPTION_FILTER_KEY_ALL instead of INTERCEPTION_FILTER_KEY_DOWN | INTERCEPTION_FILTER_KEY_UP. The arrow keys may be special ones as mentioned at the website.
void ThreadMethod()
{
while (true)
{
if (turn)
{
for (int i = 0; i < 10; i++)
{
Sleep(1000);
InterceptionKeyStroke stroke;
stroke.code = SCANCODE_Y;
stroke.state = 0;
interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
Sleep(1);
stroke.state = 1;
interception_send(context, device, (const InterceptionStroke *)&stroke, 1);
turn = false;
}
}
else Sleep(1);
}
}
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ThreadMethod, NULL, NULL, NULL);
while (interception_receive(context, device = interception_wait(context), (InterceptionStroke*)&stroke, 1) > 0)
{
if (stroke.code == SCANCODE_F5) turn = true;
interception_send(context, device, (InterceptionStroke*)&stroke, 1);
if (stroke.code == SCANCODE_ESC) break;
}
I want to implement port-forwarding using intel-upnp.
I got XML data like:
Device found at location: http://192.168.10.1:49152/gatedesc.xml
service urn:schemas-upnp-org:service:WANIPConnection:1
controlurl /upnp/control/WANIPConn1
eventsuburl : /upnp/control/WANIPConn1
scpdurl : /gateconnSCPD.xml
And now, I want to make upnp-action. But, I don't know how to make it.
If you know some code snippet or helpful URL in C, please tell me.
char actionxml[250];
IXML_Document *action = NULL;
strcpy(actionxml, "<u:GetConnectionTypeInfo xmlns:u=\"urn:schemas-upnp- org:service:WANCommonInterfaceConfig:1\">");
action = ixmlParseBuffer(actionxml);
int ret = UpnpSendActionAsync( g_handle,
"http:192.168.10.1:49152/upnp/control/WANCommonIFC1",
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1",
NULL,
action,
upnp_callback,
NULL);
I know this is an old question, but it can be kept for reference. You can take a look at the sample code in the libupnp library here: https://github.com/mrjimenez/pupnp/blob/master/upnp/sample/common/tv_ctrlpt.c
The relevant code is in the function TvCtrlPointSendAction():
int TvCtrlPointSendAction(
int service,
int devnum,
const char *actionname,
const char **param_name,
char **param_val,
int param_count)
{
struct TvDeviceNode *devnode;
IXML_Document *actionNode = NULL;
int rc = TV_SUCCESS;
int param;
ithread_mutex_lock(&DeviceListMutex);
rc = TvCtrlPointGetDevice(devnum, &devnode);
if (TV_SUCCESS == rc) {
if (0 == param_count) {
actionNode =
UpnpMakeAction(actionname, TvServiceType[service],
0, NULL);
} else {
for (param = 0; param < param_count; param++) {
if (UpnpAddToAction
(&actionNode, actionname,
TvServiceType[service], param_name[param],
param_val[param]) != UPNP_E_SUCCESS) {
SampleUtil_Print
("ERROR: TvCtrlPointSendAction: Trying to add action param\n");
/*return -1; // TBD - BAD! leaves mutex locked */
}
}
}
rc = UpnpSendActionAsync(ctrlpt_handle,
devnode->device.
TvService[service].ControlURL,
TvServiceType[service], NULL,
actionNode,
TvCtrlPointCallbackEventHandler, NULL);
if (rc != UPNP_E_SUCCESS) {
SampleUtil_Print("Error in UpnpSendActionAsync -- %d\n",
rc);
rc = TV_ERROR;
}
}
ithread_mutex_unlock(&DeviceListMutex);
if (actionNode)
ixmlDocument_free(actionNode);
return rc;
}
The explanation is that you should create the action with UpnpMakeAction() if you have no parameters or UpnpAddToAction() if you have parameters to create your action, and then send it either synchronously or asynchronously.