我可能遺漏了一些明顯的東西,如果我遺漏了,請原諒我。
無論如何,我有一個類Keys,其方法SPrintScreen如下:
class Keys{
uint32_t sentQM;
// create array to be passed to SendInput function
INPUT printscreen[2];
// set both types to keyboard events
printscreen[0].type = INPUT_KEYBOARD;
printscreen[1].type = INPUT_KEYBOARD;
// assign printscreen key
printscreen[0].ki.wVk = VK_SNAPSHOT;
printscreen[1].ki.wVk = VK_SNAPSHOT;
// add key up flag to second input
printscreen[1].ki.dwFlags = KEYEVENTF_KEYUP;
void SPrintScreen(){
sentQM = SendInput(2, printscreen, sizeof(INPUT));
if (sentQM != ARRAYSIZE(printscreen)){
std::cout << "could not simulate print screen" << std::endl;
}
}
}
當我Keys在堆疊(Keys keys;)上創建物件然后呼叫SPrintScreen(keys.SPrintScreen())時,事情按預期作業,并且模擬了列印螢屏鍵的按下并且程式可以繼續運行。
但是,當我Keys在堆 ( Keys* keys;) 上創建物件并呼叫SPrintScreen( keys->SPrintScreen() ) 時,程式只是靜默退出,沒有任何說明原因,甚至在控制臺中也沒有訊息。
這只是有時如何作業?
uj5u.com熱心網友回復:
Keys* keys;
不創建 Keys 物件。它創建一個指向鍵物件的指標。為了做某事,你必須再做兩件事
- 創建一個 Keys 物件
- 指向
keys它
像這樣
Keys *keyObj = new Keys();
keys = keyObj;
顯然你實際上會這樣做
Keys *keys = new Keys();
這會在堆上創建一個 Keys 物件。或者,您可以在堆疊上放置一個
Keys mykeys;
Keys *keys = &mykeys;
哪個對你有用取決于你想要做什么。也盡量不要有堆物件的裸指標,使用 shared_ptr 或 unique_ptr
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/426008.html
