///////////////////////////////////////////////////////////////////// // ActiveX.cpp : Implementation of CActiveX // // ActiveX Control for Demonstration // // Language: C++, MSVC++ .Net, ATL 7.0 // // Platform: Compaq, WinXP, SP1, MSDEV 7.0, .Net 1.0 // // Application: CSE775 - Distributed Objects processing, Spring' 03// // Author: Srinivasa Neerudu, MSCE, Syracuse University // // email:srneerud@ecs.syr.edu // ///////////////////////////////////////////////////////////////////// // ActiveX.cpp : Implementation of CActiveX #include "stdafx.h" #include "ActiveX.h" // CActiveX //----< thread processing function>------------------- unsigned int __stdcall CActiveX::threadProc(void *pArgs) { ((CActiveX*)pArgs)->initComponent(); ((CActiveX*)pArgs)->proc(); CoUninitialize(); return 1; } //----< accepts thr path to traverse..avoids client user interface freezing by spawining a thread to do processing>------------------- STDMETHODIMP CActiveX::acceptPath(BSTR pathToStart) { unsigned int threadID; castCharArray(pathToStart,path,512); HANDLE thHandle = (HANDLE)_beginthreadex(NULL, 0, threadProc, (void*) this, 0, &threadID ); SetThreadPriority((void *)&thHandle,THREAD_PRIORITY_LOWEST); return S_OK; } //----< called by the thread to create another instance of control >------------------- void CActiveX::initComponent() { try{ CoInitialize(NULL); HRESULT hr; COSERVERINFO csi; csi.dwReserved1 = 0; csi.dwReserved2 = 0; csi.pAuthInfo = NULL; csi.pwszName = L"127.0.0.1"; DWORD context = CLSCTX_SERVER; MULTI_QI mqi[1]; mqi[0].hr = 0; mqi[0].pIID = & IID_IActiveX; mqi[0].pItf = NULL; hr = CoCreateInstanceEx(CLSID_ActiveX,NULL,context,&csi,1,mqi); if( FAILED(hr)) throw _com_error(hr); if(FAILED(mqi[0].hr)) throw _com_error(hr); pActiveX = static_cast(mqi[0].pItf); } catch(_com_error e) { MessageBox(e.ErrorMessage()); } } //----< calls processing on the new control instance mde ion thread >------------------- void CActiveX::proc() { pActiveX->startProcessing(path); } //----< converst wide char array to char array >------------------- void CActiveX::castCharArray(wchar_t* wPtr, char* str, size_t size){ size = wcstombs(str, wPtr, size ); return; } //casts the string to wstring wstring CActiveX::castWStr(string& str) { wstring wstr; size_t size = str.length()+1; wchar_t *wcstr = new wchar_t[size]; size = mbstowcs( wcstr,str.c_str(), size ); wstr = wcstr; delete [] wcstr; return wstr; } //----< displays the file in the activeX control area and fires the event >------------------- void CActiveX::fileDisplay(string pathOfFile) { fileName=pathOfFile; BSTR file = ::SysAllocString(castWStr(fileName).c_str()); Fire_fileFound(file); ::SysFreeString(file); this->Invalidate(); } //----< starts the navigation of dir..Got from Dr. Fawcett's Navig Code >------------------- void CActiveX::start(string dir) { // convert "." or ".." into full path name char buffer[256]; if( ( dir == "." ) || ( dir == ".." ) ) { if( !SetCurrentDirectory( dir.c_str() ) ) { string temp = "could not find dir :"+ dir; MessageBox(temp.c_str()); } GetCurrentDirectory( 256, buffer); dir = buffer; } // start recursive directory walk walk(dir); } //----< walk directory tree rooted at dir >-------------------- void CActiveX::walk(const string &dir) { string fileMask = "*.*"; char fullName[512]; char curDir[ 256 ]; HANDLE fileHandle; WIN32_FIND_DATA findData; // save current dir so can restore it if( !GetCurrentDirectory( 256, curDir) ) return; // if the directory name is neither . or .. then // change to it, otherwise ignore it if( (dir != "." ) && (dir != ".." ) ) { if( !SetCurrentDirectory( dir.c_str() ) ) return; } else return; // Loop through all files in the directory fileHandle = FindFirstFile( fileMask.c_str(), &findData ); while ( fileHandle != INVALID_HANDLE_VALUE ) { // If the name is a directory, // recursively walk it. Otherwise print // print the file's data if( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) walk( findData.cFileName ); else { ::GetFullPathName(findData.cFileName,512,fullName,NULL); fileDisplay(fullName); } // loop thru remaining entries in the dir if (!FindNextFile( fileHandle, &findData )) break; } // clean up and restore directory FindClose( fileHandle ); SetCurrentDirectory( curDir ); } //----< starts the processing by calling start() >------------------- STDMETHODIMP CActiveX::startProcessing(CHAR* path) { start(path); return S_OK; }