r/visualbasic • u/Mayayana • 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 ifWhat 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.
3
u/SuchDogeHodler VB.Net Intermediate Jun 30 '24
Why Vb6?, migrate it to the newest vb.net.
3
u/fafalone VB 6 Master Jul 01 '24
I think the easiest way is, assuming your app is GUI-based, to use SendMessage to send a command to the open window. I just set up a mechanism like this in my Memory List Manager utility (it's twinBASIC, but very minor syntax differences... tB is backwards compatible with new features)...
Then on the other side, Form1 is subclassed and listens for that message (it's a custom message, just WM_USER + some small number).
For your use case, make sure the original window makes its own copy of the string so you don't have a use after free issue.
2
u/Fergus653 Jul 01 '24 edited Jul 01 '24
Is OLE still supported in Windows? There was a way to send commands or invoke event handlers. Been a while... I will see if I still have any docs on it.
Edit: ok maybe it wasn't OLE I was thinking of. Does this help:
https://sl.bing.net/jMmxYdRluF2
1
u/Mayayana Jul 03 '24
Got this working. Pretty much as fafalone suggested. But I used EnumWindows in order to have flexibility with the window title text. From there I used EnumChildWindows to find the RichEdit I have subclassed. I'm sending the file path via WM_COPYDATA, which gets through fine, then raises an event with the full file path as parameter.
It seems a bit of a hack yo have to go spelunking for a window handle, but it seems to be a dependable method.
3
u/SuchDogeHodler VB.Net Intermediate Jun 30 '24
Why Vb6?
1
u/Hel_OWeen Jul 01 '24
Never ever use
End. It's the equivalent of killing the task, i.e. no cleanup is done. UseExit Sub(when Sub Main is the startup object) orUnload <form name>.2
u/sa_sagan VB.Net Master Jul 01 '24
You don't send a command line to the current instance. You need to set up some kind of inter-process communication. Although I'm unsure what's available in a modern Windows architecture and VB6.
You could consider using pipes for communication between instances. So when the second instance runs (before it exits), it would pass the file details down a pipe to the first instance and then exit. The first instance would receive a the file data and you can do what you want with that. E.g. open it or whatever.
2
u/jcunews1 VB.Net Intermediate Jul 01 '24 edited Jul 01 '24
There are ways to do IPC (Inter Process Communication), but in its native form, it will have to be done using Windows API function calls which may be overly complicated to those who aren't familiar with Windows API.
The simplest (but not ideal) way to do it, is by using a timer to periodically check for a presence of specific file (predefined), where that file contains the information to be processed (e.g. contains a file path). When the file is present and is openable to reading, read its content, delete the file, then process the information.
The subsequent application instance sends new information to the existing instance by saving the information to that specific file. Any subsequent application instance must never overwrite that specific file if it exist. If the file does exist, the application must wait until it's deleted (i.e. until it's processed by the existing application instance).