New here: Should I start with freebasic?

New to FreeBASIC? Post your questions here.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

New here: Should I start with freebasic?

Post by newbieforever »

I posted the above question in my AHK forum I received the answer:
"https://freebasic.net/ The output exe is about 25 kb."

This sounds good. Could the following be done, for a newbie like me, in freebasic?

============

I am using a compiled AHK script "Launcher.exe" which just runs another exe and passes the CL parameters to it. But this Launcher.exe is an executable of about 400 kB size, of course. My problem is: I would need an executable with a size of < 50 kB. (Please don't ask what is the background of this whole setting, it makes sense in my project.)

Used e.g. in a Windows shortcut:

Code: Select all

"Launcher.exe" "/run:My Exe.exe" "Par 1" "Par 2" [...]
To put it in the simplest possible way: Even a simple BatchFile.bat with the content:

Code: Select all

start "" %1 %2 %3 %4
could be run in DOS it this manner:

Code: Select all

C:\>BatchFile.bat "" "My Exe.exe" "Par 1" "Par 2" [...]
and would run the 'My Exe.exe' with the specified parameters.

That is exactly what I would need, but:
– as an executable
– with a size of < 50 kB
- supporting Unicode characters in file names and parameters (!)
- running on every Windows machine (no .NET dependency or...)

My specific question:
– Does someone here know such a tool (it must be open source)?
- If not, in which programming language such a tool could be written in the easiest way?
In both cases there should be a free compiler available to compile the code.

NOTE: I'm more than amazed that such a tool
– can easily be written in AHK* and
– the same functionality is provided already by an one-line batch script**,
but the same seems to be hardly to implement in a higher-level programming language (mainly because of the Unicode support?).
Or am I wrong?


* ... executable too big
** ... without Unicode support and not as an executable
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: New here: Should I start with freebasic?

Post by St_W »

Of course one can accomplish that with FreeBasic.
newbieforever wrote:running on every Windows machine (no .NET dependency or...)
Every supported Windows version has .NET pre-installed. So You can use .NET just like you use the Win32 API.
And btw the Windows Command Prompt (cmd.exe) does support Unicode.
To be honest your requirements just seem to be foolish, but if you're willing to learn rather easy to accomplish.
So I'd suggest to not stick to your nickname and stay "newbie forever", but to start learning how to implement software properly.

I you want try FreeBasic start reading here: https://msdn.microsoft.com/en-us/librar ... 82425.aspx (and make sure to use the Unicode-Version of the function)
If you need general information on how to program or how to use the Win32 API our wiki and this forum provide a lot of examples.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: New here: Should I start with freebasic?

Post by newbieforever »

I appreciate your answer very much, St_W, (although you almost sound a bit strict to me, you have to be German ;-) (sorry!)), thank you very much!

You may be quite right in terms of Unicode & Batch. I've probably thought that batch files must be stored exclusively as ANSI, so they can not contain Unicode characters. Maybe you'll agree with me.

You will not believe me, but in the context of my project the requirements are really not foolish...

.NET Framework: I just installed Windows 10 on a machine today, and there definitely was not any .NET Framework on it. But that's not my point. Can you specify your answer regarding .NET? Are freebasic programs dependent on .NET or not?

Thanks for the advice where to start! Should anyone have a more specific indication of what such a Launcher-tool code would look like in freebasic, I will be happy!

Freebasic is portable, I understand that correctly? I think that's extremely nice. Maybe it will become my constant companion to write help tools that I need for my AHK projects... The beginning of a wonderful friendship!?
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: New here: Should I start with freebasic?

Post by badidea »

Maybe I misunderstand the question/goal, but can this be made with Command and Shell? Or is this a problem with the Unicode requirement?
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: New here: Should I start with freebasic?

Post by newbieforever »

badidea wrote:Maybe I misunderstand the question/goal, but can this be made with Command and Shell? Or is this a problem with the Unicode requirement?
In my case I definitely need an executable!
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: New here: Should I start with freebasic?

Post by jj2007 »

> "Launcher.exe" which just runs another exe and passes the CL parameters to it.

The task is indeed a bit weird, but it could be done in Masm32 at 1024 or 2048 bytes, using WinExec. But even in FreeBasic it will probably be less than 50k.
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: New here: Should I start with freebasic?

Post by PaulSquires »

I am not sure if I am understanding your needs 100% but I quickly threw this code together. No error checking or fancy analysis of each incoming command line parameter but it should work. No optimization at all.

FreeBasic 1.06 (32-bit).... 28K
FreeBasic 1.06 (64-bit).... 29K

Code: Select all

