hello
is it translatable in NIM
SetConsoleCtrlHandler Kernel32.lib the SetConsoleCtrlHandler(CtrlHandler, TRUE) until then it's ok
but this part there BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
can i do proc CtrlHandler(DWORD fdwCtrlType): BOOL=
in fact will nim know how to make the connection
BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL-C signal.
case CTRL_C_EVENT:
printf("Ctrl-C event\n\n");
Beep(750, 300);
return TRUE;
// CTRL-CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
Beep(600, 200);
printf("Ctrl-Close event\n\n");
return TRUE;
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
Beep(900, 200);
printf("Ctrl-Break event\n\n");
return FALSE;
case CTRL_LOGOFF_EVENT:
Beep(1000, 200);
printf("Ctrl-Logoff event\n\n");
return FALSE;
case CTRL_SHUTDOWN_EVENT:
Beep(750, 500);
printf("Ctrl-Shutdown event\n\n");
return FALSE;
default:
return FALSE;
}
}
int main(void)
{
if (SetConsoleCtrlHandler(CtrlHandler, TRUE))
{
printf("\nThe Control Handler is installed.\n");
printf("\n -- Now try pressing Ctrl+C or Ctrl+Break, or");
printf("\n try logging off or closing the console...\n");
printf("\n(...waiting in a loop for events...)\n\n");
while (1) {}
}
else
{
printf("\nERROR: Could not set control handler");
return 1;
}
return 0;
}
You can do everything with Nim that you can do with C. So yes.
There are two main ways to link with a .dll, by including the .h file and importing the types and definitions and possibly linking with a .lib file. See: https://livebook.manning.com/book/nim-in-action/chapter-8/60
There is another way were you just dynamically link with .dll without .h or .lib file which I prefer more. But you have to match "binary" api and types exactly as the .h file will not help you. See: https://github.com/treeform/windy/blob/master/src/windy/platforms/win32/windefs.nim
But it looks like Nim already supports a little bit of what you want with setControlCHook see: https://nim-lang.org/docs/system.html#setControlCHook%2Cproc maybe looking at source will unlock more?
Hello, I used this solution, because it is more complete under PowerShell.
proc SetConsoleCtrlHandler(x:proc ; ok : WINBOOL) : WINBOOL {.stdcall, dynlib: "kernel32", importc: "SetConsoleCtrlHandler".}
proc CtrlHandler(fdwCtrlType :DWORD) :WINBOOL =
const CTRL_C_EVENT = 0'i32
const CTRL_CLOSE_EVENT = 1'i32
const CTRL_BREAK_EVENT = 2'i32
const CTRL_LOGOFF_EVENT = 5'i32
const CTRL_SHUTDOWN_EVENT = 6'i32
case fdwCtrlType :
# Handle the CTRL-C signal.
of CTRL_C_EVENT:
echo "Ctrl-C event\n\n"
when not defined(release):
return 0
else:
return 1
# CTRL-CLOSE: confirm that the user wants to exit.
of CTRL_CLOSE_EVENT:
echo "Ctrl-Close event\n"
return 1
# Pass other signals to the next handler.
of CTRL_BREAK_EVENT:
echo "Ctrl-Break event\n"
return 1
of CTRL_LOGOFF_EVENT :
echo "Ctrl-logoff event\n"
return 0
of CTRL_SHUTDOWN_EVENT :
echo "Ctrl-Shtdown event\n"
return 0
else :
return 0
discard SetConsoleCtrlHandler(CtrlHandler,1'i32)