// TrayDnView.cpp : implementation of the CTrayDnView class
//

#include "stdafx.h"
#include "TrayDn.h"
#include "MainFrm.h"
#include "TrayDnDoc.h"
#include "TrayDnView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTrayDnView

#define LOCAL 1
#define SERVER 2

IMPLEMENT_DYNCREATE(CTrayDnView, CView)

BEGIN_MESSAGE_MAP(CTrayDnView, CView)
	//{{AFX_MSG_MAP(CTrayDnView)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrayDnView construction/destruction

CTrayDnView::CTrayDnView()
{
	sTemp = new char[100];
	m_PisStock = NULL;
	m_LocalStock = NULL;
}

CTrayDnView::~CTrayDnView()
{
	if ( m_db.IsOpen() ) m_db.Close();
	if (m_LocalStock && m_LocalStock->IsOpen() ) m_LocalStock->Close();
	if (m_PisStock &&  m_PisStock->IsOpen() ) m_PisStock->Close();

	delete m_LocalStock;
	delete m_PisStock;
	delete []sTemp;
}

BOOL CTrayDnView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CTrayDnView drawing

void CTrayDnView::OnDraw(CDC* pDC)
{
	CTrayDnDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CTrayDnView printing

BOOL CTrayDnView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CTrayDnView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CTrayDnView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CTrayDnView diagnostics

#ifdef _DEBUG
void CTrayDnView::AssertValid() const
{
	CView::AssertValid();
}

void CTrayDnView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CTrayDnDoc* CTrayDnView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTrayDnDoc)));
	return (CTrayDnDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CTrayDnView message handlers

int CTrayDnView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	char	*col_list[30] = {"FLAG", "CAR", "BODY", "PLANT",
							 "LINE", "EVENT", "PO", "COC", "T/YEAR",
							 "M/YEAR", "B/TYPE", "T/LEVEL", "LEVEL_DESC",
							 "USAGE", "DRIVER", "B/CLR", "T/CLR",
							 "VIN", "L/EVENT", "WEIGHT1", "WEIGHT2",
							 "WEIGHT3", "WEIGHT4", "CAR_DESC",
							 "CAR_SHRT_DESC", "FREE_OPTION", "CHK",
							 "REG_DATE", "REG_TIME", "RSTR_EVT"};
	int		col_width[30] = {50, 40, 50, 50, 50, 50, 50, 50, 80, 80, 80, 100, 100,
							 50, 60, 60, 60, 100, 60, 60, 60, 60, 60, 200,
							 100, 200, 50, 80, 80, 50};

	m_list.Create(
		WS_VISIBLE | WS_CHILD | WS_BORDER | LVS_REPORT | LVS_EDITLABELS,
		CRect(0,0,0,0),
		this,
		IDC_LIST);
	
	lvcolumn.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
	for ( int i=0; i<29; i++ )
	{
		lvcolumn.fmt = LVCFMT_CENTER;
		lvcolumn.pszText = col_list[i];
		lvcolumn.iSubItem = i;
		lvcolumn.cx = col_width[i];

		if ( m_list.InsertColumn(i, &lvcolumn) == -1)
			return NULL;
	}
	
	return 0;
}

void CTrayDnView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
	m_list.MoveWindow(0, 0, cx, cy);
}

void CTrayDnView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	CMainFrame*		pFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;
	CStatusBar*		pStatus = &pFrame->m_wndStatusBar;

	WORD wVersionRequested;  
	WSADATA wsaData; 
	wVersionRequested = MAKEWORD(1, 1); 
 
	WSAStartup(wVersionRequested, &wsaData); 

	if ( (m_phostent = gethostbyname("PIS")) != NULL )
	{
		memcpy(&in_addr, m_phostent->h_addr, 4);
		m_ip.Format("%s", inet_ntoa(in_addr));
	}

	WSACleanup();

	ListView_SetExtendedListViewStyle(m_list, 
			LVS_EX_FULLROWSELECT | 
			LVS_EX_GRIDLINES | 	
			LVS_EX_TRACKSELECT |
			LVS_EX_HEADERDRAGDROP
			);

	try
	{
		if ( !m_db.IsOpen() ) m_db.OpenEx("DSN=PIS;UID=ROSTOV;PWD=ROSTOV;", CDatabase::noOdbcDialog);
		
		m_PisStock = new CPisStock(&m_db);
		m_PisStock->Open(m_ip);
	
		if ( pStatus ) pStatus->SetPaneText(1, "Databases were opened succesfully");
	}
	
	catch (CDBException *e)
	{
		e->Delete();
	}

	m_LocalStock = new CLocalStock;
	m_LocalStock->Open();

	SetTimer(TRAYDN_TIMER, 10000, NULL);
}