' Launcher
'
' "Launcher.exe" "/run:My Exe.exe" "Par 1" "Par 2" [...]
' start "" %1 %2 %3 %4
' C:\>BatchFile.bat "" "My Exe.exe" "Par 1" "Par 2" [...]
'

' Must allow for unicode, therefore use WSTRING

' Read the incoming command line parameters. Assume that the first
' parameter is the name of the program to launch.

dim wszProgName as wstring * 250 
dim wszArguments as wstring * 10000  
dim wszArg as wstring * 250
dim DQ as wstring * 4 = chr(34)   ' double quotes

Dim As Integer i = 1
Do
    wszArg = Command(i)
    If Len(wszArg) = 0 Then exit do

    ' Wrap the argument in double quotes to handle embedded spaces in argument
    wszArg = DQ & wszArg & DQ
    
    if i = 1 then
       wszProgName = wszArg
    else
       wszArguments = wszArguments & " " & wszArg
    end if
    i += 1
Loop

' Transfer control over to an external program. When the program 
' exits, execution will return to the system.
Run( wszProgName, wszArguments )

Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: New here: Should I start with freebasic?

Post by Josep Roca »

AFAIK "Run" does not support unicode. Better use ShellExecuteW.

"Command" also does not support unicode.
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: New here: Should I start with freebasic?

Post by PaulSquires »

Josep Roca wrote:AFAIK "Run" does not support unicode. Better use ShellExecuteW.

"Command" also does not support unicode.
This one uses all Win32 api and weighs in at 24K. It is unicode aware.

Code: Select all

' Launcher
'
' "Launcher.exe" "/run:My Exe.exe" "Par 1" "Par 2" [...]
' start "" %1 %2 %3 %4
' C:\>BatchFile.bat "" "My Exe.exe" "Par 1" "Par 2" [...]
'
#Define UNICODE
#Include "windows.bi"
#Include "win\shellapi.bi"

' Must allow for unicode, therefore use WSTRING

' Read the incoming command line parameters. Assume that the first
' parameter is the name of the program to launch.

dim wszProgName as wstring * 250 
dim wszArguments as wstring * 10000  
dim DQ as wstring * 4 = chr(34)   ' double quotes

' FB's COMMAND() statement is not unicode aware. Use Win32api instead.

dim szArglist as wstring ptr ptr
dim nArgs as Long
dim i as long

szArglist = Cast(wstring ptr ptr, CommandLineToArgvW(GetCommandLineW(), @nArgs))
if szArglist then
   for i as long = 1 to nArgs - 1
     
    if i = 1 then
       wszProgName = DQ & *szArglist[i] & DQ
    else
       ' Wrap the argument in double quotes to handle embedded spaces in argument
       wszArguments = wszArguments & " " & DQ & *szArglist[i] & DQ
    END IF
   
   NEXT

end if

' Transfer control over to an external program. When the program 
' exits, execution will return to the system.
Dim ShExecInfo As SHELLEXECUTEINFOW  

' Run the EXE
With ShExecInfo
   .cbSize       = Len(SHELLEXECUTEINFOW)
   .fMask        = SEE_MASK_DEFAULT 
   .HWnd         = 0
   .lpVerb       = Null
   .lpFile       = @wszProgName
   .lpParameters = @wszArguments   
   .lpDirectory  = 0
   .nShow        = SW_SHOW
   .hInstApp     = 0 
End With
ShellExecuteEx(@ShExecInfo)

newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: New here: Should I start with freebasic?

Post by newbieforever »

I am overwhelmed! I have not experienced such a reception in a forum for a long time! (Often enough, it's the opposite of it.) Are you always so nice to newcomers?

YES, PaulSquires, YOUR LAUNCHER WORKS PERFECTLY! FULL UNICODE SUPPORT!

I think the next few weeks I will have to deal with freebasic. It seems to be exactly what I was looking for a long time. However, without this kind of help (PaulSquires and the others) I would not have realized that!

I often need such small-exe utilities. Incidentally, in the case of the launcher ('foolish', 'weird'), this launching function is only the base, the main part is a completely different function, and I'll deal with that for the next few weeks (it's about reading, processing and writing binary data).

