changeset 2398:4de830d3bf9e

Win: Remove winmain.c as it is unnecessary when using the dwmain() entrypoint macros. Export _dw_convertargs() internal function for use in the dwmain macro.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 25 Mar 2021 21:38:58 +0000
parents f181963f23d5
children 1cbc316292c1
files dw.h makefile.vc win/dw.c win/dw.def winmain.c
diffstat 5 files changed, 22 insertions(+), 139 deletions(-) [+]
line wrap: on
line diff
--- a/dw.h	Thu Mar 25 20:51:53 2021 +0000
+++ b/dw.h	Thu Mar 25 21:38:58 2021 +0000
@@ -1629,6 +1629,15 @@
     dw_thread_new(_dw_main_launch, data, 0); \
     _dw_main_thread(argc, argv); } \
 int _dwmain(a, b)
+#elif defined(__WIN32__)
+#define dwmain(a, b) \
+_dwmain(a, b); \
+char ** API _dw_convertargs(int *count, char *start, HINSTANCE hInstance); \
+int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {\
+   int argc; \
+   char **argv = _dw_convertargs(&argc, lpCmdLine, hInstance); \
+   return _dwmain(argc, argv); } \
+int _dwmain(a, b)
 #else
 #define dwmain(a, b) main(a, b)
 #endif
--- a/makefile.vc	Thu Mar 25 20:51:53 2021 +0000
+++ b/makefile.vc	Thu Mar 25 21:38:58 2021 +0000
@@ -210,13 +210,10 @@
 dwtest.obj: $(SRCDIR)\dwtest.c
 	$(CC) $(CFLAGS) $(CFLAGS_DEBUG) $(SRCDIR)\dwtest.c
 
-winmain.obj: $(SRCDIR)\winmain.c
-	$(CC) $(CFLAGS) $(CFLAGS_DEBUG) $(SRCDIR)\winmain.c
-
 dwtest: dwtest.exe
 
-dwtest.exe: dwtest.obj winmain.obj
-	$(LINK) $(LINKFLAGS) /out:dwtest.exe dwtest.obj winmain.obj /subsystem:windows $(DWLIBDIR)\dwcompat.lib $(DWLIBDIR)\dw.lib $(LIBS)
+dwtest.exe: dwtest.obj
+	$(LINK) $(LINKFLAGS) /out:dwtest.exe dwtest.obj /subsystem:windows $(DWLIBDIR)\dwcompat.lib $(DWLIBDIR)\dw.lib $(LIBS)
 	mt.exe /manifest dwtest.exe.manifest $(SRCDIR)\win\dwtest.exe.$(TARGET_CPU).manifest /outputresource:dwtest.exe;1
 	-erase dwtest.exe.manifest
 
--- a/win/dw.c	Thu Mar 25 20:51:53 2021 +0000
+++ b/win/dw.c	Thu Mar 25 21:38:58 2021 +0000
@@ -410,16 +410,23 @@
 };
 
 #ifdef BUILD_DLL
+/* Old function to store the instance handle, kept for compatibilty. */
 void Win32_Set_Instance(HINSTANCE hInstance)
 {
    _DWInstance = hInstance;
 }
-#else
-char **_convertargs(int *count, char *start)
+#endif
+
+/*
+ * Internal function to convert WinMain arguments into main() style.
+ * Also saves the handle to the instance passed from WinMain().
+ */
+char ** API _dw_convertargs(int *count, char *start, HINSTANCE hInstance)
 {
    char *tmp, *argstart, **argv;
    int loc = 0, inquotes = 0;
 
+   _DWInstance = hInstance;
    (*count) = 1;
 
    tmp = start;
@@ -450,7 +457,7 @@
       }
    }
 
-   argv = (char **)malloc(sizeof(char *) * ((*count)+1));
+   argv = (char **)calloc(sizeof(char *), ((*count)+1));
    argv[0] = calloc(261, 1);
    GetModuleFileNameA(_DWInstance, argv[0], 260);
 
