自己写个程序搞定喽!
请在win32空项目中粘贴下列代码。
#include
//请保证未打开其他命令行窗口,否则可能会有未知错误
void SendCommandMessage(HWND hWnd,char *str)
{
char *p = str;
while(*p != '\0')
{
::SendMessage(hWnd,WM_CHAR,*p,NULL);
p++;
}
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
::ShellExecute(NULL,TEXT("open"),TEXT("cmd.exe"),NULL,NULL,SW_SHOW);
Sleep(500);
HWND hWnd = ::FindWindow(TEXT("ConsoleWindowClass"),NULL);
if(!hWnd)
{
MessageBox(NULL,TEXT("未找到命令行窗口!"),NULL,MB_OK);
return 0;
}
char *str = "vcvars32.bat";
SendCommandMessage(hWnd,str);
SendMessage(hWnd,WM_CHAR,VK_RETURN,NULL);
//可继续发送多个命令……
/*
char *str1 = "command1.bat";
SendCommandMessage(hWnd,str1);
SendMessage(hWnd,WM_CHAR,VK_RETURN,NULL);
char *str2 = "command2.bat";
SendCommandMessage(hWnd,str1);
SendMessage(hWnd,WM_CHAR,VK_RETURN,NULL);
……
*/
return 0;
}
下面这个不受其他打开的窗口影响。
#include
BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam);
void SendCommandMessage(HWND hWnd,char *str);
PROCESS_INFORMATION pi;
STARTUPINFO si;
HWND g_hWnd = NULL;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( L"c:\\windows\\system32\\cmd.exe", // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
MessageBox(NULL,L"CREATE PROCESS FAILED!",NULL,MB_OK);
return 0;
}
Sleep(500);//必须睡眠一段时间否则可能枚举不到新创建的进程
::EnumWindows(EnumWindowsProc,NULL);
HWND hWnd = g_hWnd;
if(!hWnd)
{
MessageBox(NULL,TEXT("未找到命令行窗口!"),NULL,MB_OK);
return 0;
}
char *str = "vcvars32.bat";
SendCommandMessage(hWnd,str);
SendMessage(hWnd,WM_CHAR,VK_RETURN,NULL);
/*
char *str1 = "command1.bat";
SendCommandMessage(hWnd,str1);
SendMessage(hWnd,WM_CHAR,VK_RETURN,NULL);
char *str2 = "command2.bat";
SendCommandMessage(hWnd,str1);
SendMessage(hWnd,WM_CHAR,VK_RETURN,NULL);
……
*/
return 0;
}
void SendCommandMessage(HWND hWnd,char *str)
{
char *p = str;
while(*p != '\0')
{
::SendMessage(hWnd,WM_CHAR,*p,NULL);
p++;
}
}
BOOL CALLBACK EnumWindowsProc( HWND hwnd, LPARAM lParam)
{
DWORD dwID;
GetWindowThreadProcessId(hwnd,&dwID);
if(pi.dwProcessId == dwID)
{
g_hWnd = hwnd;
return FALSE;
}
return TRUE;
}
如果你不嫌麻烦,非要用CMD去运行csc的话,
你就把CSC拷贝到system32目录下吧。
安装的时候环境变量软件已经帮你设置好了的,如果你要用其他的库,可以再改环境变量。