void CMDExecute(void temp)
{
DWORD bytesRead;
//unsigned char buffer[4097]={0};
unsigned char buffer[4097]={0};
int *args=(int *)temp;
//THe address..
string *hs_Command=(string *)args[0];
string *hs_Result=(string)args[1];
//unsigned char *hs_Result =(unsigned char *)args[1];
STARTUPINFO hs_StartInfo;
PROCESS_INFORMATION hs_ProcessInformation;
SECURITY_ATTRIBUTES hs_SecurityAttributes;
HANDLE hs_PipeRead,hs_PipeWrite;
memset(&hs_StartInfo,0,sizeof(STARTUPINFO));
memset(&hs_ProcessInformation,0,sizeof(PROCESS_INFORMATION));
memset(&hs_SecurityAttributes,0,sizeof(SECURITY_ATTRIBUTES));
hs_SecurityAttributes.nLength=sizeof(hs_SecurityAttributes);
hs_SecurityAttributes.lpSecurityDescriptor=NULL;
hs_SecurityAttributes.bInheritHandle=TRUE;
if (!CreatePipe(&hs_PipeRead,&hs_PipeWrite,&hs_SecurityAttributes,0))
{
return;
}
GetStartupInfo(&hs_StartInfo);
hs_StartInfo.cb=sizeof(STARTUPINFO);
hs_StartInfo.dwFlags=STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
hs_StartInfo.wShowWindow=SW_HIDE;
hs_StartInfo.hStdError=hs_StartInfo.hStdOutput=hs_PipeWrite;
hs_StartInfo.hStdInput=hs_PipeWrite;
#ifdef UNICODE
int wclen= MultiByteToWideChar(CP_ACP,0,hs_Command->c_str(),strlen(hs_Command->c_str())+1,NULL,0);
LPTSTR lpcmd=new WCHAR[wclen];
MultiByteToWideChar(CP_ACP,0,hs_Command->c_str(),strlen(hs_Command->c_str())+1,lpcmd,wclen);
#else
string lpcmd;
/*
use & to return..
char * to re
*/
UTF8ToANSI((char *)hs_Command->c_str(),&lpcmd);
#endif
#ifdef UNICODE
if (CreateProcess(NULL,lpcmd,NULL,NULL,1,0,NULL,NULL,&hs_StartInfo,&hs_ProcessInformation))
#else
//if (CreateProcess(NULL,(char *)lpcmd.c_str(),NULL,NULL,1,0,NULL,NULL,&hs_StartInfo,&hs_ProcessInformation))
if (CreateProcess(NULL,(char *)lpcmd.c_str(),NULL,NULL,1,0,NULL,NULL,&hs_StartInfo,&hs_ProcessInformation))
#endif
//if (CreateProcess(NULL,lpcmd,NULL,NULL,1,0,NULL,NULL,&hs_StartInfo,&hs_ProcessInformation))
{
// WaitForSingleObject(hs_ProcessInformation.hProcess,INFINITE);
CloseHandle(hs_ProcessInformation.hProcess);
CloseHandle(hs_ProcessInformation.hThread);
CloseHandle(hs_PipeWrite);
#ifdef UNICODE
delete [] lpcmd;
#endif
}
else
{
CloseHandle(hs_PipeRead);
CloseHandle(hs_PipeWrite);
#ifdef UNICODE
delete [] lpcmd;
#endif
return;
}
while (true)
{
if (!ReadFile(hs_PipeRead,buffer,4096,&bytesRead,NULL))
{
break;
}
*hs_Result+=buffer;
//hs_Result = CatUnSignedChar(hs_Result,buffer);
memset(buffer,0,4097);
}
CloseHandle(hs_PipeRead);
}
void Base64Encode(unsigned char const* bytes_to_encode, unsigned int in_len,string &ret)
{
const string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];
while (in_len--)
{
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++)
{
ret += base64_chars[char_array_4[i]];
}
i = 0;
}
}
if (i)
{
for(j = i; j < 3; j++)
{
char_array_3[j] = '\0';
}
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
{
ret += base64_chars[char_array_4[j]];
}
while((i++ < 3))
{
ret += '=';
}
}
}