@@ -499,20 +506,6 @@
    return argv;
 }
 
-/* Ok this is a really big hack but what the hell ;) */
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
-{
-   char **argv;
-   int argc;
-
-   _DWInstance = hInstance;
-
-   argv = _convertargs(&argc, lpCmdLine);
-
-   return main(argc, argv);
-}
-#endif
-
 #ifdef UNICODE
 /* Macro and internal function to convert UTF8 to Unicode wide characters */
 #define UTF8toWide(a) _myUTF8toWide(a, a ? _alloca(MultiByteToWideChar(CP_UTF8, 0, a, -1, NULL, 0) * sizeof(WCHAR)) : NULL)
--- a/win/dw.def	Thu Mar 25 20:51:53 2021 +0000
+++ b/win/dw.def	Thu Mar 25 21:38:58 2021 +0000
@@ -2,6 +2,7 @@
 
 EXPORTS
   Win32_Set_Instance                     @1
+  _dw_convertargs                        @2
 
   dw_init                                @10
   dw_main                                @11
--- a/winmain.c	Thu Mar 25 20:51:53 2021 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,117 +0,0 @@
-/* Dynamic Windows stub file to allow Win32 applications
- * to use the main() entry point instead of WinMain().
- *
- * (C) 2000-2012 Brian Smith <brian@dbsoft.org>
- */
-
-#include <windows.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-#include <process.h>
-
-#ifndef NODW
-void Win32_Set_Instance(HINSTANCE hInstance);
-#endif
-
-char **_convertargs(int *count, char *start, HINSTANCE DWInstance)
-{
-   char *tmp, *argstart, **argv;
-   int loc = 0, inquotes = 0;
-
-   (*count) = 1;
-
-   tmp = start;
-
-   /* Count the number of entries */
-   if(*start)
-   {
-      (*count)++;
-
-      while(*tmp)
-      {
-         if(*tmp == '"' && inquotes)
-            inquotes = 0;
-         else if(*tmp == '"' && !inquotes)
-            inquotes = 1;
-         else if(*tmp == ' ' && !inquotes)
-         {
-            /* Push past any white space */
-            while(*(tmp+1) == ' ')
-               tmp++;
-            /* If we aren't at the end of the command
-             * line increment the count.
-             */
-            if(*(tmp+1))
-               (*count)++;
-         }
-         tmp++;
-      }
-   }
-
-   argv = (char **)malloc(sizeof(char *) * ((*count)+1));
-   argv[0] = malloc(260);
-   GetModuleFileNameA(DWInstance, argv[0], 260);
-
-   argstart = tmp = start;
-
-   if(*start)
-   {
-      loc = 1;
-
-      while(*tmp)
-      {
-         if(*tmp == '"' && inquotes)
-         {
-            *tmp = 0;
-            inquotes = 0;
-         }
-         else if(*tmp == '"' && !inquotes)
-         {
-            argstart = tmp+1;
-            inquotes = 1;
-         }
-         else if(*tmp == ' ' && !inquotes)
-         {
-            *tmp = 0;
-            argv[loc] = _strdup(argstart);
-
-            /* Push past any white space */
-            while(*(tmp+1) == ' ')
-               tmp++;
-
-            /* Move the start pointer */
-            argstart = tmp+1;
-
-            /* If we aren't at the end of the command
-             * line increment the count.
-             */
-            if(*(tmp+1))
-               loc++;
-         }
-         tmp++;
-      }
-      if(*argstart)
-         argv[loc] = _strdup(argstart);
-   }
-   argv[loc+1] = NULL;
-   return argv;
-}
-
-/* Protoype for the application entrypoint */
-int main(int argc, char **argv);
-
-/* Ok this is a really big hack but what the hell ;) */
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
-{
-   char **argv;
-   int argc;
-
-#ifndef NODW
-   Win32_Set_Instance(hInstance);
-#endif
-
-   argv = _convertargs(&argc, lpCmdLine, hInstance);
-
-   return main(argc, argv);
-}