From eea09632a3ef44a2151c6bd1e18e0189d20027f5 Mon Sep 17 00:00:00 2001
From: nickysn <nickysn@gmail.com>
Date: Sun, 6 Sep 2015 10:25:34 +0000
Subject: [PATCH] + add and initialize the win16 system unit variables that
 correspond to the   WinMain parameters in C

git-svn-id: trunk@31547 -
---
 rtl/inc/resh.inc       |  2 ++
 rtl/inc/sysres.inc     |  2 ++
 rtl/win16/prt0comn.asm | 19 +++++++++++++------
 rtl/win16/system.pp    | 12 ++++++++++++
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/rtl/inc/resh.inc b/rtl/inc/resh.inc
index f40690d085..cefdde5eb7 100644
--- a/rtl/inc/resh.inc
+++ b/rtl/inc/resh.inc
@@ -53,7 +53,9 @@ function Is_IntResource(aStr : pchar) : boolean; {$ifdef SYSTEMINLINE}inline;{$e
 function MakeLangID(primary,sub : word) : word; {$ifdef SYSTEMINLINE}inline;{$endif}
 
 // Win32 API compatible Resource functions
+{$ifndef Win16}
 Function HINSTANCE : TFPResourceHMODULE;
+{$endif Win16}
 Function EnumResourceTypes(ModuleHandle : TFPResourceHMODULE; EnumFunc : EnumResTypeProc; lParam : PtrInt) : LongBool;
 Function EnumResourceNames(ModuleHandle : TFPResourceHMODULE; ResourceType : PChar; EnumFunc : EnumResNameProc; lParam : PtrInt) : LongBool;
 Function EnumResourceLanguages(ModuleHandle : TFPResourceHMODULE; ResourceType, ResourceName : PChar; EnumFunc : EnumResLangProc; lParam : PtrInt) : LongBool;
diff --git a/rtl/inc/sysres.inc b/rtl/inc/sysres.inc
index 1d03cd74bf..4b45830537 100644
--- a/rtl/inc/sysres.inc
+++ b/rtl/inc/sysres.inc
@@ -141,10 +141,12 @@ end;
 *****************************************************************************)
 { These functions will call the specified resource manager functions }
 
+{$ifndef Win16}
 Function HINSTANCE : TFPResourceHMODULE;
 begin
   Result:=resourcemanager.HINSTANCEFunc();
 end;
+{$endif Win16}
 
 Function EnumResourceTypes(ModuleHandle : TFPResourceHMODULE; EnumFunc : EnumResTypeProc; lParam : PtrInt) : LongBool;
 begin
diff --git a/rtl/win16/prt0comn.asm b/rtl/win16/prt0comn.asm
index 4586251e42..59a6666be1 100644
--- a/rtl/win16/prt0comn.asm
+++ b/rtl/win16/prt0comn.asm
@@ -27,6 +27,10 @@
 
                 extern PASCALMAIN
                 extern __fpc_PrefixSeg
+                extern __fpc_CmdLine
+                extern __fpc_CmdShow
+                extern __fpc_HInstance
+                extern __fpc_HPrevInst
 
                 extern InitTask
                 import InitTask KERNEL
@@ -67,7 +71,11 @@
 
                 mov ax, es
                 mov [__fpc_PrefixSeg], ax
-                mov [hInst], di
+                mov [__fpc_CmdLine+2], ax
+                mov [__fpc_CmdLine], bx
+                mov [__fpc_CmdShow], dx
+                mov [__fpc_HInstance], di
+                mov [__fpc_HPrevInst], si
 
                 ; call WaitEvent(0) to clear the event that started this task
                 ; Windows expects this call immediately after InitTask
@@ -75,10 +83,10 @@
                 push ax
                 call far WaitEvent
 
-                ; call InitApp(hInst) to initialize the queue and support routines
-                ; for the app. Windows expects this to be the third call in the
-                ; Win16 startup sequence.
-                push word [hInst]
+                ; call InitApp(hInstance) to initialize the queue and support
+                ; routines for the app. Windows expects this to be the third
+                ; call in the Win16 startup sequence.
+                push word [__fpc_HInstance]
                 call far InitApp
                 test ax, ax
                 jz error
@@ -108,7 +116,6 @@ pStackBot:      dw 0
                 ; end of reserved area, filled by InitTask
 
 
-hInst:          dw 0
 
                 segment _STACK stack class=STACK align=16
 
diff --git a/rtl/win16/system.pp b/rtl/win16/system.pp
index 23e5b8b178..23f9f5962d 100644
--- a/rtl/win16/system.pp
+++ b/rtl/win16/system.pp
@@ -13,6 +13,8 @@ interface
   systemh.inc is included otherwise the
   $mode switch is not effective }
 
+{$DEFINE HAS_CMDLINE}
+
 {$I systemh.inc}
 {$I tnyheaph.inc}
 
@@ -52,6 +54,9 @@ const
   64K bytes (BP7 compatibility) }
   SelectorInc: Word = $1000;
 
+type
+  LPSTR = ^Char;far;
+
 var
 { Mem[] support }
   mem  : array[0..$7fff-1] of byte absolute $0:$0;
@@ -66,6 +71,13 @@ var
 { The DOS Program Segment Prefix segment (TP7 compatibility) }
   PrefixSeg:Word;public name '__fpc_PrefixSeg';
 
+{ BP7 compatible windows variables }
+{ In C, these are the parameters to WinMain }
+  CmdLine: LPSTR;public name '__fpc_CmdLine';
+  CmdShow: SmallInt;public name '__fpc_CmdShow';
+  HInstance: Word{HINST};public name '__fpc_HInstance';
+  HPrevInst: Word{HINST};public name '__fpc_HPrevInst';
+
 {  SaveInt00: FarPointer;public name '__SaveInt00';}
 
   AllFilesMask: string [3];