1. 使用

例子:

使用ie打开指定的网页。

注意第二个参数是 可执行文件+命令行参数

 


[cpp] 
1. #include "stdafx.h" 
2. #include <windows.h>  
3. #include <stdio.h>  
4.   
5. int main(int argc, char* argv[])   
6. {   
7. sizeof(si) };   
8.     PROCESS_INFORMATION pi;   
9.   
10.     si.dwFlags = STARTF_USESHOWWINDOW;   
11. //TRUE表示显示创建的进程的窗口
12. TCHAR cmdline[] =TEXT("c://program files//internet explorer//iexplore.exe ");  
13. BOOL
14.         NULL,  
15. //在Unicode版本中此参数不能为常量字符串,因为此参数会被修改  
16.         NULL,   
17.         NULL,   
18.         FALSE,   
19.         CREATE_NEW_CONSOLE,   
20.         NULL,   
21.         NULL,   
22.         &si,   
23.         &pi);   
24.   
25. int
26. if(bRet)   
27.     {   
28.         ::CloseHandle (pi.hThread);   
29.         ::CloseHandle (pi.hProcess);   
30.   
31. " 新进程的进程ID号:%d /n", pi.dwProcessId);   
32. " 新进程的主线程ID号:%d /n", pi.dwThreadId);   
33.     }   
34. else
35.     {  
36. "error code:%d/n",error );  
37.     }  
38. return
39. }

 


#include "stdafx.h"
#include <windows.h> 
#include <stdio.h> 

int main(int argc, char* argv[]) 
{ 
	STARTUPINFO si = { sizeof(si) }; 
	PROCESS_INFORMATION pi; 

	si.dwFlags = STARTF_USESHOWWINDOW; 
	si.wShowWindow = TRUE; //TRUE表示显示创建的进程的窗口
	TCHAR cmdline[] =TEXT("c://program files//internet explorer//iexplore.exe ");
	BOOL bRet = ::CreateProcess ( 
		NULL,
		cmdline, //在Unicode版本中此参数不能为常量字符串,因为此参数会被修改	 
		NULL, 
		NULL, 
		FALSE, 
		CREATE_NEW_CONSOLE, 
		NULL, 
		NULL, 
		&si, 
		&pi); 

	int error = GetLastError();
	if(bRet) 
	{ 
		::CloseHandle (pi.hThread); 
		::CloseHandle (pi.hProcess); 

		printf(" 新进程的进程ID号:%d /n", pi.dwProcessId); 
		printf(" 新进程的主线程ID号:%d /n", pi.dwThreadId); 
	} 
	else
	{
		printf("error code:%d/n",error );
	}
	return 0; 
}

也可如此:

第一个参数是 可执行文件;第二个参数是 命令行参数

 

 


[cpp] 
1. #include "stdafx.h" 
2. #include <windows.h>  
3. #include <stdio.h>  
4.   
5. int main(int argc, char* argv[])   
6. {   
7. sizeof(si) };   
8.     PROCESS_INFORMATION pi;   
9.   
10.     si.dwFlags = STARTF_USESHOWWINDOW;   
11. //TRUE表示显示创建的进程的窗口
12. TCHAR cmdline[] =TEXT(" "); //注意前面有空格,否则打开的是主页。  
13. BOOL
14. "c://program files//internet explorer//iexplore.exe"),  
15. //在Unicode版本中此参数不能为常量字符串,因为此参数会被修改  
16.         NULL,   
17.         NULL,   
18.         FALSE,   
19.         CREATE_NEW_CONSOLE,   
20.         NULL,   
21.         NULL,   
22.         &si,   
23.         &pi);   
24.   
25. int
26. if(bRet)   
27.     {   
28.         ::CloseHandle (pi.hThread);   
29.         ::CloseHandle (pi.hProcess);   
30.   
31. " 新进程的进程ID号:%d /n", pi.dwProcessId);   
32. " 新进程的主线程ID号:%d /n", pi.dwThreadId);   
33.     }   
34. else
35.     {  
36. "error code:%d/n",error );  
37.     }  
38.   
39.   
40. return
41. }


2. 检查是否退出或存在

a:

WaitForSingleObject( 你的进程句柄, INFINITE ); 这种方式似乎存在问题,建议用其他。

查看MSDN里关于WaitForSingleObject的帮助就知道,它可以用来等待一个进程的结束,WaitForSingleObject执行时,就会等在那里,但不占用CPU时间,一旦正等待的进程退出,WaitForSingleObject才返回,上面的INFINITE是无限等待下去的意思。  


b:

还有两个办法为:

::GetExitCodeProcess(pi.hProcess, &dwMark);
             if (dwMark == STILL_ACTIVE )
             { 
                 Sleep( 3 );
                 printf("active...\n");
             }
             else
             {
                 break;
             }

  这种方式似乎存在问题,建议用其他。

下面也是一个办法:

