I am creating a college project in C++ language using Dev C++ SDK, with the help of Windows API (windows.h header).
我在Windows API(windows.h header)的帮助下,使用Dev C ++ SDK创建了一个C ++语言的大学项目。
I have to create a Window Interface which consist of a button and I want to use that button to open a batch file (.bat) or run batch code stored in the program.
我必须创建一个由按钮组成的窗口界面,我想使用该按钮打开批处理文件(.bat)或运行存储在程序中的批处理代码。
I have created a Window with a button by using your Windows API tutorials found on Zetcode blog.
我使用Zetcode博客上的Windows API教程创建了一个带按钮的窗口。
But I am not able to use that button to open a batch file (.bat) or run batch code stored in the program. Please tell me the code to implement this in my project. I am adding my program code and screenshot image of my program.
但我无法使用该按钮打开批处理文件(.bat)或运行存储在程序中的批处理代码。请告诉我在我的项目中实现这个的代码。我正在添加我的程序代码和我的程序的截图图像。
#include <windows.h>
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
switch(Message) {
case WM_CREATE:
CreateWindow(TEXT("button"), TEXT("Disable USB"),
WS_VISIBLE | WS_CHILD,
10, 10, 100, 25,
hwnd, (HMENU) 1, NULL, NULL
);
CreateWindow(TEXT("button"), TEXT("Enable Firewall"),
WS_VISIBLE | WS_CHILD,
120, 10, 120, 25,
hwnd, (HMENU) 2, NULL, NULL
);
break;
case WM_COMMAND:
// I need code which i can write here to execute batch code or run a bat file (.bat).
break;
/* Upon destruction, tell the main thread to stop */
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
/* All other messages (a lot of them) are processed using default procedures */
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc; /* A properties struct of our window */
HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
MSG msg; /* A temporary location for all messages */
/* zero out the struct and set the stuff we want to modify */
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
wc.hbrBackground = (GetSysColorBrush(COLOR_3DFACE));
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","System Hardening Tool",WS_VISIBLE|WS_SYSMENU|WS_MINIMIZEBOX|WS_MAXIMIZEBOX,
CW_USEDEFAULT, /* x */
CW_USEDEFAULT, /* y */
640, /* width */
480, /* height */
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
/*
This is the heart of our program where all input is processed and
sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
this loop will not produce unreasonably high CPU usage
*/
while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
TranslateMessage(&msg); /* Translate key codes to chars if present */
DispatchMessage(&msg); /* Send it to WndProc */
}
return msg.wParam;
}
Screenshots:
My Project Window with diaglog box and buttons
我的项目窗口有diaglog框和按钮
Program code
1 个解决方案
#1
1
There are many options to launch a new process. For example, you can use the system
function, the spawn
function, or you can use CreateProcess
from the Win32 API. They take different parameters, and may have slightly different behavior. Their respective documentation should detail the exact behavior, and it is dependent on the parameters you pass.
启动新流程有很多选择。例如,您可以使用系统函数,spawn函数,也可以使用Win32 API中的CreateProcess。它们采用不同的参数,并且行为可能略有不同。他们各自的文档应该详细说明确切的行为,它取决于您传递的参数。
Note that, since you will be running the new process inside a windows function handler, if you use a blocking call (such as with system
), the window launching the process will freeze until the executed process exits.
请注意,由于您将在Windows函数处理程序中运行新进程,如果使用阻塞调用(例如使用系统),启动进程的窗口将冻结,直到执行的进程退出。
#1
1
There are many options to launch a new process. For example, you can use the system
function, the spawn
function, or you can use CreateProcess
from the Win32 API. They take different parameters, and may have slightly different behavior. Their respective documentation should detail the exact behavior, and it is dependent on the parameters you pass.
启动新流程有很多选择。例如,您可以使用系统函数,spawn函数,也可以使用Win32 API中的CreateProcess。它们采用不同的参数,并且行为可能略有不同。他们各自的文档应该详细说明确切的行为,它取决于您传递的参数。
Note that, since you will be running the new process inside a windows function handler, if you use a blocking call (such as with system
), the window launching the process will freeze until the executed process exits.
请注意,由于您将在Windows函数处理程序中运行新进程,如果使用阻塞调用(例如使用系统),启动进程的窗口将冻结,直到执行的进程退出。