问题描述
- system()函数为什么在这里会出错呢?望大神指点
-
第一次照猫画虎的写了个win32程序,打算在程序中用system()执行外部命令,但好像有问题,不知道为什么。下面是代码,有点乱,请不要介意。#include
#include
#include
#include
#include "resource.h"const char szClassName[] = "myWindowClass";
NOTIFYICONDATA nid ;LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){ case WM_CREATE : CreateWindow(TEXT("button"),TEXT("OK"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 100,120,100,30,hwnd,(HMENU)ID_BUTTON_OK, ((LPCREATESTRUCT)lParam)->hInstance,NULL); CreateWindow(TEXT("edit"),TEXT(""), WS_CHILD|WS_VISIBLE|ES_LEFT|ES_NUMBER, 125,70,50,30, hwnd,(HMENU)ID_EDIT,((LPCREATESTRUCT)lParam)->hInstance,NULL); break; case WM_IAWENTRAY: if(wParam == IDI_MYICON){ if(lParam == WM_LBUTTONDOWN){ ShowWindow(hwnd, SW_SHOWNORMAL); printf("showWindows"); } } break; case WM_SYSCOMMAND: switch(wParam) { case SC_CLOSE: DestroyWindow(hwnd); PostQuitMessage(0); break; case SC_MINIMIZE: ShowWindow(hwnd,SW_HIDE); Shell_NotifyIcon(NIM_ADD,&nid); break; } return DefWindowProc(hwnd,msg,wParam,lParam); break; case WM_CLOSE : DestroyWindow(hwnd); break; case WM_DESTROY : Shell_NotifyIcon(NIM_DELETE,&nid); PostQuitMessage(0); break; case WM_COMMAND : switch(LOWORD(wParam)){ case ID_FILE_EXIT : PostMessage(hwnd,WM_CLOSE,0,0); break; case ID_BUTTON_OK : int length = GetWindowTextLength(GetDlgItem(hwnd, ID_EDIT)); char command[50] = "shutdown -s -t "; if(length > 0) { char message[100] = "系统将在" ; char* buf = (char*)GlobalAlloc(GPTR, length + 1); GetDlgItemText(hwnd,ID_EDIT,buf,length+1); int time = atoi(buf); time *= 60 ; strcat(message,buf); strcat(message,"分钟后关闭"); GlobalFree((HANDLE)buf); itoa(time,buf,10); strcat(command,buf); GlobalFree((HANDLE)buf); MessageBox( NULL, TEXT(message), TEXT("提示"), MB_OK | MB_ICONINFORMATION); system(command); // 就是这里 SetWindowText((HWND)lParam,"Cancle"); PostMessage(hwnd,WM_SYSCOMMAND,SC_MINIMIZE,0); }else{ MessageBox( NULL, TEXT("请至少输入一些东西"), TEXT("警告"), MB_OK | MB_ICONINFORMATION); } system(command); break; }
break;
default :
return DefWindowProc(hwnd,msg,wParam,lParam); } return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc; HWND hwnd ; MSG msg ; wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU); wc.hInstance = hInstance; wc.hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_MYICON)); wc.hCursor = LoadCursor(GetModuleHandle(NULL),IDC_ARROW); wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND ; wc.hIconSm = LoadIcon(NULL,MAKEINTRESOURCE(IDI_MYICON)); wc.lpszClassName = szClassName ; if(!RegisterClassEx(&wc)){ MessageBox(NULL,"some things happened in class","Error",MB_ICONEXCLAMATION|MB_OK); return 0; } hwnd = CreateWindowEx(0,szClassName,"ShutDown",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,320,240, NULL,NULL,hInstance,NULL); if(hwnd == NULL){ MessageBox(NULL,"some things happened","Error",MB_ICONEXCLAMATION|MB_OK); return 0; } ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); nid.cbSize = sizeof(NOTIFYICONDATA); nid.hWnd = hwnd; nid.uID = IDI_MYICON; nid.uCallbackMessage=WM_IAWENTRAY ; nid.hIcon=LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON)); nid.uFlags=NIF_ICON | NIF_MESSAGE | NIF_TIP; _tcscpy(nid.szTip,TEXT("TimeWork正在工作!rn点击打开主界面")); while(GetMessage(&msg, NULL, 0, 0) > 0){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;
}
解决方案
改变为 char command[50] = "shutdown /s /t "
试试。
char command[50] = "shutdown /s";是直接关机
解决方案二:
shoudown -s -t 后面还要加个时间参数吧是不是少了个参数
时间: 2024-11-03 14:37:36