void CTrayDnView::OnTimer(UINT nIDEvent) 
{
	KillTimer(TRAYDN_TIMER);

	CheckSystem();
	DoIt(nIDEvent);

	SetTimer(TRAYDN_TIMER, 10000, NULL);

	CView::OnTimer(nIDEvent);
}


void CTrayDnView::DoIt(UINT nIDEvent)
{
	CMainFrame*		pFrame = (CMainFrame *)AfxGetApp()->m_pMainWnd;
	CStatusBar*		pStatus = &pFrame->m_wndStatusBar;

	int				nCnt;
	int				Failure;

	m_list.DeleteAllItems();

	lvitem.mask = LVIF_TEXT;
	lvitem.iItem = 0;

	try
	{
		if ( !m_db.IsOpen() )
		{
			Failure = SERVER;
 			m_db.OpenEx( "DSN=PIS;UID=ROSTOV;PWD=ROSTOV;", CDatabase::noOdbcDialog );
			Failure = 0;

			if ( m_PisStock )  delete m_PisStock;
			m_PisStock  = new CPisStock(&m_db);
			m_PisStock->Open(m_ip);
			
		/*	if ( m_LocalStock ) delete m_LocalStock;
			m_LocalStock  = new CLocalStock(&m_db);
			m_LocalStock->Open();*/
			
			if ( pStatus ) pStatus->SetPaneText(1, "Databases were opened succesfully");
		}

		Failure = SERVER;

		if ( m_PisStock->IsOpen() ) m_PisStock->Requery();
		else m_PisStock->Open(m_ip);

		Failure = 0;
		if ( !m_LocalStock->IsOpen() )
		{
			Failure = LOCAL;
			m_LocalStock->Open() ;
			Failure = 0;
		}
		else m_LocalStock->Requery();

		if( !m_PisStock->IsEOF() ) 
		{
			while ( !m_PisStock->IsEOF() )
			{
				sprintf(sTemp, "%s", m_PisStock->m_FLAG);
				lvitem.iSubItem = 0;
				lvitem.pszText = sTemp;
				m_list.InsertItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_TYPE);
				lvitem.iSubItem = 1;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_BODY_NO);
				lvitem.iSubItem = 2;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_PLANT);
				lvitem.iSubItem = 3;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_LINE);
				lvitem.iSubItem = 4;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_EVENT);
				lvitem.iSubItem = 5;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_PO_NO);
				lvitem.iSubItem = 6;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_COC);
				lvitem.iSubItem = 7;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_TYPE_YEAR);
				lvitem.iSubItem = 8;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_MODEL_YEAR);
				lvitem.iSubItem = 9;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_BODY_TYPE);
				lvitem.iSubItem = 10;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_TRIM_LEVEL);
				lvitem.iSubItem = 11;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_LEVEL_DESC);
				lvitem.iSubItem = 12;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_USAGE);
				lvitem.iSubItem = 13;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_DRIVER);
				lvitem.iSubItem = 14;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_BODY_COLOR);
				lvitem.iSubItem = 15;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_TRIM_COLOR);
				lvitem.iSubItem = 16;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_VIN);
				lvitem.iSubItem = 17;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_LAST_EVENT);
				lvitem.iSubItem = 18;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_WEIGHT1);
				lvitem.iSubItem = 19;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_WEIGHT2);
				lvitem.iSubItem = 20;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_WEIGHT3);
				lvitem.iSubItem = 21;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_WEIGHT4);
				lvitem.iSubItem = 22;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_DESC);
				lvitem.iSubItem = 23;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CAR_SHRT_DESC);
				lvitem.iSubItem = 24;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_FREE_OPTION);
				lvitem.iSubItem = 25;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_CHK);
				lvitem.iSubItem = 26;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_REG_DATE);
				lvitem.iSubItem = 27;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_REG_TIME);
				lvitem.iSubItem = 28;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);

				sprintf(sTemp, "%s", m_PisStock->m_RSTR_EVT);
				lvitem.iSubItem = 29;
				lvitem.pszText = sTemp;
				m_list.SetItem(&lvitem);


				m_PisStock->MoveNext();
			}	// while loop

			nCnt = m_list.GetItemCount();
			for ( int i=0; i<nCnt; i++ )
			{
				switch ( m_list.GetItemText(i,0).GetAt(0) )
				{
					case 'I' :
						m_LocalStock->AddNew();

						m_LocalStock->m_CAR_TYPE = m_list.GetItemText(i, 1);
						m_LocalStock->m_BODY_NO = m_list.GetItemText(i, 2);
						m_LocalStock->m_PLANT = m_list.GetItemText(i, 3);
						m_LocalStock->m_LINE = m_list.GetItemText(i, 4);
						m_LocalStock->m_EVENT = m_list.GetItemText(i, 5);
						m_LocalStock->m_PO_NO = m_list.GetItemText(i, 6);
						m_LocalStock->m_COC = m_list.GetItemText(i, 7);
						m_LocalStock->m_TYPE_YEAR = m_list.GetItemText(i, 8);
						m_LocalStock->m_MODEL_YEAR = m_list.GetItemText(i, 9);
						m_LocalStock->m_BODY_TYPE = m_list.GetItemText(i, 10);
						m_LocalStock->m_TRIM_LEVEL = m_list.GetItemText(i, 11);
						m_LocalStock->m_LEVEL_DESC = m_list.GetItemText(i, 12);
						m_LocalStock->m_USAGE = m_list.GetItemText(i, 13);
						m_LocalStock->m_DRIVER = m_list.GetItemText(i, 14);
						m_LocalStock->m_BODY_COLOR = m_list.GetItemText(i, 15);
						m_LocalStock->m_TRIM_COLOR = m_list.GetItemText(i, 16);
						m_LocalStock->m_VIN = m_list.GetItemText(i, 17);
						m_LocalStock->m_LAST_EVENT = m_list.GetItemText(i, 18);
						m_LocalStock->m_CAR_WEIGHT1 = m_list.GetItemText(i, 19);
						m_LocalStock->m_CAR_WEIGHT2 = m_list.GetItemText(i, 20);
						m_LocalStock->m_CAR_WEIGHT3 = m_list.GetItemText(i, 21);
						m_LocalStock->m_CAR_WEIGHT4 = m_list.GetItemText(i, 22);
						m_LocalStock->m_CAR_DESC = m_list.GetItemText(i, 23);
						m_LocalStock->m_CAR_SHRT_DESC = m_list.GetItemText(i, 24);
						m_LocalStock->m_FREE_OPTION = m_list.GetItemText(i, 25);
						m_LocalStock->m_REG_DATE = m_list.GetItemText(i, 27);
						m_LocalStock->m_REG_TIME = m_list.GetItemText(i, 28);
						m_LocalStock->m_RSTR_EVT= m_list.GetItemText(i, 29);

						if ( m_LocalStock->CanUpdate() )
						{
							CString		sSQL;

							sSQL += "UPDATE STOCK_DUMY ";
							sSQL += "SET CHK = '*' ";
							sSQL += "WHERE CAR_TYPE = '" + m_list.GetItemText(i, 1) + "' ";
							sSQL += "AND BODY_NO = '" + m_list.GetItemText(i, 2) + "' ";
							sSQL += "AND IP_ADDR = '" + m_ip + "'";

							m_db.ExecuteSQL(sSQL);

							sSQL.Empty();

							m_LocalStock->Update();
						}

						break;

					case 'D' :
						m_LocalStock->MoveFirst();
						while ( !m_LocalStock->IsEOF() )
						{
							if ( strcmp(m_LocalStock->m_CAR_TYPE, m_list.GetItemText(i, 1)) == 0 &&
								strcmp(m_LocalStock->m_BODY_NO, m_list.GetItemText(i, 2)) == 0 )
							{
								m_LocalStock->Delete();
								m_LocalStock->MoveFirst();

								if ( m_LocalStock->CanUpdate() )
								{
									CString		sSQL;

									sSQL += "UPDATE STOCK_DUMY ";
									sSQL += "SET CHK = '*' ";
									sSQL += "WHERE CAR_TYPE = '" + m_list.GetItemText(i, 1) + "' ";
									sSQL += "AND BODY_NO = '" + m_list.GetItemText(i, 2) + "' ";
									sSQL += "AND IP_ADDR = '" + m_ip + "'";

									m_db.ExecuteSQL(sSQL);

									sSQL.Empty();

									//m_LocalStock->Update();

									break;
								}
							}

							m_LocalStock->MoveNext();
						}

						break;

					case 'U' :
						m_LocalStock->MoveFirst();
						while ( !m_LocalStock->IsEOF() )
						{
							if ( strcmp(m_LocalStock->m_CAR_TYPE, m_list.GetItemText(i, 1)) == 0 &&
								strcmp(m_LocalStock->m_BODY_NO, m_list.GetItemText(i, 2)) == 0 )
							{
								m_LocalStock->Edit();
								m_LocalStock->m_PLANT = m_list.GetItemText(i, 3);
								m_LocalStock->m_LINE = m_list.GetItemText(i, 4);
								m_LocalStock->m_EVENT = m_list.GetItemText(i, 5);
								m_LocalStock->m_PO_NO = m_list.GetItemText(i, 6);
								m_LocalStock->m_COC = m_list.GetItemText(i, 7);
								m_LocalStock->m_TYPE_YEAR = m_list.GetItemText(i, 8);
								m_LocalStock->m_MODEL_YEAR = m_list.GetItemText(i, 9);
								m_LocalStock->m_BODY_TYPE = m_list.GetItemText(i, 10);
								m_LocalStock->m_TRIM_LEVEL = m_list.GetItemText(i, 11);
								m_LocalStock->m_LEVEL_DESC = m_list.GetItemText(i, 12);
								m_LocalStock->m_USAGE = m_list.GetItemText(i, 13);
								m_LocalStock->m_DRIVER = m_list.GetItemText(i, 14);
								m_LocalStock->m_BODY_COLOR = m_list.GetItemText(i, 15);
								m_LocalStock->m_TRIM_COLOR = m_list.GetItemText(i, 16);
								m_LocalStock->m_VIN = m_list.GetItemText(i, 17);
								m_LocalStock->m_LAST_EVENT = m_list.GetItemText(i, 18);
								m_LocalStock->m_CAR_WEIGHT1 = m_list.GetItemText(i, 19);
								m_LocalStock->m_CAR_WEIGHT2 = m_list.GetItemText(i, 20);
								m_LocalStock->m_CAR_WEIGHT3 = m_list.GetItemText(i, 21);
								m_LocalStock->m_CAR_WEIGHT4 = m_list.GetItemText(i, 22);
								m_LocalStock->m_CAR_DESC = m_list.GetItemText(i, 23);
								m_LocalStock->m_CAR_SHRT_DESC = m_list.GetItemText(i, 24);
								m_LocalStock->m_FREE_OPTION = m_list.GetItemText(i, 25);
								m_LocalStock->m_REG_DATE = m_list.GetItemText(i, 27);
								m_LocalStock->m_REG_TIME = m_list.GetItemText(i, 28);
								m_LocalStock->m_RSTR_EVT = m_list.GetItemText(i, 29);

								if ( m_LocalStock->CanUpdate() )
								{
									CString		sSQL;

									sSQL += "UPDATE STOCK_DUMY ";
									sSQL += "SET CHK = '*' ";
									sSQL += "WHERE CAR_TYPE = '" + m_list.GetItemText(i, 1) + "' ";
									sSQL += "AND BODY_NO = '" + m_list.GetItemText(i, 2) + "' ";
									sSQL += "AND IP_ADDR = '" + m_ip + "'";

									m_db.ExecuteSQL(sSQL);

									sSQL.Empty();

									m_LocalStock->Update();
								}

								break;
							}

							m_LocalStock->MoveNext();
						}

						break;
				}
			}	// for loop

		}	// m_PisStock is EOF

	}
	catch( CDBException* e )
	{
		e->Delete();

		if ( pStatus ) 
		{
			if ( Failure==LOCAL ) pStatus->SetPaneText(1, "Fail to open Local Database");
			else
			{
				if ( Failure == SERVER ) pStatus->SetPaneText(1, "Fail to open Database");
				else pStatus->SetPaneText(1, "Unknown Failure");
			}

			m_db.Close();
			if ( m_PisStock && m_PisStock->IsOpen() ) m_PisStock->Close();
			if ( m_PisStock )
			{
				delete m_PisStock;
				m_PisStock = NULL;
			}

		/*	if ( m_LocalStock && m_LocalStock->IsOpen() ) m_LocalStock->Close();
			if ( m_LocalStock )
			{
				delete m_LocalStock;
				m_LocalStock = NULL;
			}*/
		}
	}
	
}


