Example #1
0
static void DirWalk (LPCTSTR pszRootPath,  BOOL fRecurse) {      // Expand subdirectories.

   TCHAR szCurrDir[_MAX_DIR];
   DIRWALKDATA DW;

   // Save the current directory so that it can 
   // be restored later.
   GetCurrentDirectory(chDIMOF(szCurrDir), szCurrDir);

   // Set the current directory to where we want
   // to start walking.
   SetCurrentDirectory(pszRootPath);

   // nDepth is used to control indenting. The value -1 will
   // cause the first level to display flush left.
   DW.nDepth = -1;
   DW.fRecurse = fRecurse;

   // Call the recursive function to walk the subdirectories.
   DirWalkRecurse(&DW);

   // Restore the current directory to what it was 
   // before the function was called.
   SetCurrentDirectory(szCurrDir);
}
Example #2
0
VOID FirstFunc(BOOL fLocal, COptex& optex) {

   optex.Enter();  // Gain ownership of the optex

   // Since this thread owns the optex, we should be able to get it again
   chVERIFY(optex.TryEnter()); 

   HANDLE hOtherThread = NULL;
   if (fLocal) {
      // Spawn a secondary thread for testing purposes (pass it the optex)

      DWORD dwThreadId;
      hOtherThread = chBEGINTHREADEX(NULL, 0, 
         SecondFunc, (PVOID) &optex, 0, &dwThreadId);

   } else {
      // Spawn a secondary process for testing purposes
      STARTUPINFO si = { sizeof(si) };
      PROCESS_INFORMATION pi;
      TCHAR szPath[MAX_PATH];
      GetModuleFileName(NULL, szPath, chDIMOF(szPath));
      CreateProcess(NULL, szPath, NULL, NULL, 
         FALSE, 0, NULL, NULL, &si, &pi);
      hOtherThread = pi.hProcess;
      CloseHandle(pi.hThread);
   }

   // Wait for the second thread to own the optex
   chMB("Primary: Hit OK to give optex to secondary");

   // Let the second thread gain ownership of the optex
   optex.Leave();
   optex.Leave();

   // Wait for the second thread to own the optex
   chMB("Primary: Hit OK to wait for the optex\n(Dismiss me 1st)");

   optex.Enter();  // Try to gain ownership back

   WaitForSingleObject(hOtherThread, INFINITE);
   CloseHandle(hOtherThread);
   optex.Leave();
}
Example #3
0
BOOL WINAPI DllMain(HINSTANCE hinstDll, DWORD fdwReason, PVOID fImpLoad) {

   if (fdwReason == DLL_PROCESS_ATTACH) {
      char szBuf[MAX_PATH * 100] = { 0 };

      PBYTE pb = NULL;
      MEMORY_BASIC_INFORMATION mbi;
      while (VirtualQuery(pb, &mbi, sizeof(mbi)) == sizeof(mbi)) {

         int nLen;
         char szModName[MAX_PATH];

         if (mbi.State == MEM_FREE)
            mbi.AllocationBase = mbi.BaseAddress;

         if ((mbi.AllocationBase == hinstDll) ||
             (mbi.AllocationBase != mbi.BaseAddress) ||
             (mbi.AllocationBase == NULL)) {
            // Do not add the module name to the list
            // if any of the following is true:
            // 1. If this region contains this DLL
            // 2. If this block is NOT the beginning of a region
            // 3. If the address is NULL
            nLen = 0;
         } else {
            nLen = GetModuleFileNameA((HINSTANCE) mbi.AllocationBase, 
               szModName, chDIMOF(szModName));
         }

         if (nLen > 0) {
            wsprintfA(strchr(szBuf, 0), "\n%p-%s", 
               mbi.AllocationBase, szModName);
         }

         pb += mbi.RegionSize;
      }

      chMB(&szBuf[1]);
   }

   return(TRUE);
}
Example #4
0
void DirWalk(LPCTSTR pszRootPath, LPCTSTR pszDesPATH, BOOL fRecurse)
{
	TCHAR szCurrDir[_MAX_DIR];
	DIRWALKDATA DW;

	//save the current dir so that it can be restored later
	GetCurrentDirectory(chDIMOF(szCurrDir), szCurrDir);

	//set the current dir to where we want to start walking
	SetCurrentDirectory(pszRootPath);

	//nDpeth is used to control indenting(缩进). the value -1 will cause the first level to display flush left
	DW.nDepth = -1;

	DW.fRecurse = fRecurse;

	//call DirWalkRecurse() to do the real work
	DirWalkRecurse(&DW, pszRootPath, pszDesPATH);

	//restore the current directory to what it was before DirWalkRecurse() was called
	SetCurrentDirectory(szCurrDir);
}
Example #5
0
DWORD WINAPI ThreadFunc(PVOID)
{
  TCHAR szIPFrom[18];
      TCHAR szReply[128];
     
      CIcmpReply IcmpReply;
#pragma chMSG(Funzione schifo ma tanto va rifatta tutta...)
      HWND hwnd = GetForegroundWindow();

      GetDlgItemText(hwnd, IDC_IPFROM, szIPFrom, chDIMOF(szIPFrom));

      WSADATA wsadata;
	    if (WSAStartup(MAKEWORD(2,2),&wsadata) != 0)
		    _tprintf(TEXT("WSAStartup %i\n"), WSAGetLastError());
      SOCKET s=WSASocket(AF_INET,SOCK_RAW,IPPROTO_IP,NULL,0,WSA_FLAG_OVERLAPPED);

      CIcmp Icmp;

      CInetAddr InAddr;

      BOOL bStatus = InAddr.LookUp(szIPFrom);
      assert(bStatus);
      Icmp.SendEcho(InAddr, NULL, 0, 64, 0, 0, 1000, &IcmpReply);

      wsprintf(szReply, TEXT("Reply from %x TTL %i RoundTrip %i %i bytes"), 
                        IcmpReply.Address, 
                        IcmpReply.Ttl,
                        IcmpReply.RoundTripTime,
                        IcmpReply.DataSize);

      //GetDlgItemText(hwnd, IDC_IPFROM, szFrom, chDIMOF(szFrom));
      //SetDlgItemText(hwnd, IDC_IPTO, szFrom);
      HWND hwndLB = GetDlgItem(hwnd, IDC_LBREPLY);
      ListBox_SetCurSel(hwndLB, ListBox_AddString(hwndLB, szReply));

      SOCKADDR_IN if0;
      
      if0.sin_port=0;
      if0.sin_family=AF_INET;
      ZeroMemory(&if0.sin_zero, sizeof(if0.sin_zero));
      
      if0.sin_addr.S_un.S_addr = InAddr;
	//Attiva la comunicazione usando le strutture SOCKADDR e SOCKADDR_IN, le 
	//quali contengono le informazioni per risolvere gli address Internet domains.
      if (bind(s,(SOCKADDR *)&if0,sizeof(if0)) == SOCKET_ERROR)
      {
        _tprintf(TEXT("Bind %i\n"), WSAGetLastError());
      }

      DWORD dwBytesRet=0;

      int optval=1;   

      if(WSAIoctl(s,SIO_RCVALL,&optval,sizeof(optval),NULL,0,&dwBytesRet,NULL,NULL)==SOCKET_ERROR)
      {
        _tprintf(TEXT("WSAIoctl %i\n"), WSAGetLastError());
      }

      for(;;)
      {
        char buf[MAX_IP_SIZE];
        dwBytesRet=0;
        DWORD dwFlags=0;
        WSABUF wsb;

        wsb.buf = buf;  
        wsb.len = MAX_IP_SIZE;
    		memset(wsb.buf,0x0,MAX_IP_SIZE);

        if(SOCKET_ERROR==WSARecv(s,&wsb,1,&dwBytesRet,&dwFlags,NULL,NULL))
        {
          DWORD error = WSAGetLastError();
          _tprintf(TEXT("%i\n"), error);
          break;
        }

        IN_ADDR inaddr;
        

        TCHAR szMsg[MAX_IP_SIZE];
        PIPHEADER iphdr = (PIPHEADER)(wsb.buf);
        
        inaddr.S_un.S_addr = iphdr->dwDestIP;
        char buff[60];

        strcpy_s(buff, 60, inet_ntoa(inaddr));

        //struct hostent* hent = gethostbyaddr(buff, strlen(buff), 0);
        //ASSERT(hent != NULL);
          
        
        wsprintf(szMsg, TEXT("Packet: %x %x %x %x"), iphdr->dwSourceIP,
                                                     iphdr->dwDestIP,
                                                     iphdr->cbNextProtocol,
                                                     iphdr->wTotalLen);

        ListBox_SetCurSel(hwndLB, ListBox_AddString(hwndLB, szMsg));
      }
      return 0;
}
Example #6
0
// Walk the directory structure and fill a list box with
// filenames. If pDW->fRecurse is set, list any child
// directories by recursively calling DirWalkRecurse.
static void DirWalkRecurse (LPDIRWALKDATA pDW) {
   HANDLE hFind;

   pDW->nDepth++;

   pDW->nIndent = 3 * pDW->nDepth;
   _stprintf(pDW->szBuf, __TEXT("%*s"), pDW->nIndent,
      __TEXT(""));

   GetCurrentDirectory(chDIMOF(pDW->szBuf) - pDW->nIndent,
      &pDW->szBuf[pDW->nIndent]);
   //store data in filenamelist
   /*
   _FileNameData fnd;
   memcpy(fnd.buf, pDW->szBuf, BUF_SIZE);
   fileNameList.push_back(fnd);
   */
   //end
   hFind = FindFirstFile(__TEXT("*.*"), &pDW->FindData);
   pDW->fOk = (hFind != INVALID_HANDLE_VALUE);

   while (pDW->fOk) {
      pDW->fIsDir = pDW->FindData.dwFileAttributes &
         FILE_ATTRIBUTE_DIRECTORY;
      if (!pDW->fIsDir ||
         (!pDW->fRecurse && IsChildDir(&pDW->FindData))) {

         _stprintf(pDW->szBuf,
            pDW->fIsDir ? __TEXT("%*s[%s]") : __TEXT("%*s%s"),
            pDW->nIndent, __TEXT(""),
            pDW->FindData.cFileName);

         //ListBox_AddString(pDW->hwndTreeLB, pDW->szBuf);
           _FileNameData fnd;
          memcpy(fnd.buf, pDW->szBuf, BUF_SIZE);
          fileNameList.push_back(fnd);
      }
      pDW->fOk = FindNextFile(hFind, &pDW->FindData);
   }
   if (hFind != INVALID_HANDLE_VALUE)
      FindClose(hFind);

   if (pDW->fRecurse) {
      // Get the first child directory.
      hFind = FindFirstChildDir(
         __TEXT("*.*"), &pDW->FindData);
      pDW->fOk = (hFind != INVALID_HANDLE_VALUE);
      while (pDW->fOk) {
         // Change into the child directory.
         if (SetCurrentDirectory(pDW->FindData.cFileName)) {

            // Perform the recursive walk into the child
            // directory.  Remember that some members of pDW 
            // will be overwritten by this call.
            DirWalkRecurse(pDW);

            // Change back to the child's parent directory.
            SetCurrentDirectory(__TEXT(".."));
         }

         pDW->fOk = FindNextChildDir(hFind, &pDW->FindData);
      }

      if (hFind != INVALID_HANDLE_VALUE)
         FindClose(hFind);
   }
   pDW->nDepth--;
}
Example #7
0
static void DirWalkRecurse(LPDIRWALKDATA pDW, LPCTSTR pszRootPath, LPCTSTR pszDesPath)	//pointer to DIRWALKDATA as parameter, so the param in the struct can be constantly updated such as nDepth
{
	HANDLE hFind;						//FindFirstFile()返回的句柄  作为FindNextFile()的参数,多次查找的关联

	LPTSTR src = (LPTSTR)malloc(100 * sizeof(LPTSTR));
	LPTSTR des = (LPTSTR)malloc(100 * sizeof(LPTSTR));

	pDW->nDepth++;						//recursive depth + 1
	pDW->nIndent = 3 * pDW->nDepth;		//控制缓冲区写入位置
	_stprintf_s(pDW->szBuf, _TEXT("%*s"), pDW->nIndent, _TEXT(""));

	GetCurrentDirectory(chDIMOF(pDW->szBuf) - pDW->nIndent, &pDW->szBuf[pDW->nIndent]);		//将当前目录存入缓冲区

	//printf("%s\n", pDW->szBuf);

	hFind = FindFirstFile(_TEXT("*.*"), &pDW->FindData);						//system API
	pDW->fOk = (hFind != INVALID_HANDLE_VALUE);

	while (pDW->fOk)		//当前目录搜索完毕则跳出
	{
		pDW->fIsDir = pDW->FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;	//是否是目录
		if (!pDW->fIsDir || (!pDW->fRecurse && IsChildDir(&pDW->FindData)))			//为文件非目录 || (非递归 && 为一个目录文件)
		{
			_stprintf_s(pDW->szBuf,
				pDW->fIsDir ? _TEXT("%*s[%s]") : _TEXT("%*s%s"),
				pDW->nIndent,
				_TEXT(""),
				pDW->FindData.cFileName);

			//封装路径
			lstrcpy(src, pszRootPath);
			lstrcat(src, "\\");
			lstrcat(src, pDW->FindData.cFileName);

			lstrcpy(des, pszDesPath);
			lstrcat(des, "\\");
			lstrcat(des, pDW->FindData.cFileName);

			//创建/打开文件
			HANDLE desFile = CreateFile(
				des,
				GENERIC_READ | GENERIC_WRITE,
				0,
				NULL,
				OPEN_ALWAYS,
				pDW->FindData.dwFileAttributes,
				NULL);
			HANDLE srcFile = CreateFile(
				src,
				GENERIC_READ | GENERIC_WRITE,
				0,
				NULL,
				OPEN_EXISTING,
				pDW->FindData.dwFileAttributes,
				NULL);

			DWORD filesize = GetFileSize(srcFile, NULL);
			char* buffer = new char[filesize + 1];
			DWORD readsize;

			ReadFile(srcFile, buffer, filesize, &readsize, NULL);
			WriteFile(desFile, buffer, filesize, &readsize, NULL);
			buffer[filesize] = 0;
			CloseHandle(srcFile);
			CloseHandle(desFile);

			//printf("%s\n", pDW->szBuf);
		}
		else {
			SECURITY_ATTRIBUTES attribute;
			attribute.nLength = sizeof(attribute);
			attribute.lpSecurityDescriptor = NULL;
			attribute.bInheritHandle = FALSE;
			SetCurrentDirectory(pszDesPath);
			CreateDirectory(pDW->FindData.cFileName, &attribute);
			SetCurrentDirectory(pszRootPath);
		}

		pDW->fOk = FindNextFile(hFind, &pDW->FindData);							//system API
	}

	if (hFind != INVALID_HANDLE_VALUE)
	{
		FindClose(hFind);	
	}

	if (pDW->fRecurse)
	{
		//Get the first child directory
		hFind = FindFirstChildDir(_TEXT("*.*"), &pDW->FindData);				//找到第一个子目录 FindFirstFile
		pDW->fOk = (hFind != INVALID_HANDLE_VALUE);
		
		while (pDW->fOk)
		{
			//change into the child directory
			if (SetCurrentDirectory(pDW->FindData.cFileName))
			{
				//封装路径
				lstrcpy(src, pszRootPath);
				lstrcat(src, "\\");
				lstrcat(src, pDW->FindData.cFileName);

				lstrcpy(des, pszDesPath);
				lstrcat(des, "\\");
				lstrcat(des, pDW->FindData.cFileName);
				DirWalkRecurse(pDW, src, des);

				//change back to the child's parent dir
				SetCurrentDirectory(_TEXT(".."));
			}

			pDW->fOk = FindNextChildDir(hFind, &pDW->FindData);					//将hFind句柄传入 FindNextFile
		}

		if (hFind != INVALID_HANDLE_VALUE)
		{
			FindClose(hFind);
		}
	}

	pDW->nDepth--;
}