如何将成员函数传递给SetConsoleCtrlHandler()?

时间:2016-10-25 11:49:00

标签: c++ windows

我有一个命令行应用程序的代码(示例)。 我想确保在 ctrl + c 事件中调用Disconnect。 这就是我添加Handler的原因。 我可以让CtrlHandler成为成员函数,这样当按下 ctrl + c 时,处理程序有一个有效的Foo对象来调用Disconnect() on ?

#include <windows.h> 

class Foo
{

 public :
     Foo()
     {
      Connect();
     }
     virtual ~Foo()
     {
       Disconnect();
     }

  protected:
     Run();
     Connect();
     Disconnect();
};

BOOL CtrlHandler(DWORD fdwCtrlType)
{
    switch (fdwCtrlType)
    {
        // Handle the CTRL-C signal. 
    case CTRL_C_EVENT:
        //Disconnect somehow
        return(TRUE);
    default:
        return FALSE;
    }
}




int main(int argc, char* argv[])
 {
  SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE );
  Foo myFoo;
  myFoo.Run();        
 }

1 个答案:

答案 0 :(得分:1)

您唯一的选择是自己管理回调的上下文。单线程应用程序中一种非常简单的方法,无法重新输入import java.util.Scanner; public class ATM { public static void main(String[] args) throws Exception { String ATM; ATM myATM = new ATM(); myATM.go(); ifStatement(); //Main method, declares variables and calls the go() and ifStatement() methds. } public void go() throws Exception { int balance; Scanner userInput = new Scanner(System.in); System.out.println("Welcome to online ATM banking\nHow much do you want in your bank account?\nEnter your number"); balance = userInput.nextInt(); //Starts the program and sets a value to the variable "balance". } public static void ifStatement() throws Exception { //Creats if statements that change the outcome of the program depending on what the user as inputte dinto thto the program. //This has been done using int variables whihc have been converted into strings so that they can communicate wiht the userOption variable, the userOption variable's value has been set to whatever the user inputs int the program. String Withdraw; String Deposit; String Inquire; String Quit; String usersOption; int a = 1; int b = 2; int c = 3; int d = 4; String s = Integer.toString(a); String ss = Integer.toString(b); String sss = Integer.toString(c); String ssss = Integer.toString(d); //Declares variables Scanner userInput = new Scanner(System.in); // Allows user input. System.out.println("What do you want to do?\n1.Withdraw\n2.Deposit\n3.Inquire\n4.Quit\nEnter your number"); usersOption = userInput.nextLine();//Sets user input to a variable called userOption. if (usersOption.equals(s)){ System.out.println("*****************************************\n Withdrawl\n*****************************************\nHow much would you like to draw?\nEnter your number"); String withdrawl; withdrawl = userInput.nextLine(); balance = balance - withdrawl; } else { System.out.println(); } if(usersOption.equals(ss)) { System.out.println("*****************************************\n Deposit\n*****************************************\nHow much do you want to deposit?"); userInput.nextLine(); }else {System.out.println(); } if(usersOption.equals(sss)) { System.out.println("*****************************************\n Your balance is 100\n*****************************************"); } else { System.out.println(); } if(usersOption.equals(ssss)) { System.out.println("*****************************************\n Goodbye!\n*****************************************"); System.exit(0); }else {System.out.println();} } }

Run

但请允许我重申一下:对于不那么简单的应用程序,你需要更强大的功能。