文章目录
进程
windows下运行的进程分为两个部分:
- 内核对象,操作系统用它来管理进程.
- 地址空间,包含可执行文件或者dll模块的代码和数据,还包含动态内存分配.
//GetModuleHandle()返回主线程的基地址
#include "stdafx.h"
#include<iostream>
#include<windows.h>
using std::cout;
using std::endl;
using std::cin;
void usage()
{
HMODULE hModule = GetModuleHandle(NULL);
cout << "with GetModuleHandle(NULL)=0x" << std::oct<< hModule << endl;
hModule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,(PCTSTR)usage,&hModule);
cout << "with GetModuleHandleEx=0x" << std::oct << hModule << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
usage();
return 0;
}
//result:
//with GetModuleHandle(NULL)=0x01230000
//with GetModuleHandleEx=0x01230000
//CommandLineToArgvW()返回一个字符串的指针数组的地址,很多运用不会释放这块内存
#include "stdafx.h"
#include<iostream>
#include<windows.h>
using std::cout;
using std::endl;
using std::cin;
void usage()
{
int nNumberArgs;
PWSTR *ppArgv=CommandLineToArgvW(GetCommandLineW(), &nNumberArgs);//参数:命令行的缓冲区的地址,命令行参数的个数
if (ppArgv[1] == L"x"){
cout << "hello" << endl; }
HeapFree(GetProcessHeap, 0, ppArgv);//释放内存块
}
int _tmain(int argc, _TCHAR* argv[])
{
usage();
return 0;
}
进程的环境变量
访问进程块的方式
输出全部的环境变量
//GetEnvironmentStrings()获取环境不按量的地址快
#include "stdafx.h"
#include<strsafe.h>
#include<windows.h>
#include<iostream>
using std::wcout;
void DumpEnvStrings() {
//1.换的环境变量块
PTSTR pEnvBlock = GetEnvironmentStrings();
TCHAR szName[MAX_PATH];
TCHAR szValue[MAX_PATH];
PTSTR pszCurrent = pEnvBlock;
HRESULT hr = S_OK;
PCTSTR pszPos = NULL;
int current = 0;
//2.输出环境变量
while (pszCurrent != NULL) {
if (*pszCurrent != TEXT('=')) {
//存在 =::=::\这类的环境变量
pszPos = _tcschr(pszCurrent, TEXT('='));
pszPos++;
size_t cbNameLength = (size_t)pszPos - (size_t)pszCurrent - sizeof(TCHAR);
hr = StringCbCopyN(szName, MAX_PATH, pszCurrent, cbNameLength);
if (FAILED(hr)) {
break;
}
hr = StringCchCopyN(szValue, MAX_PATH, pszPos, _tcslen(pszPos) + 1);
if (SUCCEEDED(hr)) {
_tprintf(TEXT("[%u] %s=%s\r\n"), current, szName, szValue);
}
else // something wrong happened; check for truncation.
if (hr == STRSAFE_E_INSUFFICIENT_BUFFER) {
_tprintf(TEXT("[%u] %s=%s...\r\n"), current, szName, szValue);
}
else {
// This should never occur.
_tprintf(
TEXT("[%u] %s=???\r\n"), current, szName
);
break;
}
}
else {
_tprintf(TEXT("[%u] %s\r\n"), current, pszCurrent);
}
current++;
while (*pszCurrent != TEXT('\0'))
pszCurrent++;
pszCurrent++;
if (*pszCurrent == TEXT('\0'))
break;
};
//3.释放环境变量块
FreeEnvironmentStrings(pEnvBlock);
}
int _tmain(int argc, _TCHAR* argv[])
{
DumpEnvStrings();
return 0;
}
获取特定的环境变量
//GetEnvironmentVariable(PCTSTR pszVariableName,PTSTR pszValue,0)
//获取环境变量的值
#include "stdafx.h"
#include<strsafe.h>
#include<windows.h>
#include<iostream>
using std::wcout;
void PrintEnvironmentVariable(PCTSTR pszVariableName) {
PTSTR pszValue = NULL; // Get the size of the buffer that is required to store the value
DWORD dwResult = GetEnvironmentVariable(pszVariableName, pszValue, 0);
if(dwResult != 0)
{
// Allocate the buffer to store the environment variable value
DWORD size = dwResult * sizeof(TCHAR);
pszValue = (PTSTR)malloc(size);
GetEnvironmentVariable(pszVariableName, pszValue, size);
_tprintf(TEXT("%s=%s\n"), pszVariableName, pszValue); free(pszValue);
}
else
{
_tprintf(TEXT("'%s'=<unknown value>\n"), pszVariableName);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
PrintEnvironmentVariable(L"USERPROFILE");
return 0;
}
特定的字符,比如%USER%的查看
/*
DWORD ExpandEnvironmentStrings( PTCSTR pszSrc, PTSTR pszDst, DWORD chSize); 字符串的替代,好比%USER%\Document
==c:/xx/Document
*/
#include "stdafx.h"
#include<strsafe.h>
#include<windows.h>
#include<iostream>
using std::wcout;
void usage()
{
DWORD chValue =
ExpandEnvironmentStrings(TEXT("PATH='%PATH%'"), NULL, 0);
PTSTR pszBuffer = new TCHAR[chValue];
chValue = ExpandEnvironmentStrings(TEXT("PATH='%PATH%'"), pszBuffer, chValue);
_tprintf(TEXT("%s\r\n"), pszBuffer);
delete[] pszBuffer;
}
int _tmain(int argc, _TCHAR* argv[])
{
usage();
return 0;
}
设置环境变量
/*
BOOL SetEnvironmentVariable( PCTSTR pszName, PCTSTR pszValue);设置环境变量,如果""就会删除环境变量
*/
#include "stdafx.h"
#include<strsafe.h>
#include<windows.h>
#include<iostream>
using std::wcout;
void PrintEnvironmentVariable(PCTSTR pszVariableName) {
PTSTR pszValue = NULL; // Get the size of the buffer that is required to store the value
DWORD dwResult = GetEnvironmentVariable(pszVariableName, pszValue, 0);
if (dwResult != 0)
{
// Allocate the buffer to store the environment variable value
DWORD size = dwResult * sizeof(TCHAR);
pszValue = (PTSTR)malloc(size);
GetEnvironmentVariable(pszVariableName, pszValue, size);
_tprintf(TEXT("%s=%s\n"), pszVariableName, pszValue); free(pszValue);
}
else
{
_tprintf(TEXT("'%s'=<unknown value>\n"), pszVariableName);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
if (SetEnvironmentVariable(L"name", L"zylg"))
{
std::cout << "环境变量设置成功\n";
}
PrintEnvironmentVariable(L"name");
return 0;
}
进程目录及系统版本
进程的错误模式
UINT SetErrorMode(UINT fuErrorMode);
SEM_FAILCRITICALERRORS
SEM_NOGPFAULTERRORBOX
SEM_NOOPENFILEERRORBOX
SEM_NOALIGNMENTFAULTEXCEPT
进程所在的驱动器和目录
DWORD GetCurrentDirectory( DWORD cchCurDir, PTSTR pszCurDir);
BOOL SetCurrentDirectory(PCTSTR pszCurDir);
/*
BOOL GetCurrentDirectory(len,buf)//获取进程的当前的目录,参数1是字符串的长度,参数2,存取字符串的变量
BOOL SetCurrentDirectory(L"E:/");//是指把进程的当前路径换成:E:/
*/
#include "stdafx.h"
#include<strsafe.h>
#include<windows.h>
#include<iostream>
using std::wcout;
#define BUFSIZE MAX_PATH
void usage()
{
TCHAR Buffer[BUFSIZE];
DWORD dwRet;
dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
if (dwRet == 0)
{
_tprintf(L"GetCurrentDirectory failed (%d)", GetLastError());
return ;
}
else if (dwRet > BUFSIZE) //如果超过了最大字符数,则提示越界,MAX_PATH 是260
{
_tprintf(L"GetCurrentDirectory failed (buffer too small,need %d chars)\n", dwRet);
return ;
}
else
{
_tprintf(L"current directory is :%s\r\n", Buffer);
}
SetCurrentDirectory(L"E:/");//是指把进程的当前路径换成:E:/
dwRet = GetCurrentDirectory(BUFSIZE, Buffer);
_tprintf(L"current directory is :%s\r\n", Buffer);
}
int _tmain(int argc, _TCHAR* argv[])
{
usage();
return 0;
}
/*
GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);获取c驱动器的当前目录
*/
#include "stdafx.h"
#include<windows.h>
#include<iostream>
using std::wcout;
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szCurDir[MAX_PATH];
DWORD cchLength = GetFullPathName(TEXT("C:"), MAX_PATH, szCurDir, NULL);
_tprintf(L"current directory is :%s\r\n", szCurDir);
return 0;