Re: Problem in running exe
Finally..
Manage to complete all stuff...
Attaching code in case some other programmer need help..
Code:
// test3.cpp : Defines the entry point for the application.
#include "Stdafx.h"
#include "windows.h"
#include "resource.h"
#include "stdio.h"
#include<process.h>
#define BUFSIZE 4096
HWND WindowInterface=NULL;
HINSTANCE WindowInterfaceInstance=NULL;
int CALLBACK WindowInterfaceProc(HWND hwnd,UINT mes,WPARAM wParam,LPARAM lParam);
HWND hPWindow;
BOOL m_bStopped = FALSE;
TCHAR szString[MAX_PATH];
TCHAR szString1[MAX_PATH];
HWND CANCEL;
HWND hwnd;
UINT mes;
WPARAM wParam;
LPARAM lParam;
//BOOL Abort = FALSE;
void AppendText(char *Msg)
{
static HWND Edit = NULL;
if(Edit == NULL)
Edit = GetDlgItem(hPWindow, IDC_EDIT1);
SetFocus(Edit);
SendMessage(Edit,EM_REPLACESEL,(WPARAM)0,(LPARAM) Msg);
}
void MyFunction(void *dummy)
{
HWND *hwnd = (HWND *)dummy;
HANDLE PipeReadHandle;
HANDLE PipeWriteHandle;
PROCESS_INFORMATION ProcessInfo;
SECURITY_ATTRIBUTES SecurityAttributes;
STARTUPINFOA StartupInfo;
BOOL Success;
ZeroMemory( &StartupInfo, sizeof( StartupInfo ));
ZeroMemory( &ProcessInfo, sizeof( ProcessInfo ));
ZeroMemory( &SecurityAttributes, sizeof( SecurityAttributes ));
SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle = TRUE;
SecurityAttributes.lpSecurityDescriptor = NULL;
Success = CreatePipe(&PipeReadHandle, &PipeWriteHandle, &SecurityAttributes, 0);
if(!Success)
{
MessageBox(NULL, "Error creating pipe", "Test", MB_OK);
_endthread();
}
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
StartupInfo.wShowWindow = SW_HIDE;
StartupInfo.hStdOutput = PipeWriteHandle;
StartupInfo.hStdError = PipeWriteHandle;
char szBuffer[MAX_PATH];
TCHAR Info[MAX_PATH];
GetDlgItemText(*hwnd, IDC_EDIT2, szString, 100);
GetDlgItemText(*hwnd, IDC_EDIT3, szString1, 100);
GetWindowsDirectory(Info , MAX_PATH);
sprintf(szBuffer, Info);
lstrcat(szBuffer, "\\system32\\");
lstrcat(szBuffer, szString);
lstrcat(szBuffer, " ");
lstrcat(szBuffer, szString1);
Success = CreateProcess(
NULL,
szBuffer,
NULL,
NULL,
TRUE,
0,
NULL,
NULL,
&StartupInfo,
&ProcessInfo);
if(!Success)
{
DWORD err = GetLastError();
MessageBox(NULL, "Error creating process", "Test", MB_OK);
_endthread();
}
DWORD BytesLeftThisMessage = 0;
DWORD NumBytesRead;
CHAR PipeData[2*MAX_PATH];
DWORD TotalBytesAvailable = 0;
while(1)
{
NumBytesRead = 0;
Success = PeekNamedPipe(PipeReadHandle, // handle to pipe to copy from
PipeData, // pointer to data buffer
1, // size, in bytes, of data buffer
&NumBytesRead, // pointer to number of bytes read
&TotalBytesAvailable, // pointer to total number of bytes available
&BytesLeftThisMessage // pointer to unread bytes in this message
);
if(!Success)
{
MessageBox(NULL, "PeekNamedPipe fialed", "Test", MB_OK);
break;
}
if(NumBytesRead)
{
Success = ReadFile(
PipeReadHandle, // handle to pipe to copy from
PipeData, // address of buffer that receives data
(sizeof(PipeData) - 1), // number of bytes to read
&NumBytesRead, // address of number of bytes read
NULL); // address of structure for data for overlapped I/O
if(!Success)
{
MessageBox(NULL, "ReadFile fialed", "Test", MB_OK);
break;
}
PipeData[NumBytesRead] = '\0';
AppendText(PipeData);
}
else
{
if(WaitForSingleObject(ProcessInfo.hProcess, 0) == WAIT_OBJECT_0)
break;
}
}
_endthread();
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WindowInterfaceInstance = hInstance;
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)WindowInterfaceProc);
return(0);
}
int CALLBACK WindowInterfaceProc(HWND hwnd, UINT mes, WPARAM wParam, LPARAM lParam)
{
switch(mes)
{
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDOK:
{
hPWindow = hwnd;
//MyFunction(hwnd);
//int a = 0;
//void *ptr = &a;
_beginthread(MyFunction, 0, &hwnd);
break;
}
case IDCANCEL:
{
m_bStopped = TRUE;
Sleep(20);
ExitProcess(0);
return TRUE;
break;
}
}
}
return TRUE;
case WM_CLOSE:
{
ExitProcess(0);
return TRUE;
}
case WM_DESTROY:
{
ExitProcess(0);
return TRUE;
}
}
return FALSE;
}