SetErrorMode
2016-07-11 11:27
573 查看
1. SetErrorMode:
原型:
[cpp] view
plain copy
UINT WINAPI SetErrorMode( _In_ UINT uMode);
功能:
控制指定类型的严重错误是由windows处理还是由应用程序处理。
参数:
uMode:
返回值:错误模式的先前的状态
备注:默认情况下,子进程继承父进程的错误模式标志
2. HeapSetInformation:
原型:
[cpp] view
plain copy
BOOL WINAPI HeapSetInformation(
_In_opt_ HANDLE HeapHandle,
_In_ HEAP_INFORMATION_CLASS HeapInformationClass,
_In_ PVOID HeapInformation,
_In_ SIZE_T HeapInformationLength
);
功能: 为某个堆使能堆特性。
参数:
HeapHandle: 堆句柄,为HeapCreate或GetProcessHeap函数的返回值;
HeapInformationClass: 将被设置的特性值;最重要的值是HeapEnableTerminationOnCorruption,表示使能堆被破坏则程序中止特性;
...
示例:
[cpp] view
plain copy
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
备注: 强烈推荐在程序中使能HeapEnableTerminationOnCorruption。
3. 显式加载动态库函数示例
[cpp] view
plain copy
HINSTANCE h_Kernel32 = LoadLibrary("kernel32.dll");
if(h_Kernel32)
{
BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
mySetDllDirectoryA = (BOOL WINAPI (*)(const char*))GetProcAddress(h_Kernel32, "SetDllDirectoryA");
if(mySetDllDirectoryA) mySetDllDirectoryA("");
FreeLibrary(h_Kernel32);
}
备注:
1. LoadLibrary的参数可带路径,但路径必须使用'\',如果不带路径,则会在默认搜索路径搜索该dll,默认搜索路径参考SetDllDirectory备注3;
2. 若进程是第一次加载某个dll,则LoadLibrary将使用DLL_PROCESS_ATTACH调用该dll的DllMain()函数,如果DllMain返回FALSE,则LoadLibrary将卸载该dll并返回NULL;
3. BOOL WINAPI SetDllDirectory(_In_opt_ LPCTSTR lpPathName):
功能:将lpPathName添加到dll搜索路径,若该参数为"",则将当前路径从dll默认搜索路径中删除;若该参数为NULL,则函数还原默认搜索搜索路径;
dll默认搜索路径:
1. 当前程序路径;
2. 由lpPathName指定的路径;
3. 系统路径;(GetSystemDirectory函数可获取)
4. 16位系统路径,即System路径;
5. Windows路径;(GetWindowsDirectory函数可获取)
6. PATH所指定的路径。
若需要添加多余一个的路径,需要使用AddDllDirectory函数。
4. SetProcessDEPPolicy
原型:
[cpp] view
plain copy
BOOL WINAPI SetProcessDEPPolicy(_In_ DWORD dwFlags);
功能: 设置数据执行保护策略(DEP,Data Execution Prevention)。类似于读/写/执行权限控制。
参数:
dwFlags: 0: 禁止DEP;
1:使能DEP。
5. 命令行参数读取示例
[cpp] view
plain copy
#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")
#include <windows.h>
#include <stdio.h>
static char *FromWide(const wchar_t *wide)
{
size_t len;
len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
char *out = (char *)malloc(len);
if (out)
WideCharToMultiByte(CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
return out;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
int argc;
printf("%s\n", lpCmdLine); // lpCmdLine是所有命令行参数组成的一个字符串
// CommandLineToArgvW返回和标准c类似的wargv,参数个数保存在argc中,注意,使用完之后,wargv需要调用LocalFree释放
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (wargv == NULL) return 1;
for (int i = 0; i < argc; i++) wprintf(L"%s\n", wargv[i]);
char **argv = (char**)malloc(sizeof(char*)*argc);
if (argv == NULL) return 1;
for (int i = 0; i < argc; i++) argv[i] = FromWide(wargv[i]);
for (int i = 0; i < argc; i++) printf("%s\n", argv[i]);
for (int i = 0; i < argc; i++) free(argv[i]);
free(argv);
LocalFree(wargv);
system("pause");
return 0;
}
备注:
1. GetCommandLine: 该函数返回一个纸箱当前进程的命令行字符串的指针;
2. CommandLineToArgvW: 该函数解析一个unicode命令行字符串,返回一个纸箱命令行参数的指针首地址(相当于标准C的argv),注意此函数的返回指针在使用完后需要调用LocalFree()释放;
3. WideCharToMultiByte: 将宽字符集(UTF-16)的字符串转换为另一种字符集的字符串。
[cpp] view
plain copy
int WideCharToMultiByte(
_In_ UINT CodePage,
_In_ DWORD dwFlags,
_In_ LPCWSTR lpWideCharStr,
_In_ int cchWideChar,
_Out_opt_ LPSTR lpMultiByteStr,
_In_ int cbMultiByte,
_In_opt_ LPCSTR lpDefaultChar,
_Out_opt_ LPBOOL lpUsedDefaultChar
);
原型:
[cpp] view
plain copy
UINT WINAPI SetErrorMode( _In_ UINT uMode);
功能:
控制指定类型的严重错误是由windows处理还是由应用程序处理。
参数:
uMode:
参数值 | 含义 |
0 | 使用系统默认的,既显示所有错误的对话框 |
SEM_FAILCRITICALERRORS 0x0001 | 系统不显示关键错误处理消息框。 相反,系统发送错误给调用进程。 |
SEM_NOALIGNMENTFAULTEXCEPT 0x0004 | 系统会自动修复故障此功能只支持部分处理器架构。 |
SEM_NOGPFAULTERRORBOX 0x0002 | 系统不显示Windows错误报告对话框。 |
SEM_NOOPENFILEERRORBOX 0x8000 | 当无法找到文件时不弹出错误对话框。 相反,错误返回给调用进程。 |
备注:默认情况下,子进程继承父进程的错误模式标志
2. HeapSetInformation:
原型:
[cpp] view
plain copy
BOOL WINAPI HeapSetInformation(
_In_opt_ HANDLE HeapHandle,
_In_ HEAP_INFORMATION_CLASS HeapInformationClass,
_In_ PVOID HeapInformation,
_In_ SIZE_T HeapInformationLength
);
功能: 为某个堆使能堆特性。
参数:
HeapHandle: 堆句柄,为HeapCreate或GetProcessHeap函数的返回值;
HeapInformationClass: 将被设置的特性值;最重要的值是HeapEnableTerminationOnCorruption,表示使能堆被破坏则程序中止特性;
...
示例:
[cpp] view
plain copy
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
备注: 强烈推荐在程序中使能HeapEnableTerminationOnCorruption。
3. 显式加载动态库函数示例
[cpp] view
plain copy
HINSTANCE h_Kernel32 = LoadLibrary("kernel32.dll");
if(h_Kernel32)
{
BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
mySetDllDirectoryA = (BOOL WINAPI (*)(const char*))GetProcAddress(h_Kernel32, "SetDllDirectoryA");
if(mySetDllDirectoryA) mySetDllDirectoryA("");
FreeLibrary(h_Kernel32);
}
备注:
1. LoadLibrary的参数可带路径,但路径必须使用'\',如果不带路径,则会在默认搜索路径搜索该dll,默认搜索路径参考SetDllDirectory备注3;
2. 若进程是第一次加载某个dll,则LoadLibrary将使用DLL_PROCESS_ATTACH调用该dll的DllMain()函数,如果DllMain返回FALSE,则LoadLibrary将卸载该dll并返回NULL;
3. BOOL WINAPI SetDllDirectory(_In_opt_ LPCTSTR lpPathName):
功能:将lpPathName添加到dll搜索路径,若该参数为"",则将当前路径从dll默认搜索路径中删除;若该参数为NULL,则函数还原默认搜索搜索路径;
dll默认搜索路径:
1. 当前程序路径;
2. 由lpPathName指定的路径;
3. 系统路径;(GetSystemDirectory函数可获取)
4. 16位系统路径,即System路径;
5. Windows路径;(GetWindowsDirectory函数可获取)
6. PATH所指定的路径。
若需要添加多余一个的路径,需要使用AddDllDirectory函数。
4. SetProcessDEPPolicy
原型:
[cpp] view
plain copy
BOOL WINAPI SetProcessDEPPolicy(_In_ DWORD dwFlags);
功能: 设置数据执行保护策略(DEP,Data Execution Prevention)。类似于读/写/执行权限控制。
参数:
dwFlags: 0: 禁止DEP;
1:使能DEP。
5. 命令行参数读取示例
[cpp] view
plain copy
#pragma comment( linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")
#include <windows.h>
#include <stdio.h>
static char *FromWide(const wchar_t *wide)
{
size_t len;
len = WideCharToMultiByte(CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
char *out = (char *)malloc(len);
if (out)
WideCharToMultiByte(CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
return out;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
int argc;
printf("%s\n", lpCmdLine); // lpCmdLine是所有命令行参数组成的一个字符串
// CommandLineToArgvW返回和标准c类似的wargv,参数个数保存在argc中,注意,使用完之后,wargv需要调用LocalFree释放
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (wargv == NULL) return 1;
for (int i = 0; i < argc; i++) wprintf(L"%s\n", wargv[i]);
char **argv = (char**)malloc(sizeof(char*)*argc);
if (argv == NULL) return 1;
for (int i = 0; i < argc; i++) argv[i] = FromWide(wargv[i]);
for (int i = 0; i < argc; i++) printf("%s\n", argv[i]);
for (int i = 0; i < argc; i++) free(argv[i]);
free(argv);
LocalFree(wargv);
system("pause");
return 0;
}
备注:
1. GetCommandLine: 该函数返回一个纸箱当前进程的命令行字符串的指针;
2. CommandLineToArgvW: 该函数解析一个unicode命令行字符串,返回一个纸箱命令行参数的指针首地址(相当于标准C的argv),注意此函数的返回指针在使用完后需要调用LocalFree()释放;
3. WideCharToMultiByte: 将宽字符集(UTF-16)的字符串转换为另一种字符集的字符串。
[cpp] view
plain copy
int WideCharToMultiByte(
_In_ UINT CodePage,
_In_ DWORD dwFlags,
_In_ LPCWSTR lpWideCharStr,
_In_ int cchWideChar,
_Out_opt_ LPSTR lpMultiByteStr,
_In_ int cbMultiByte,
_In_opt_ LPCSTR lpDefaultChar,
_Out_opt_ LPBOOL lpUsedDefaultChar
);
相关文章推荐
- Radar Installation(贪心 区间覆盖)
- Java enum的用法
- 【Android基础知识】使用Gallery和ImageSwitcher实现图片轮播效果
- 电子商务系统需求分析
- RabbitMQ性能测试案例
- 后缀表达式
- Nginx转发给Tomcat页面去掉端口号
- Js运动动画系列5--多物体运动-透明度
- java-----容器类总结之ArrayList与LinkedList的区别
- Oracle修改字段相关操作
- 消息中间件-ActiveMQ入门实例
- 鼠标放到图片上会滑出提示文字
- 鼠标滑过,缩略图放大显示(纯CSS)
- 强大的sscanf函数
- jenkins服务构建
- c3p0、dbcp和proxool比较
- 平滑图片滚动
- 前后轮翻的JS图片幻灯切换
- 清爽简洁的图片交替导航效果
- 利用html5、websocket和opencv实现人脸检测