OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
winmain.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of WinMain for user exes
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/defs.h>
10#include <owl/applicat.h>
11#include <owl/lclstrng.h>
12#include <owl/gdiplus.h>
13
14#if defined(BI_COMP_BORLANDC)
15#include <dos.h>
16#endif
17
18#if defined(BI_COMP_GNUC) || defined(BI_COMP_CLANG)
19
20// We need some headers for our command line parser.
21//
22#include <vector>
23#include <algorithm>
24
25#endif
26
27#if defined(__BORLANDC__)
28# pragma option -w-ccc // Disable "Condition is always true/false"
29#endif
30
31namespace owl {
32
35
36} // OWL namespace
37
38using namespace std;
39using namespace owl;
40
41#if defined(BI_COMP_GNUC) || defined(BI_COMP_CLANG)
42
43namespace
44{
45
46 // Parse the command line into an argv-style string list.
47 // Note: Does not handle escape characters.
48 //
50 {
51 using TIt = tstring::const_iterator;
52 const auto quote = _T('\"');
53
54 auto parseQuotedString = [&](TIt& i, TIt end) -> tstring
55 {
56 PRECONDITION(i != end && *i == quote);
57 const auto e = find(++i, end, quote);
58 if (e == end) throw TXOwl{_T("Unpaired quote in command-line.")};
59 const auto s = tstring{i, e};
60 i = next(e);
61 return s;
62 };
63
64 auto parseString = [](TIt& i, TIt end) -> tstring
65 {
66 PRECONDITION(i != end);
67 const auto e = find_if(i, end, [](tchar c) { return _istspace(c); });
68 const auto s = tstring{i, e};
69 i = e;
70 return s;
71 };
72
73 auto a = vector<tstring>{};
74 for (auto i = cmd.begin(); i != cmd.end();)
75 {
76 auto c = *i;
77 if (_istspace(c))
78 ++i;
79 else if (c == quote)
80 a.push_back(parseQuotedString(i, cmd.end()));
81 else
82 a.push_back(parseString(i, cmd.end()));
83 }
84 return a;
85 };
86
87} // namespace
88
89#endif
90
91// MSC code in module.cpp
92#if !defined(_BUILDOWLDLL) && !defined(_OWLDLL)
93#if defined(BI_COMP_BORLANDC)
94// NOTE: We use priority 31 to come just before/after ctr/dtr of global
95// objects (which are assigned a priorority of 32)
96//
97long TlsAddRefs();
98long TlsRelease();
99
100static void __initOWL()
101{
102 TlsAddRefs();
103}
104#pragma startup __initOWL 31
105
106//
107static void __termOWL()
108{
109 TlsRelease();
110}
111#pragma exit __termOWL 31
112#endif // BI_COMP_BORLANDC
113#endif // if !_BUILDOWLDLL
114
115using owl::TModule;
117
118#if defined(BI_COMP_BORLANDC) && defined(_UNICODE)
119
120extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShow);
121
122#elif defined(BI_COMP_GNUC)
123
124extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShow);
125
126#endif
127
128//
129// Default WinMain calls OwlMain, after setting params into TApplication
130//
131int WINAPI
133{
134 TRACEX(OwlMain, 0, _T("WinMain(")
135 << hex << static_cast<void*>(hInstance) << _T(", ")
136 << hex << static_cast<void*>(hPrevInstance) << _T(", \"")
137 << TResId(cmdLine) << _T("\", ")
138 << cmdShow << _T(") called"));
139
140 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
142 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
143
145
147 int retVal;
148 try
149 {
150
151#if defined(BI_COMP_GNUC) || defined(BI_COMP_CLANG)
152
153 // Create an argv-style array of string pointers to the command line arguments.
154 //
156 auto argv = vector<LPTSTR>{a.size()};
157 transform(a.begin(), a.end(), argv.begin(), [](tstring& s) {return &s[0];});
158 retVal = OwlMain(argv.size(), &argv[0]);
159
160#elif defined(BI_COMP_BORLANDC)
161
163
164#else
165
167
168#endif
169
170 }
171 catch (owl::TXEndSession&) {retVal = 0;}
172 catch (owl::TXBase& x) {retVal = owl::HandleGlobalException(x, 0);}
173
174 Gdiplus::GdiplusShutdown(gdiplusToken);
175 TRACEX(OwlMain, 0, _T("WinMain() returns ") << retVal);
176 return retVal;
177}
Definition of class TApplication.
#define PRECONDITION(condition)
Definition checks.h:227
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
#define TRACEX(group, level, message)
Definition checks.h:263
Derived from TModule and TMsgThread and virtually derived from TEventHandler, TApplication acts as an...
Definition applicat.h:141
static void SetWinMainParams(HINSTANCE hInstance, HINSTANCE hPrevInstance, const tstring &cmdLine, int cmdShow)
Set the data members with data from WinMain.
Definition applicat.h:694
ObjectWindows dynamic-link libraries (DLLs) construct an instance of TModule, which acts as an object...
Definition module.h:75
Derived from xmsg, TXBase is the base class for ObjectWindows and ObjectComponents exception-handling...
Definition exbase.h:41
TXEndSession is thrown from TWindow's handler for WM_ENDSESSION.
Definition except.h:111
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define _tWinMain
Definition cygwin.h:92
#define _T(x)
Definition cygwin.h:51
Utilities for GDI+ interoperability.
TLocaleString - localized name support.
long TlsAddRefs()
Definition thread.cpp:761
long TlsRelease()
Definition thread.cpp:766
STDAPI_(owl::TDocTemplate **) GetDocTemplateHead(owl STDAPI_(owl::TModule **) GetModulePtr(owl in OwlMain)(int argc, TCHAR *argv[])
Main entry point for an Owl application.
Definition module.h:391
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
OWL_DIAGINFO
Definition animctrl.cpp:14
int HandleGlobalException(owl::TXBase &x, LPCTSTR caption, LPCTSTR canResume=nullptr)
Definition except.cpp:29
std::string tstring
Definition defs.h:79
void InitGlobalModule(HINSTANCE hInstance)
Definition global.cpp:36
General definitions used by all ObjectWindows programs.