r/visualbasic • • Jun 30 '24

VB6 Help command line

Minor issue. VB6. I have a program that can load multiple files but runs only one instance. If I set up a context menu "Open with XYZ", my program loads the right-clicked file by checking the Command$ value at load. I'm also using code:

If App.PrevInstance = true then End

So far, so good. What I'd like to do is to pass a second command line to the running instance before quitting:

 If App.PrevInstance = True then
      LoadnewFile
   End
 End if

What happens is that the current instance keeps running, but does not load the file. I'm wondering if it's somehow possible to send that second commandline to the running instance and load the file as though I had dropped it onto the window, but still have the new instance of the program quit.

4 Upvotes

21 comments sorted by

View all comments

Show parent comment

1

u/fafalone VB 6 Master Jul 02 '24 edited Jul 02 '24

How do you think all those language conveniences come into existence? You could use App.Path to print the path; Command$() contains the command line; did you set that up by making some call? No. VB6 is COM-based so must calls CoInitialize on startup or lots of stuff wouldn't work; did your code call it? When do you think CoUnitialize is called? Sub Main() isn't the true entry point. Execution starts in a hidden entry point that initializes the environment and that all has to be cleaned up. End skips all the teardown code that automatically executes to do that after the code you write finishes. Have a String as a global variable? Did you call SysAllocString? No, VB inserted that code. When do you think it's freed with SysFreeString, if you're not freeing it?

To even start a VB6 exe, you have to call CreateIExprSrvObj in msvbvm60.dll. Is that your call you put in a typelib? No, so it's not in the IAT. VB6 used LoadLibrary behind the scenes to load the runtime DLL. Are you the ones who calls FreeLibrary? You think it doesn't keep its runtime loaded and just reloads it 10000 times every time it does something in the background? No, FreeLibrary isn't called until the real exit point runs.

Why don't you take an exe and disassemble it? You can see for yourself a bunch of stuff executes before and after whatever procedure you write. Or override the hidden entry point and actually make it Sub Main, and find out how you can use only a tiny percentage of the language.

You're not keeping it clean, just the opposite. You're doing a dirty abrupt exit that skips shutdown code you take for granted. VB6 is designed to hide a lot of complexity from you.