2016-11-15 118 views
-1

我正在使用Dev C++ SDK在Windows API(windows.h頭文件)的幫助下使用C++語言創建大學項目。如何在C++程序中運行批處理文件(.bat)或批處理代碼?

我必須創建一個窗口接口,它包含一個按鈕,我想用該按鈕打開批處理文件(.bat)或運行存儲在程序中的批處理代碼。

我用Zetcode博客上的Windows API教程創建了一個帶有按鈕的窗口。

但我不能使用該按鈕打開批處理文件(.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; 
} 

截圖:

我的項目窗口與diaglog框和按鈕

enter image description here

程序代碼

enter image description here

+0

請不要張貼代碼的照片,但張貼代碼。 –

+0

[mcve]確實意味着最小化。請修復您的問題(刪除與問題無關的所有問題)。沒有任何窗口代碼是相關的。 – IInspectable

+0

[使用CreateProcess運行批處理文件]的可能重複(http://stackoverflow.com/questions/25919451/use-createprocess-to-run-a-batch-file) – IInspectable

回答

1

有很多選擇,推出一個新的進程。例如,您可以使用system函數,spawn函數,也可以使用Win32 API中的CreateProcess。他們採取不同的參數,並且可能會有稍微不同的行爲。他們各自的文檔應詳細說明確切的行爲,並取決於您傳遞的參數。請注意,由於您將在Windows函數處理程序內運行新進程,因此如果使用阻塞調用(例如system),則啓動該進程的窗口將凍結,直到執行的進程退出。