tagging release
[dasher.git] / Src / Win32 / Common / WinHelper.cpp
blob9fe29cefc82355fea5cbcd6cec8a0ce7762af3be
1 // WinHelper.cpp
2 //
3 /////////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (c) 2002 Iain Murray, Inference Group, Cavendish, Cambridge.
6 //
7 /////////////////////////////////////////////////////////////////////////////
9 #include "WinCommon.h"
11 #include "WinHelper.h"
13 // Track memory leaks on Windows to the line that new'd the memory
14 #ifdef _WIN32
15 #ifdef _DEBUG
16 #define DEBUG_NEW new( _NORMAL_BLOCK, THIS_FILE, __LINE__ )
17 #define new DEBUG_NEW
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
21 #endif
23 namespace WinHelper {
24 HINSTANCE hInstApp;
25 } void WinHelper::LastWindowsError() {
26 LPVOID lpMsgBuf;
27 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
28 (LPTSTR) & lpMsgBuf, 0, NULL);
29 MessageBox(NULL, (LPCTSTR) lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONINFORMATION);
30 LocalFree(lpMsgBuf);
33 namespace {
34 bool CommonControlsInited = false;
37 void WinHelper::InitCommonControlLib() {
38 if(CommonControlsInited)
39 return;
41 #ifdef OriginalWin95
42 InitCommonControls();
43 #else
44 INITCOMMONCONTROLSEX iccex;
45 iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
46 iccex.dwICC = ICC_BAR_CLASSES | ICC_COOL_CLASSES;
47 InitCommonControlsEx(&iccex);
48 #endif
50 CommonControlsInited = true;
53 #ifndef OriginalWin95
54 namespace {
55 // For SHGetSpecialFolderPath. Needs IE 4.0 or above (Win98+ always ok).
56 #include "shlobj.h"
58 #endif
60 void WinHelper::GetUserDirectory(Tstring *Output) {
61 Tstring & UserData = *Output;
63 #ifdef OriginalWin95
64 // Not a lot else I can do :(
65 GetAppDirectory(Output);
66 #elif defined DASHER_WINCE
67 GetAppDirectory(Output);
68 #else
70 TCHAR Buffer[MAX_PATH];
71 // My documentation says SHGetSpecialFolderPath returns NOERROR if successful
72 // With my headers NOERROR==0 and this function returns TRUE==1 as docs online say.
73 // Not sure I trust the return value, so I'm just going to assume if I have a string
74 // it is probably correct :)
75 Buffer[0] = TEXT('\0');
76 SHGetSpecialFolderPath(NULL, Buffer, CSIDL_APPDATA, 0);
77 UserData = Buffer;
78 if(UserData[UserData.size() - 1] != TEXT('\\'))
79 UserData += TEXT('\\');
81 #endif
85 void WinHelper::GetAppDirectory(Tstring *Output) {
86 Tstring & AppLocation = *Output;
88 TCHAR Buffer[MAX_PATH];
89 if(0 != GetModuleFileName(NULL, Buffer, MAX_PATH)) {
90 TCHAR *pos = _tcsrchr(Buffer, TEXT('\\')); // 3 line hack to remove filename
91 pos++; //
92 *pos = TEXT('\0'); //
93 AppLocation = Buffer;
95 else
96 AppLocation = TEXT(""); // Cop out. Current directory may be elsewhere.
97 // Hopefully it won't come to this...