关闭

用代码设置Minidump的注册表项

958人阅读 评论(0) 收藏 举报

今天实在无聊,写了一个超级没有技术含量的代码:(

晒晒吧o(∩_∩)o...

 int SetMinidump(int debuglevel)
{
 HKEY hKEY;
 DWORD ret;
 DWORD value;
 char* szvalue = NULL;


 LPCTSTR data_Set = "SYSTEM//CurrentControlSet//Control//CrashControl";

 ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, data_Set, 0, KEY_WRITE, &hKEY);
 if(ret != ERROR_SUCCESS)
 {
  PrintDebugInfo(debuglevel,"[SetMinidump] Open Register DataSet Error, Return");
  return 0;
 }
 else
 {
  PrintDebugInfo(debuglevel,"[SetMinidump] Open Register DataSet              [OK]");
 }

 //是否自动重启
 //0x00: 不自动重启  0x01: 自动重启   
 value = 0;
 ret = RegSetValueEx(hKEY, "AutoReboot", NULL, REG_DWORD, (LPBYTE)&value, 4);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl AutoReboot Error, Return");
  return 0;
 }
 else
 {  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl AutoReboot        [OK]");

 }

 //设置dump类型:
 //  0x00:不记录    0x01:记录小转储64K   0x02:核心转储  0x03: 完全转储
 value = 3;
 ret = RegSetValueEx(hKEY, "CrashDumpEnabled", NULL, REG_DWORD, (LPBYTE)&value, 4);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl CrashDumpEnabled Error, Return");
  return 0;
 }
 else
 {
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl CrashDumpEnabled  [OK]");
 }

 //设置dump文件名
 szvalue = "%SystemRoot%//MEMORY.DMP";
 value = strlen(szvalue);
 ret = RegSetValueEx(hKEY, "DumpFile", NULL, REG_EXPAND_SZ, (LPBYTE)szvalue, value);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl DumpFile Error, Return");
  return 0;
 }
 else
 { 
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl DumpFile          [OK]");
 }


 //设置是否记录Minidump
 //0x00:不记录minidump   0x01:记录minidump
 value = 1;
 ret = RegSetValueEx(hKEY, "LogEvent", NULL, REG_DWORD, (LPBYTE)&value, 4);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl LogEvent Error, Return");
  return 0;
 }
 else
 { 
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl LogEvent          [OK]");
 }

 //设置Minidump目录
    szvalue = "%SystemRoot%//Minidump";
 value = strlen(szvalue);
 ret = RegSetValueEx(hKEY, "MinidumpDir", NULL, REG_EXPAND_SZ, (LPBYTE)szvalue, value);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl MinidumpDir Error, Return");
  return 0;
 }
 else
 { 
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl MinidumpDir       [OK]");
 }
 //设置是否覆盖
 //0x00:不覆盖   0x01:覆盖
 value = 1;
 ret = RegSetValueEx(hKEY, "Overwrite", NULL, REG_DWORD, (LPBYTE)&value, 4);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl Overwrite Error, Return");
  return 0;
 }
 else
 {
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl Overwrite         [OK]");
 }

 //设置是否发送告警
 //0x00:不发送   0x01:发送
 value = 1;
 ret = RegSetValueEx(hKEY, "SendAlert", NULL, REG_DWORD, (LPBYTE)&value, 4);
 if(ret != ERROR_SUCCESS)
 {
  RegCloseKey(hKEY);
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl SendAlert Error, Return");
  return 0;
 }
 else
 {
  PrintDebugInfo(debuglevel,"[SetMinidump] Set CrashControl SendAlert         [OK]");
 }


 RegCloseKey(hKEY);
 return 1;
}

void PrintDebugInfo(int level,char *fmt, ...)
{
 if(level == 0)
 {
  return;
 }
 
 char out[1024];
 va_list body;
 va_start(body, fmt);
 vsprintf(out, fmt, body);     // 译注:格式化输入的字符串 fmtt
 va_end(body);                 //       到输出字符串 ou
 OutputDebugString(out);       // 译注:输出格式化后的字符串到调试器
}

 

写那么多代码,没啥用,不如写个reg文件来得实惠

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/CrashControl]
"AutoReboot"=dword:00000000
"CrashDumpEnabled"=dword:00000003
"DumpFile"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,/
  74,00,25,00,5c,00,4d,00,45,00,4d,00,4f,00,52,00,59,00,2e,00,44,00,4d,00,50,/
  00,00,00
"LogEvent"=dword:00000001
"MinidumpDir"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,/
  00,74,00,25,00,5c,00,4d,00,69,00,6e,00,69,00,64,00,75,00,6d,00,70,00,00,00
"Overwrite"=dword:00000001
"SendAlert"=dword:00000001

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:169245次
    • 积分:2169
    • 等级:
    • 排名:第18252名
    • 原创:32篇
    • 转载:43篇
    • 译文:0篇
    • 评论:78条
    最新评论