Use Slim Reader/Writer lock to replace CRITICAL_SECTION (better performance).
[gdipp.git] / gdipp_pre / gdipp_pre.cpp
blob9b63f8eb671d4b6065a441aa99f55c0271c99511
1 // gdipp_pre.cpp : main source file for gdipp_pre.exe
2 //
4 #include "stdafx.h"
5 #include "gdipp_pre.h"
6 #include "gdipp_lib/helper.h"
7 #include "gdipp_pre/resource.h"
8 #include "gdipp_pre/MainDlg.h"
10 CAppModule _Module;
12 HINSTANCE h_instance;
13 wchar_t config_path[MAX_PATH];
14 wchar_t *preview_text = NULL;
16 int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
18 CMessageLoop theLoop;
19 _Module.AddMessageLoop(&theLoop);
21 CMainDlg dlgMain;
23 if (dlgMain.Create(NULL) == NULL)
25 ATLTRACE(_T("Main dialog creation failed!\n"));
26 return 0;
29 dlgMain.ShowWindow(nCmdShow);
31 int nRet = theLoop.Run();
33 _Module.RemoveMessageLoop();
34 return nRet;
37 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
39 BOOL b_ret;
41 h_instance = hInstance;
43 b_ret = gdipp::get_dir_file_path(hInstance, L"gdipp_config.xml", config_path);
44 assert(b_ret);
46 wchar_t preview_text_path[MAX_PATH];
47 b_ret = gdipp::get_dir_file_path(hInstance, L"gdipp_preview.txt", preview_text_path);
48 assert(b_ret);
50 FILE *f;
51 errno_t err = _wfopen_s(&f, preview_text_path, L"r, ccs=UNICODE");
52 if (err == 0)
54 fseek(f, 0, SEEK_END);
55 const long text_len = ftell(f);
56 rewind(f);
58 preview_text = new wchar_t[text_len + 1];
59 const size_t bytes_read = fread(preview_text, sizeof(wchar_t), text_len, f);
60 preview_text[bytes_read] = L'\0';
62 fclose(f);
65 HMODULE h_gdimm = NULL;
66 WCHAR gdimm_path[MAX_PATH];
68 #ifdef _M_X64
69 b_ret = gdipp::get_dir_file_path(NULL, L"gdimm_64.dll", gdimm_path);
70 #else
71 b_ret = gdipp::get_dir_file_path(NULL, L"gdimm_32.dll", gdimm_path);
72 #endif // _M_X64
74 // TODO: incomplete code
75 h_gdimm = LoadLibraryW(gdimm_path);
76 if (h_gdimm == NULL)
79 // If you are running on NT 4.0 or higher you can use the following call instead to
80 // make the EXE free threaded. This means that calls come in on a random RPC thread.
81 HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
82 ATLASSERT(SUCCEEDED(hRes));
84 // this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
85 ::DefWindowProc(NULL, 0, 0, 0L);
87 AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
89 hRes = _Module.Init(NULL, hInstance);
90 ATLASSERT(SUCCEEDED(hRes));
92 int nRet = Run(lpstrCmdLine, nCmdShow);
94 _Module.Term();
95 ::CoUninitialize();
97 if (h_gdimm != NULL)
98 FreeLibrary(h_gdimm);
100 if (preview_text != NULL)
101 delete[] preview_text;
103 return nRet;