BOOL CTrayDnView::QueryProcessInformation(   DWORD pid,
                                PROCESSINFOCLASS infoEnum,
                                void * pBuffer,
                                unsigned cbBuffer )
{
//    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, pid );
    HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pid );
    if ( !hProcess )
        return FALSE;

    // NtQueryInformationProcess에게 전달될 버퍼를 0으로 채운다...
    memset( pBuffer, 0, cbBuffer );

    DWORD retLen;
    int retValue;

    retValue = NtQueryInformationProcess(   hProcess, infoEnum,
                                            pBuffer, cbBuffer, &retLen );
//	retValue = -1;

    CloseHandle( hProcess );

    if ( retValue < 0 ) // NtQueryInformationProcess가 실패했을 때는 음의 값을
        return FALSE;   // 되돌린다...

    return TRUE;
}

int CTrayDnView::CheckSystem()
{
    DWORD aProcesses[1024], cbNeeded;
	BOOL	bFile = FALSE;

	STARTUPINFO	si;
	SECURITY_ATTRIBUTES	saProcess, saThread;
	PROCESS_INFORMATION pi;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return 0;
    
    DWORD cProcesses = cbNeeded / sizeof(DWORD);

    for ( unsigned i = 0; i < cProcesses; i++ )
    {
        char szProcessName[MAX_PATH] = "unknown";
        DWORD pid = aProcesses[i];
        
        HANDLE hProcess = OpenProcess(  PROCESS_ALL_ACCESS, FALSE, pid );

        if ( !hProcess ) continue;

        HMODULE hMod;
        DWORD cbNeeded;
    
        if ( EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName,
                                sizeof(szProcessName) );

			if ( strncmp("TrayUp.exe", szProcessName, 10) == 0 )
			{
				PROCESS_BASIC_INFORMATION pbi;
				if ( QueryProcessInformation(   pid, ProcessBasicInformation,
												&pbi, sizeof(pbi)) )
				{
					bFile = TRUE;
				}
			}
		}
        CloseHandle( hProcess );
	}

	if ( !bFile )
	{
		memset(&si, 0, sizeof(STARTUPINFO));
		si.cb = sizeof(si);

		saProcess.nLength = sizeof(saProcess);
		saProcess.lpSecurityDescriptor = NULL;
		saProcess.bInheritHandle = TRUE;

		saThread.nLength = sizeof(saProcess);
		saThread.lpSecurityDescriptor = NULL;
		saThread.bInheritHandle = TRUE;

		CreateProcess(
			"TrayUp.exe",
			NULL,
			&saProcess,
			&saThread,
			FALSE,
			0,
			NULL,
			NULL,
			&si,
			&pi);
	}

	return 0;
}