I allow myself three additional questions:
- Can the black console window be prevented?
- Is freebasic (or PaulSquires code) .NET independent or not?
- This is a bit rude now, I know, but then I really would have the complete basis: What would the processing of the first parameter in the notation '/run:' look like? (I'll use a lot of such parameters later.)

Best regards,
newbieforever
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: New here: Should I start with freebasic?

Post by PaulSquires »

newbieforever wrote: I allow myself three additional questions:
- Can the black console window be prevented?
- Is freebasic (or PaulSquires code) .NET independent or not?
- This is a bit rude now, I know, but then I really would have the complete basis: What would the processing of the first parameter in the notation '/run:' look like? (I'll use a lot of such parameters later.)
To prevent the console window from showing, compile using the -s gui compiler option. Check out the FB help file for info on how to specify the different compiler options when compiling your code.

FB has zero dependence on .NET. The code I posted is 100% self contained and does not depend on any other runtime library.

Here is the "/run:" code... not tested because I am writing this post online away from my development computer.

Code: Select all

' Launcher
'
' "Launcher.exe" "/run:My Exe.exe" "Par 1" "Par 2" [...]
' start "" %1 %2 %3 %4
' C:\>BatchFile.bat "" "My Exe.exe" "Par 1" "Par 2" [...]
'
#Define UNICODE
#Include "windows.bi"
#Include "win\shellapi.bi"

' Must allow for unicode, therefore use WSTRING

' Read the incoming command line parameters. Assume that the first
' parameter is the name of the program to launch.

dim wszProgName as wstring * 250
dim wszArguments as wstring * 10000 
dim DQ as wstring * 4 = wchr(34)   ' double quotes

' FB's COMMAND() statement is not unicode aware. Use Win32api instead.

dim szArglist as wstring ptr ptr
dim nArgs as Long
dim i as long

szArglist = Cast(wstring ptr ptr, CommandLineToArgvW(GetCommandLineW(), @nArgs))
if szArglist then
   for i as long = 1 to nArgs - 1
     
    if left(szArglist[i], 5) = "/run:" then
       wszProgName = DQ & mid(*szArglist[i], 6) & DQ
    else
       ' Wrap the argument in double quotes to handle embedded spaces in argument
       wszArguments = wszArguments & " " & DQ & *szArglist[i] & DQ
    END IF
   
   NEXT

end if
LocalFree CAST(HLOCAL, szArglist)
 
' Transfer control over to an external program. When the program
' exits, execution will return to the system.
Dim ShExecInfo As SHELLEXECUTEINFOW 

' Run the EXE
With ShExecInfo
   .cbSize       = Len(SHELLEXECUTEINFOW)
   .fMask        = SEE_MASK_DEFAULT
   .HWnd         = 0
   .lpVerb       = Null
   .lpFile       = @wszProgName
   .lpParameters = @wszArguments   
   .lpDirectory  = 0
   .nShow        = SW_SHOW
   .hInstApp     = 0
End With
ShellExecuteEx(@ShExecInfo)

newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: New here: Should I start with freebasic?

Post by newbieforever »

PaulSquires, you are a schatz!
Last edited by newbieforever on Jun 24, 2018 16:09, edited 2 times in total.
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: New here: Should I start with freebasic?

Post by St_W »

newbieforever wrote:.NET Framework: I just installed Windows 10 on a machine today, and there definitely was not any .NET Framework on it. But that's not my point. Can you specify your answer regarding .NET? Are freebasic programs dependent on .NET or not?
FreeBasic is not dependent on .NET and actually doesn't have any relation to .NET; I just wanted to point out that your reason for not using .NET does not really make sense. And in Windows 10 .NET 4 is an integral part of the system that can't be even uninstalled (isn't even listed in programs or windows-features lists), so you definitely had .NET on your Windows 10 system although you might didn't "see" it. btw here you can find a good overview of .NET versions included by default with specific Windows versions: https://blogs.msdn.microsoft.com/astebn ... of-the-os/
newbieforever wrote:Freebasic is portable, I understand that correctly?
Yes, it is portable; but of course portability also depends on the code you write. For example if you write code that uses the Win32-API your applications won't work on Linux for obvious reasons. Unfortunately many FB built-in methods don't support Unicode properly so you have to resort to the Win32-API (on Windows) if you need that. You've seen that in the code above that used e.g. ShellExecuteEx, among others.
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: New here: Should I start with freebasic?

Post by marcov »

St_W wrote: I just wanted to point out that your reason for not using .NET does not really make sense. And in Windows 10 .NET 4 is an integral part of the system that can't be even uninstalled (isn't even listed in programs or windows-features lists), so you definitely had .NET on your Windows 10 system although you might didn't "see" it
Having "a" .NET on all supported Windows versions, doesn't necessarily mean you can deploy one .NET program to all supported Windows versions. Some have 2.0-3.5, some have 4.0
St_W
Posts: 1619
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: New here: Should I start with freebasic?

Post by St_W »

marcov wrote:Having "a" .NET on all supported Windows versions, doesn't necessarily mean you can deploy one .NET program to all supported Windows versions. Some have 2.0-3.5, some have 4.0
That's true, but it's not hard to make your .NET application compatible with both 2.0-3.5 and 4.0 runtimes - especially if it's such a simple application like the one request by the thread's author.
And features also differ between different versions of the Win32-API or other APIs.
Post Reply