OSDN Git Service

Add TortoiseProc
[tortoisegit/TortoiseGitJp.git] / Utils / DebugHelpers.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2006 - Stefan Kueng\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "debughelpers.h"\r
21 \r
22 CString GetLastErrorMessageString(int err)\r
23 {\r
24         LPVOID lpMsgBuf;\r
25         FormatMessage( \r
26                 FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
27                 FORMAT_MESSAGE_FROM_SYSTEM | \r
28                 FORMAT_MESSAGE_IGNORE_INSERTS,\r
29                 NULL,\r
30                 err,\r
31                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
32                 (LPTSTR) &lpMsgBuf,\r
33                 0,\r
34                 NULL \r
35         );\r
36         CString temp = CString((LPCTSTR)lpMsgBuf);\r
37         LocalFree(lpMsgBuf);\r
38         return temp;\r
39 };\r
40 \r
41 CString GetLastErrorMessageString()\r
42 {\r
43         LPVOID lpMsgBuf;\r
44         FormatMessage( \r
45                 FORMAT_MESSAGE_ALLOCATE_BUFFER | \r
46                 FORMAT_MESSAGE_FROM_SYSTEM | \r
47                 FORMAT_MESSAGE_IGNORE_INSERTS,\r
48                 NULL,\r
49                 GetLastError(),\r
50                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language\r
51                 (LPTSTR) &lpMsgBuf,\r
52                 0,\r
53                 NULL \r
54         );\r
55         CString temp = CString((LPCTSTR)lpMsgBuf);\r
56         LocalFree(lpMsgBuf);\r
57         return temp;\r
58 };\r
59 \r
60 void SetThreadName(DWORD dwThreadID, LPCTSTR szThreadName)\r
61 {\r
62 #ifdef _UNICODE\r
63         char narrow[_MAX_PATH * 3];\r
64         BOOL defaultCharUsed;\r
65         int ret = WideCharToMultiByte(CP_ACP, 0, szThreadName, (int)_tcslen(szThreadName), narrow, _MAX_PATH*3 - 1, ".", &defaultCharUsed);\r
66         narrow[ret] = 0;\r
67 #endif\r
68         THREADNAME_INFO info;\r
69         info.dwType = 0x1000;\r
70 #ifdef _UNICODE\r
71         info.szName = narrow;\r
72 #else\r
73         info.szName = szThreadName;\r
74 #endif\r
75         info.dwThreadID = dwThreadID;\r
76         info.dwFlags = 0;\r
77 \r
78         __try\r
79         {\r
80                 RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD),\r
81                         (DWORD *)&info);\r
82         }\r
83         __except (EXCEPTION_CONTINUE_EXECUTION)\r
84         {\r
85         }\r
86 }\r
87 \r