c++线程CreateThread以及调用参数的函数

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using std::cout;
using std::endl;
DWORD WINAPI TaskThreadExe(LPVOID lp);
typedef struct stuParam
{
std::string name;
int age;

}TstuParam;

int _tmain(int argc, _TCHAR* argv[])
{
/*
E:\Versioncheck\iCAN V001R003 集成定制专用版本\bin>VerCheckCmd.exe -b "E:\19\bas
e\iManagerM2000_CBSC_MATCH_ENG_V200R011C33SPC019.tar" -s ""E:\19\new\iManagerM20
00_CBSC_MATCH_CHS_V200R012C36SPC013_pkg.tar"" -a -r -o "E:\out"
*/
cout<<"start:>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
HANDLE s1;
TstuParam MyParam;
MyParam.name = "aaa";
MyParam.age = 12;

//通过线程去执行
s1 = CreateThread(NULL,0,TaskThreadExe,&MyParam,0,NULL);
cout<<"main thread is run:\r\n"<<endl;
::Sleep(4000);
CloseHandle(s1);
cout<<"main thread is end>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;


char *ptr[5] = {"beijing", "shanghai", "tianjin", "chongqing","aaaaaaaa"};
int len = sizeof(ptr)/sizeof(ptr[0]);
char *ptr2[5];
for(int i=0; i<len; i++){
printf("aaaa:%s\r\n", ptr[i]);
ptr2[i] = ptr[i];
//printf("bbbb:%s\r\n", ptr2[i]);
}
cout<<">>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
for(int i=0; i<len; i++){
printf("bbbb:%s\r\n", ptr2[i]);
}
cout<<len<<endl;
system("pause");

return 0;
}

void ExeCmd();
void ExecuteCmdCommand(const std::string &ToDoStr);
DWORD WINAPI TaskThreadExe(LPVOID lp)
{
cout<<">>>>>>>>>>>>>>>>>>>>>"<<endl;
TstuParam *myPara = (TstuParam *)lp;
std::string namet = myPara->name;
int aget = myPara->age;
//cout<<"姓名:"<<namet<<endl;
cout<<"年龄:"<<aget<<endl;
//cout<<"thread1 is running:\r\n"<<endl;
/* int i=0;
try
{
for (;i<2;i++)
{
cout<<"线程:>>>>>>>>>>>>>>>>>>>>>"<<i<<endl;
ExeCmd();
}
}
catch (...)
{
cout<<"出错了<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<i<<endl;
}
cout<<"thread is end<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<i<<endl;
*/

return 0;
}

void ExeCmd(){
//cout<<"线程:>>>>>>>>>>>>>>"<<endl;
std::string str = "\" \"E:\\Versioncheck\\iCAN V001R003 集成定制专用版本\\bin\\";
str += "VerCheckCmd.exe\" -b \"E:\\19\\base";
str += "\\iManagerM2000_CBSC_MATCH_ENG_V200R011C33SPC019.tar\" -s \"E:\\19\\new\\iManagerM2000_CBSC_MATCH_CHS_V200R012C36SPC013_pkg.tar\"";
str += " -c -f -p -r \"";
//str = "\" \"E:\\workProject\\VersionCheck\\集成\\Code\\debug\\VerCheckCmd.exe \"";
ExecuteCmdCommand(str);
//system(str.c_str());
}

void ExecuteCmdCommand(const std::string &ToDoStr)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW ;
si.wShowWindow = SW_HIDE;
std::string str("C:\\WINDOWS\\system32\\cmd.exe /c ");//+ToDoStr
str += ToDoStr;
//LPTSTR pSter = _T(str);
LPSTR pSter;
pSter = new char[str.length()+1];
::strcpy(pSter,str.c_str());

BOOL ret = CreateProcess(NULL, pSter, NULL, NULL, FALSE,0, NULL, NULL, &si, &pi);
//str.clear();
::WaitForSingleObject(pi.hProcess,INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值