- // 本程序演示系統(tǒng)環(huán)境信息的獲取,,環(huán)境變量的設(shè)置和獲取
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
- #define MAX_LENGTH 1024
- /*
- GetEnvironmentStrings()
-
- FreeEnvironmentStrings()
-
- GetEnvironmentVariable()
-
- setenvironmentv()
- */
-
- VOID EnvVariable(TCHAR * pszVar)
- {
- SetEnvironmentVariable(pszVar,TEXT("C:\\"));//參數(shù)一:The name of the environment variable.參數(shù)二:The contents of the environment variable.
- TCHAR szValue[MAX_LENGTH]={0};
- GetEnvironmentVariable(pszVar,szValue,MAX_LENGTH);
- _tprintf(TEXT("%s=%s"),pszVar,szValue);
-
- }
-
- VOID EnvString()
- {
- TCHAR * pszEnv=GetEnvironmentStrings();//取得系統(tǒng)環(huán)境信息,,返回一個(gè)字符串的串
- TCHAR * pszTmp=pszEnv;
- while (pszTmp[0]!=0)
- {
- _tprintf(TEXT("%s\n"),pszTmp);
- pszTmp=_tcslen(pszTmp)+1+pszTmp;
- }
- FreeEnvironmentStrings(pszEnv);//釋放環(huán)境信息
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"zhi");
- _tprintf(TEXT("取得的系統(tǒng)信息如下:\n"));
- EnvString();
- _tprintf(TEXT("-------------------------------------------\n"));
- EnvVariable(TEXT("MYPATH"));
- _tprintf(TEXT("\n-------------------------------------------\n"));
- return 0;
- }
- // 本程序用來(lái)演示基本的進(jìn)程信息和PSAPI這個(gè)WIN NT擴(kuò)展庫(kù)的使用
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
- #include "../psapi/PSAPI.H" //加載process status API庫(kù)的頭文件
- #pragma comment(lib,"../psapi/psapi.lib")//隱式加載process status API動(dòng)態(tài)庫(kù)
-
- VOID ProcModule()
- {
- //枚舉當(dāng)前進(jìn)程的所有的模塊
- _tprintf(TEXT("All Modules:\n"));
- HANDLE hProc=GetCurrentProcess();//取得當(dāng)前進(jìn)程的偽句柄。
-
- /*
- BOOL WINAPI EnumProcessModules(
- __in HANDLE hProcess,
- __out HMODULE *lphModule,
- __in DWORD cb,//The size of the lphModule array, in bytes.注意是字節(jié)數(shù)
- __out LPDWORD lpcbNeeded//The number of bytes required to store all module handles in the lphModule array.注意也是字節(jié)數(shù)
- );
- */
- HMODULE hModule[260]={0};//定義一個(gè)模塊數(shù)組,,作為EnumProcessModules()的出參,。
- DWORD nWriteCount=0;//返回寫(xiě)入的模塊的數(shù)量
- EnumProcessModules(hProc,hModule,260,&nWriteCount);//枚舉進(jìn)程的所有的模塊
- DWORD nSize=sizeof(HMODULE);//4個(gè)字節(jié)
- DWORD nCount=nWriteCount/nSize;//100字節(jié)/4字節(jié)
- for (int nIndex=0;nIndex<nCount;nIndex++)
- {
- TCHAR szPath[260]={0};
- GetModuleFileNameEx(hProc,hModule[nIndex],szPath,260);//通過(guò)模塊句柄返回模塊的名字
- _tprintf(TEXT("\t%d:%p=%s\n"),nIndex+1,hModule[nIndex],szPath);//0x 00 d0 00 00 4個(gè)字節(jié)
- }
-
- }
-
- VOID ProcInfo()
- {
- //獲取進(jìn)程的ID
- DWORD nID=GetCurrentProcessId();
- //獲取進(jìn)程的偽句柄
- HANDLE hProc=GetCurrentProcess();
- _tprintf(TEXT("\n\t當(dāng)前的進(jìn)程ID是:%d,進(jìn)程的偽句柄的數(shù)值是:%p\n"),nID,hProc);//hProc的十六進(jìn)制數(shù)值是FFFFFFFF十進(jìn)制數(shù)值是-1。
- hProc=OpenProcess(PROCESS_ALL_ACCESS,FALSE,nID);//通過(guò)指定的進(jìn)程ID獲取進(jìn)程的句柄,。
- _tprintf(TEXT("\t當(dāng)前進(jìn)程的真正的句柄是:%p\n\n"),hProc);
- }
-
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"zhi");
- ProcModule();
- ProcInfo();
- return 0;
- }
- //本程序用來(lái)演示進(jìn)程的創(chuàng)建,,銷(xiāo)毀,等待
-
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
-
- VOID CreateProcess()
- {
- /*
- BOOL WINAPI CreateProcess(
- __in_opt LPCTSTR lpApplicationName,
- __inout_opt LPTSTR lpCommandLine,
- __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
- __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
- __in BOOL bInheritHandles,
- __in DWORD dwCreationFlags,
- __in_opt LPVOID lpEnvironment,
- __in_opt LPCTSTR lpCurrentDirectory,
- __in LPSTARTUPINFO lpStartupInfo,
- __out LPPROCESS_INFORMATION lpProcessInformation
- );
- */
- STARTUPINFO start_info={0};
- start_info.cb=sizeof(STARTUPINFO);
- start_info.dwFlags= STARTF_USESIZE|STARTF_USESTDHANDLES;//如果想要使用下面的兩個(gè)參數(shù)調(diào)整窗口的大小,,那么一定要指定這個(gè)標(biāo)示
- start_info.dwXSize=300;//通過(guò)下面的兩個(gè)參數(shù)調(diào)整窗口的大小
- start_info.dwYSize=300;
-
- PROCESS_INFORMATION Process_info={0};
- //創(chuàng)建進(jìn)程,但是無(wú)法創(chuàng)建,,因?yàn)镈OS方式的控制臺(tái)的16位程序必須使用第二個(gè)參數(shù)所以這句執(zhí)行之后是看不到,ChildProc.exe程序運(yùn)行的
- //CreateProcess(TEXT("C:\\ChildProc.exe"),TEXT("\"Hello Child\""),NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&start_info,&Process_info);
- CreateProcess(NULL,TEXT("C:\\ChildProc.exe"),NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&start_info,&Process_info);
- //這個(gè)函數(shù)在VC++6.0 XP環(huán)境下運(yùn)行成功,,但是WIN7 VS2010不行
- //數(shù)據(jù)進(jìn)程的相關(guān)的信息
- _tprintf(TEXT("進(jìn)程的句柄是:%p\n"),Process_info.hProcess);
- _tprintf(TEXT("進(jìn)程的ID是:%d\n"),Process_info.dwProcessId);
- _tprintf(TEXT("線程的句柄是:%p\n"),Process_info.hThread);
- _tprintf(TEXT("線程的ID是:%d\n"),Process_info.dwThreadId);
- }
-
- VOID TerminateProc(DWORD nID)
- {
- //根據(jù)進(jìn)程的ID獲取進(jìn)程的句柄
- HANDLE hProc=OpenProcess(PROCESS_ALL_ACCESS,FALSE,nID);
- //終止進(jìn)程
- TerminateProcess(hProc,0);
- }
-
- VOID WaitProcess()
- {
- //創(chuàng)建進(jìn)程
- STARTUPINFO si = { 0 };
- PROCESS_INFORMATION pi = { 0 };
- si.cb = sizeof( si );//這個(gè)參數(shù)是必須進(jìn)行賦值的,,原因好像在windows核心編程中講過(guò),但是我忘記了
- CreateProcess( TEXT("C:\\MineSweeper.exe"),NULL, NULL, NULL, FALSE, 0,NULL, NULL, &si, &pi );
- _tprintf(TEXT("掃雷程序正在運(yùn)行,!\n"));
- WaitForSingleObject(pi.hProcess,9000000);
- _tprintf(TEXT("掃雷程序結(jié)束運(yùn)行,!\n"));
-
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"zhi");
- CreateProcess();
- //TerminateProc( 5368 );//結(jié)束一個(gè)進(jìn)程,,當(dāng)前任務(wù)管理器中的這個(gè)進(jìn)程是QQ
- //WaitProcess();
- return 0;
- }
下面的兩個(gè)程序功能是一樣的,代碼基本上是一樣的,。都是創(chuàng)建一個(gè)作業(yè)然后在作業(yè)中添加了兩個(gè)進(jìn)程,,我們對(duì)作業(yè)進(jìn)行了功能的限制,限制在作業(yè)中的進(jìn)程都不能使用剪切板的功能,,但是我們驚奇的發(fā)現(xiàn)同樣的程序在 WIN7 操作系統(tǒng)上,,使用VS2010 和 在XP操作系統(tǒng)上使用 VC++6.0 代碼運(yùn)行之后出現(xiàn)的效果是完全不同的。
我們?cè)赩C++6.0上程序運(yùn)行的時(shí)候,,完全能夠?qū)崿F(xiàn)限制剪切板的操作,。也就是剪切板不能使用,進(jìn)行復(fù)制和黏貼的操作,。但是同樣的程序在VS2010下執(zhí)行的時(shí)候,,剪切板的功能竟然能夠使用!?。,。。,?!具體的原因到現(xiàn)在還有找到。,。,。。,。找到之后再記錄下來(lái),。
- //本程序用來(lái)演示作業(yè)的使用 在WIN7系統(tǒng)和VS2010編譯環(huán)境下運(yùn)行。無(wú)法實(shí)現(xiàn)限制剪切板的功能
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
-
- HANDLE Create(TCHAR * szPath)//根據(jù)指定的引用程序名字創(chuàng)建一個(gè)進(jìn)程,,然會(huì)返回進(jìn)程的句柄
- {
- STARTUPINFO si={0};
- PROCESS_INFORMATION pi={0};
- si.cb=sizeof(STARTUPINFO);
- CreateProcess(szPath,NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
- return pi.hProcess;
- }
-
- VOID Job()
- {
- //創(chuàng)建Job對(duì)象
- HANDLE hJob = CreateJobObject( NULL,TEXT( "MyJob") );
- //設(shè)置權(quán)限
- JOBOBJECT_BASIC_UI_RESTRICTIONS ui = {0};
- ui.UIRestrictionsClass = JOB_OBJECT_UILIMIT_READCLIPBOARD|JOB_OBJECT_UILIMIT_WRITECLIPBOARD;
- SetInformationJobObject( hJob,JobObjectBasicUIRestrictions,&ui,sizeof(ui));
- //創(chuàng)建進(jìn)程
- HANDLE hProc =Create(TEXT("c:\\mspaint.exe" ));
- //將進(jìn)程加入作業(yè)
- AssignProcessToJobObject(hJob, hProc);
-
- hProc=Create(TEXT("c:\\mspaint.exe"));
- AssignProcessToJobObject(hJob, hProc );
- getchar( );
- //結(jié)束作業(yè)
- TerminateJobObject( hJob, 0 );
- //關(guān)閉Job
- CloseHandle( hJob );
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- Job();
- return 0;
- }
- // 本程序在XP操作系統(tǒng)上,,使用VC++6.0環(huán)境,我們可以看到程序能實(shí)現(xiàn)限制畫(huà)圖程序的剪切板的使用的功能
- //本程序運(yùn)行的時(shí)候需要在stdafx.cpp文件中添加宏定義:#define _WIN32_WINNT 0X0500
- //本程序用來(lái)演示作業(yè)的使用
-
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
-
-
- HANDLE Create(TCHAR * szPath)//根據(jù)指定的引用程序名字創(chuàng)建一個(gè)進(jìn)程,,然會(huì)返回進(jìn)程的句柄
- {
- STARTUPINFO si={0};
- PROCESS_INFORMATION pi={0};
- si.cb=sizeof(STARTUPINFO);
- CreateProcess(szPath,NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi);
- return pi.hProcess;
- }
-
- VOID Job()
- {
- //創(chuàng)建Job對(duì)象
- HANDLE hJob = CreateJobObject( NULL,TEXT( "MyJob") );
- //設(shè)置權(quán)限
- JOBOBJECT_BASIC_UI_RESTRICTIONS ui = {0};
- ui.UIRestrictionsClass = JOB_OBJECT_UILIMIT_READCLIPBOARD|JOB_OBJECT_UILIMIT_WRITECLIPBOARD;
- SetInformationJobObject( hJob,JobObjectBasicUIRestrictions,&ui,sizeof(ui));
- //創(chuàng)建進(jìn)程
- HANDLE hProc =Create(TEXT("C:\\mspaint.exe" ));
- //將進(jìn)程加入作業(yè)
- AssignProcessToJobObject(hJob, hProc);
-
- hProc=Create(TEXT("C:\\mspaint.exe"));
- AssignProcessToJobObject(hJob, hProc );
- getchar( );
- //結(jié)束作業(yè)
- TerminateJobObject( hJob, 0 );
- //關(guān)閉Job
- CloseHandle( hJob );
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
-
- Job();
- return 0;
- }
- //本程序用來(lái)演示進(jìn)程的創(chuàng)建,,銷(xiāo)毀,等待
-
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
-
- VOID CreateProcess()
- {
- /*
- BOOL WINAPI CreateProcess(
- __in_opt LPCTSTR lpApplicationName,
- __inout_opt LPTSTR lpCommandLine,
- __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes,
- __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
- __in BOOL bInheritHandles,
- __in DWORD dwCreationFlags,
- __in_opt LPVOID lpEnvironment,
- __in_opt LPCTSTR lpCurrentDirectory,
- __in LPSTARTUPINFO lpStartupInfo,
- __out LPPROCESS_INFORMATION lpProcessInformation
- );
- */
- STARTUPINFO start_info={0};
- start_info.cb=sizeof(STARTUPINFO);
- start_info.dwFlags= STARTF_USESIZE|STARTF_USESTDHANDLES;//如果想要使用下面的兩個(gè)參數(shù)調(diào)整窗口的大小,,那么一定要指定這個(gè)標(biāo)示
- start_info.dwXSize=300;//通過(guò)下面的兩個(gè)參數(shù)調(diào)整窗口的大小
- start_info.dwYSize=300;
-
- PROCESS_INFORMATION Process_info={0};
- //創(chuàng)建進(jìn)程,但是無(wú)法創(chuàng)建,,因?yàn)镈OS方式的控制臺(tái)的16位程序必須使用第二個(gè)參數(shù)所以這句執(zhí)行之后是看不到,ChildProc.exe程序運(yùn)行的
- //CreateProcess(TEXT("C:\\ChildProc.exe"),TEXT("\"Hello Child\""),NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&start_info,&Process_info);
- CreateProcess(NULL,TEXT("C:\\ChildProc.exe"),NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&start_info,&Process_info);
- //這個(gè)函數(shù)在VC++6.0 XP環(huán)境下運(yùn)行成功,,但是WIN7 VS2010不行
- //數(shù)據(jù)進(jìn)程的相關(guān)的信息
- _tprintf(TEXT("進(jìn)程的句柄是:%p\n"),Process_info.hProcess);
- _tprintf(TEXT("進(jìn)程的ID是:%d\n"),Process_info.dwProcessId);
- _tprintf(TEXT("線程的句柄是:%p\n"),Process_info.hThread);
- _tprintf(TEXT("線程的ID是:%d\n"),Process_info.dwThreadId);
- }
-
- VOID TerminateProc(DWORD nID)
- {
- //根據(jù)進(jìn)程的ID獲取進(jìn)程的句柄
- HANDLE hProc=OpenProcess(PROCESS_ALL_ACCESS,FALSE,nID);
- //終止進(jìn)程
- TerminateProcess(hProc,0);
- }
-
- VOID WaitProcess()
- {
- //創(chuàng)建進(jìn)程
- STARTUPINFO si = { 0 };
- PROCESS_INFORMATION pi = { 0 };
- si.cb = sizeof( si );//這個(gè)參數(shù)是必須進(jìn)行賦值的,,原因好像在windows核心編程中講過(guò),但是我忘記了
- CreateProcess( TEXT("C:\\winmine.exe"),NULL, NULL, NULL, FALSE, 0,NULL, NULL, &si, &pi );
- _tprintf(TEXT("掃雷程序正在運(yùn)行!\n"));
- WaitForSingleObject(pi.hProcess,INFINITE);
- _tprintf(TEXT("掃雷程序結(jié)束運(yùn)行,!\n"));
-
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"zhi");
- // CreateProcess();
- //TerminateProc( 5368 );//結(jié)束一個(gè)進(jìn)程,,當(dāng)前任務(wù)管理器中的這個(gè)進(jìn)程是QQ
- WaitProcess();
- return 0;
- }
- // 本程序用來(lái)演示線程的創(chuàng)建和使用
- //
-
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
-
- DWORD WINAPI ThreadProc1(LPVOID lpParameter)
- {
- DWORD nValue=(DWORD)lpParameter;//把傳遞進(jìn)來(lái)的數(shù)值強(qiáng)制類(lèi)型轉(zhuǎn)換成為一個(gè)DWORD類(lèi)型的數(shù)值
- for (int nIndex=0;nIndex<10;nIndex++)
- {
- _tprintf(TEXT("線程一 --------------%d\n"),nValue);
- Sleep(1000);
- }
- return 0;
- }
-
- DWORD WINAPI ThreadProc2(LPVOID lpParameter)
- {
- while(1)
- {
- _tprintf(TEXT("----------------------線程二\n"));
- Sleep(1000);
- }
- return 0;
- }
-
- VOID Create()
- {
- DWORD nValue=100;
- DWORD nThreadId=0;
- //創(chuàng)建一個(gè)掛起的線程
- HANDLE hThread=CreateThread(NULL,0,ThreadProc1,(LPVOID)nValue,CREATE_SUSPENDED,&nThreadId);
- _tprintf(TEXT("線程1的線程ID:%d\n"),nThreadId);
- _tprintf(TEXT("線程1的線程句柄是:%p\n"),hThread);
- ResumeThread(hThread);//恢復(fù)線程使線程開(kāi)始運(yùn)行。
- WaitForSingleObject(hThread,INFINITE);//等候線程1的運(yùn)行的結(jié)束
- CloseHandle(hThread);
-
- hThread=CreateThread(NULL,0,ThreadProc2,NULL,0,&nThreadId);
- _tprintf(TEXT("線程2的線程ID:%d\n"),nThreadId);
- _tprintf(TEXT("線程2的線程句柄是:%p\n"),hThread);
- //掛起線程
- //SuspendThread(hThread);
- //WaitForSingleObject(hThread,INFINITE);//沒(méi)有這條語(yǔ)句,,我們會(huì)看不到線程2的執(zhí)行,,會(huì)值打印出線程2的ID就結(jié)束了。
- CloseHandle(hThread);
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"zhi");
- Create();
- return 0;
- }
- <pre name="code" class="cpp">// 線程局部存儲(chǔ)TLS
- //
- #include "stdafx.h"
- #include "windows.h"
- #include "tchar.h"
- #include "locale.h"
-
- TCHAR * g_pszText1=NULL;
-
- __declspec(thread) TCHAR * g_pszText2=NULL;
-
- VOID Print( )
- {
- _tprintf(TEXT("Text1:%s\n"),g_pszText1);
- //_tprintf(TEXT("Text2:%s\n"),g_pszText2);
- }
-
-
- DWORD WINAPI PrintProc( LPVOID lpParameter)
- {
- TCHAR * pszText=(TCHAR *) lpParameter;
- g_pszText1=(TCHAR *)malloc(100);
- memset(g_pszText1,0,100);
- _tcscpy(g_pszText1,pszText);
-
- g_pszText2=(TCHAR *)malloc(100);
- memset(g_pszText2,0,100);
- _tcscpy(g_pszText2,pszText);
-
- while(1)
- {
- Print();
- Sleep(1000);
- }
- return 0;
- }
-
- VOID Create()
- {
- DWORD dwThread=0;
-
- TCHAR szText1[]=TEXT("線程1-------------------");
- HANDLE hThread=CreateThread(NULL,0,PrintProc,szText1,0,&dwThread);
-
- TCHAR szText2[]=TEXT("-----------線程2--------");
- hThread=CreateThread(NULL,0,PrintProc,szText2,0,&dwThread);
-
- TCHAR szText3[]=TEXT("-------------------線程3");
- hThread=CreateThread(NULL,0,PrintProc,szText3,0,&dwThread);
- getchar();//這個(gè)getchar()必須放在Create()函數(shù)中,,而不能放在main()函數(shù)中,。否則會(huì)出現(xiàn)亂碼因?yàn)閟zText[]都是局部變量。
- }
-
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- setlocale(LC_ALL,"chs");
- Create();
- return 0;
- }
|