C/C++ Code Example: Retrieving Task Exit Code
 This example retrieves the last exit code returned by a known task. (A returned value of "0" indicates the task was never run.) The example assumes that the task, Test Task, already exists on the local computer.

 #include <windows.h>
 #include <initguid.h>
 #include <ole2.h>
 #include <mstask.h>
 #include <msterr.h>
 #include <wchar.h>

 int main(int argc, char **argv)
 {
   HRESULT hr = ERROR_SUCCESS;
   
   
   ///
   // Call CoInitialize to initialize the COM library and then
   // CoCreateInstance to get the Task Scheduler object.
   ///
   ITaskScheduler *pITS;
   hr = CoInitialize(NULL);
   if (SUCCEEDED(hr))
   {
     hr = CoCreateInstance(CLSID_CTaskScheduler,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_ITaskScheduler,
                           (void **) &pITS);
     if (FAILED(hr))
     {
       CoUninitialize();
       return 1;
     }
   }
   else
   {
      return 1;
   }
   
   
   ///
   // Call ITaskScheduler::Activate to get the Task object.
   ///
   ITask *pITask;
   LPCWSTR lpcwszTaskName;
   lpcwszTaskName = L"TestTask";
   hr = pITS->Activate(lpcwszTaskName,
                       IID_ITask,
                       (IUnknown**) &pITask);
   
   // Release ITaskScheduler interface.
   pITS->Release();
   
   if (FAILED(hr))
   {
     wprintf(L"Failed calling ITaskScheduler::Activate: ");
     wprintf(L"error = 0x%x\n",hr);
     CoUninitialize();
     return 1;
   }
   
   
   ///
   // Call ITask::GetExitCode. Note that this method is 
   // inherited from IScheduledWorkItem.
   ///
   DWORD pdwExitCode;
   
   hr = pITask->GetExitCode(&pdwExitCode);
   
   // Release ITask interface.
   pITask->Release();
   
   if (FAILED(hr))
   {
     wprintf(L"Failed calling ITask::GetExitCode: ");
     wprintf(L"error = 0x%x\n",hr);
     CoUninitialize();
     return 1;
   }
   
   
   wprintf(L"The last exit code of Test Task is: %d\n", pdwExitCode);
   
   
   CoUninitialize();
   return 0;
 }
c:
 BOOL IsExeFileRun(CString ExeFileName)
 {
 
 HANDLE hProcessSnap = NULL;
 PROCESSENTRY32 pe32= {0};
  
 hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
 if (hProcessSnap == (HANDLE)-1)
 {
 return FALSE;
 }
 pe32.dwSize = sizeof(PROCESSENTRY32);
 if (Process32First(hProcessSnap, &pe32))
 {
 do
 {   
 if (ExeFileName.Compare(_T(pe32.szExeFile)) == 0)
 {
 return TRUE;
 }
 }while (Process32Next(hProcessSnap, &pe32));
 }
 
 CloseHandle (hProcessSnap);
 
 return FALSE;
 }

 

方法的参数是要判断的进程的名字,如notepad.exe


C++例子


#include "StdAfx.h" 

 

  #include "windows.h" 

 

  #include "tlhelp32.h" 

 

  #include "stdio.h" 

 

  int main(int argc, char* argv[]) 

 

  { 

 
 
 PROCESSENTRY32 pe32; 

 

  //在使用这个结构前,先设置它的大小 

 

  pe32.dwSize = sizeof(pe32); 

 

  //给系统内所有的进程拍个快照 

 

  HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); 

 

  if (hProcessSnap == INVALID_HANDLE_VALUE) 

 

  { 

 

  printf("CreateToolhelp32Snapshot 调用失败.\n"); 

 

  return -1; 

 

  } 

 

  //遍历进程快照,轮流显示每个进程的信息 

 

  BOOL bMore = ::Process32First(hProcessSnap,&pe32); 

 

  while (bMore) 

 

  { 

 

  printf("进程名称:%s\n",pe32.szExeFile); 

 

  printf("进程ID:%u\n\n",pe32.th32ProcessID); 

 

  bMore = ::Process32Next(hProcessSnap,&pe32); 

 

  } 

 

  //不要忘记清除掉snapshot对象 
  

 
 
 
 ::CloseHandle(hProcessSnap); 

 
 

  return 0; 

 

  }


3 杀进程

