Expose WinMain parameters as predefined constants

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Expose WinMain parameters as predefined constants

Post by Tipperton »

In a C/C++ program you'd define the main function as:

Code: Select all

long main(unsigned long hInstance, unsigned long hPrevInst, char *lpszCmdLine, long nCmdShow)
to have access to the parameters Windows passes to the program, since PureBasic doesn't use a main function, it would be nice if these parameters were exposed as predefined constants so we could have access to them as well if needed.

Another way would be to allow us to define our own main function, like this:

Code: Select all

Procedure.l main(hInstance.l, hPrevInst.l, lpszCmdLine.s, nCmdShow.l)
PureBasic would behave normaly, but if you define this main function, PureBasic's startup code would call it instead of the first line of code outside any procedures.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

You do know about the ProgramParameter() function right? I don't get why you would need a "main function" really.
I like logic, hence I dislike humans but love computers.
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

I know about ProgramParameter function and I also know that you can get hInstance through the Windows API with GetModuleHandle_(0), but so far I haven't figured out an easy way to get the two parameters.

I just think it'd be nice if PureBasic made them readily available as other languages do.

And no, a main function isn't needed, I just presented that as an option, predefined constants would work fine too.

Code: Select all

#INSTANCE
#PREVINST
#CMDLINE
#CMDSHOW
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Aha, okay, then I agree :)
I like logic, hence I dislike humans but love computers.
Edwin Knoppert
Addict
Addict
Posts: 1073
Joined: Fri Apr 25, 2003 11:13 pm
Location: Netherlands
Contact:

Post by Edwin Knoppert »

For dll's pb does pass the minimal info like hinstance.
For your exe you can use GetModuleHandle_( "" ) which imo is a poor way of doing this.
The instance handle *should* be present somehow imo.

The winapi previnstance variable is obsolete in win32.

To know the showcommand you can use GetStartupInfo_()

So it is solvable but if it's nice..?
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

So something like this:

Code: Select all

hInstance.l=GetModuleHandle_(0)
hPrevInst.l=0
GetStartupInfo_(info.STARTUPINFO)
nCmdShow.l=info\wShowWindow
lpszCmdLine.s=""
n.l=CountProgramParameters()-1
For i.l=0 To n
  If i
    lpszCmdLine+" "
  EndIf
  p.s=Trim(ProgramParameter(i))
  If Len(p)=0
    lpszCmdLine+#DOUBLEQUOTE$+#DOUBLEQUOTE$
  ElseIf FindString(p, " ", 1)>0
    lpszCmdLine+#DOUBLEQUOTE$+p+#DOUBLEQUOTE$
  Else
    lpszCmdLine+p
  EndIf
Next i
I know the hPrevInst and the lpszCmdLine stuff isn't really needed, just showing it for completeness of what Windows would pass to a program.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

This is win only, PureBasic isn't win only.
For what should this usefull?
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

ts-soft wrote:This is win only
So are the Windows API calls like GetModuleHandle_() and GetStartupInfo_().
ts-soft wrote:PureBasic isn't win only.
So we should remove anything that is platform specific from it?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Tipperton wrote: So we should remove anything that is platform specific from it?
No, for this is the API.
The Values only usefull for API, so you can find the values with API.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

OK

Although this feature would be more convenient, I'll just save the code that gets the information in a template and just add it to any project where it's needed.

I didn't know about the GetStartupInfo_() call before as a way to get nCmdShow so I thought adding this feature was the only way to get it.

Moderators, you can move this topic to Windows programming if you like.
Post Reply