1. #include <stdio.h>  
2. #include <windows.h>  
3. #include <winbase.h>  
4. #include <tlhelp32.h>  
5. #pragma comment(lib,"kernel32.lib")  
6. #pragma comment(lib,"advapi32.lib")  
7. void EnableDebugPriv() {  
8. HANDLE hToken;  
9.     TOKEN_PRIVILEGES tkp;  
10.     OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);  
11.     LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);  
12.     tkp.PrivilegeCount = 1;  
13.     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
14.     AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL);  
15.     CloseHandle(hToken);  
16. }  
17. int pskill(int id)   //根据进程ID杀进程  
18. {  
19. HANDLE hProcess=NULL;  
20. //打开目标进程  
21.     hProcess=OpenProcess(PROCESS_TERMINATE,FALSE,id);  
22. if (hProcess==NULL) {  
23. "\nOpen Process fAiled:%d\n",GetLastError());  
24. return -1;  
25.     }  
26. //结束目标进程  
27. DWORD ret=TerminateProcess(hProcess,0);  
28. if(ret==0) {  
29. "%d",GetLastError());  
30.     }  
31. return -1;  
32. }  
33. int main() {  
34. //进程列举  
35. HANDLE hSnApshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);  
36. if (hSnApshot != INVALID_HANDLE_VALUE) {  
37. sizeof(te) };  
38. BOOL f0k = Process32First(hSnApshot, &te);  
39. for (; f0k; f0k = Process32Next(hSnApshot, &te)) {  
40. "Pid: %d %s\n", te.th32ProcessID, te.szExeFile);  
41.         }  
42.     }  
43.     CloseHandle(hSnApshot);  
44. //杀进程  
45. "the process's id which you want to kill:");  
46. int id = 0;  
47. "%d", &id);  
48. //提升权限  
49.     pskill(id);  
50. return 0;  
51. }


Windows快速创建大文件的三种方法


今天花了半个小时研究了一下创建大文件的方法:

一、使用内存映射文件:
CreateFileMapping的参数可以设置最大长度,当实际文件的长度小于设置的最大长度时,系统自动扩展。注意:在CreateFile的时候要加写权限。

HANDLE hFile; 
 
     HANDLE hMapFile; 
 

     hFile = CreateFile( 
 
         "D:\\LargeFile.dat", 
 
         GENERIC_WRITE | GENERIC_READ, 
 
         FILE_SHARE_READ, 
 
         NULL, 
 
         CREATE_ALWAYS, 
 
         FILE_ATTRIBUTE_NORMAL, 
 
         NULL 
 
         ); 
 
     if( hFile == INVALID_HANDLE_VALUE ) 
 
     { 
 
         printf( "create file failed.\n" ); 
 
         return; 
 
     } 
 

     hMapFile = CreateFileMapping( 
 
         hFile, 
 
         NULL, 
 
         PAGE_READWRITE, 
 
         0, 
 
         2*1024*1024, // 2 MByte 
 
         NULL 
 
         ); 
 
     if( hMapFile == NULL ) 
 
     { 
 
         printf( "create file mapping failed.\n" ); 
 
         CloseHandle( hFile ); 
 
         return;    
 
     } 
 

     printf( "OK!\n" ); 
 

     CloseHandle( hMapFile ); 
 
     CloseHandle( hFile );



二、设置文件指针 + 设置EOF
两个API完成,首先调用SetFilePointer,在参数里填入所需的长度,再调用SetEndOfFile,完成。

HANDLE hFile; 
 
     hFile = CreateFile( 
 
         "D:\\LargeFile1.dat", 
 
         GENERIC_WRITE | GENERIC_READ, 
 
         FILE_SHARE_READ, 
 
         NULL, 
 
         CREATE_ALWAYS, 
 
         FILE_ATTRIBUTE_NORMAL, 
 
         NULL 
 
         ); 
 
     if( hFile == INVALID_HANDLE_VALUE ) 
 
     { 
 
         printf( "create file failed.\n" ); 
 
         return; 
 
     } 
 

     SetFilePointer( 
 
         hFile, 
 
         2*1024*1024, 
 
         NULL, 
 
         FILE_BEGIN 
 
         ); 
 

     SetEndOfFile( hFile ); 
 

     printf( "OK!\n" ); 
 
     CloseHandle( hFile );



三、设置文件指针 + 写1Byte
也是两个API完成,首先调用SetFilePointer,再调用WriteFile写入1byte就完成了。

HANDLE hFile; 
 
     DWORD dwR; 
 
     hFile = CreateFile( 
 
         "D:\\LargeFile2.dat", 
 
         GENERIC_WRITE | GENERIC_READ, 
 
         FILE_SHARE_READ, 
 
         NULL, 
 
         CREATE_ALWAYS, 
 
         FILE_ATTRIBUTE_NORMAL, 
 
         NULL 
 
         ); 
 
     if( hFile == INVALID_HANDLE_VALUE ) 
 
     { 
 
         printf( "create file failed.\n" ); 
 
         return; 
 
     } 
 

     SetFilePointer( 
 
         hFile, 
 
         2*1024*1024 - 1, 
 
         NULL, 
 
         FILE_BEGIN 
 
         ); 
 

     WriteFile( 
 
         hFile, 
 
         "A", 
 
         1, 
 
         &dwR, 
 
         NULL 
 
         ); 
 

     printf( "OK!\n" ); 
 
     CloseHandle( hFile );




    写完收工,顺便说一下,第三种方法是FlashGet使用的方法。第一种方法只发一个IRP,速度最快,第二种要发两个IRP,第三种也是发两个IRP,不过还有一个写动作,速度略慢。