From many versions of same program.exe name how to determine it's arguments

Pages: 123
Hello

In a Visual Studio 11 C++ program, I have the name of a program such as n49.exe. There "might" be several of these programs running. They have different argument lists.

Question: From a non-n49.exe program, how do I determine the argument list for each program such as n49.exe from within a C++ program?

Thank you,
Michael
Last edited on
Parse the command line arguments you used to start the program.
https://www.learncpp.com/cpp-tutorial/command-line-arguments/
Do you mean, you want to know if an exe was started as, say, "n49.exe arg1 arg2" vs. "n49.exe argA argB", after the fact?
Yes, from my program called "abc.exe", I want to know about the arguments used by "n49.exe" when it started.

"n49.exe" could be started before "abc.exe" or afterwards.

"argv[]" is useless to me because I want information about "other" programs running, not my program.

Michael
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main(int argc, char* argv[])
{
   std::cout << "There are " << argc << " arguments:\n";

   // loop through each argument and print its number and value
   for (int count { 0 }; count < argc; ++count)
   {
      std::cout << count << ": " << argv[count] << '\n';
   }
}

C:\Programming\My Projects\args\Release>args 1 arf 124 Arg4
There are 5 arguments:
0: args
1: 1
2: arf
3: 124
4: Arg4
"argv[]" and "argc" are useless to me. I want information about other programs running, not mine.

Michael
First, the information can't always be reliably obtained, so it should not be the go-to method for vital decision logic. See: https://devblogs.microsoft.com/oldnewthing/20091125-00/?p=15923
[Raymond Chen: "How do I get the command line of another process?"]

Nonetheless, in my experience it is usually reliable.
Check out the project at: https://github.com/sonicmouse/ProcCmdLine
(I have doubts this is the original author of the code, considering the code I copied from is older than it, but it should be what you're looking for) <Edit: Actually, the code is different in a lot of places, so I shouldn't claim this>
Last edited on
That's not C++. I'll try to study it.

But, I would definitely like to know how to obtain the information using C++.

Thanks,
Michael
Last edited on
The C# is just the boilerplate. Look in the actual logic in the .cpp files.
Another possibility is to use Boost.Asio and structure each program to be a mini network server/sender for asynchronous communication between processes.
https://www.boost.org/doc/libs/1_75_0/doc/html/boost_asio.html
I can't use boost because my program doesn't already use it.

Michael
When I copied and pasted the program from

https://stackoverflow.com/questions/9589431/getting-the-command-line-arguments-of-another-process-in-windows

to my program it compiled, but it had username and password information in it which was obviously useless. I started deleting things like that and the paste became really useless to me.

I'm discarding it for now.

Does anyone know the answer to the main question of this thread? How do I from my program determine the probable starting parameters used to start another completely different program on Windows?

Michael
What is the problem with the github link I already gave? Study that instead.
Mikey wants to make things a lot more complicated than needs be, half-arsing his way into the subject.
Why do want to know the argument list? Are these console programs each running within their own console window - or windows programs with their top-level window - or even service programs? How you distinguish between different running instances of the same program varies depending upon what type they are. The easiest - for both a console program running in its own console or a windows program is for the program to set the window title to something unique. Then use standard WIN32 API to find the appropriate window from a title.
Knowing how a program was started can be very useful for creating your own scripts to start the same program on-demand, when there isn't always good command-line documentation available (or even if there is, sometimes it's faster to just see the arguments yourself instead of searching the internet). Also, you don't always have control over what another program sets its title to.
Last edited on
Ganado Wrote: What is the problem with the github link I already gave? Study that instead.


FYI: I downloaded and extracted the zip file at https://github.com/sonicmouse/ProcCmdLine and tried to run it using Visual Studio 2017. It gives me the following error message:


Test ProcCmdLine Library
------------------------

explorer (1772):

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'ProcCmdLine64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

at ProcCmdLine.Win32Native.GetProcWorkingDir64W(UInt32 nProcId, StringBuilder sb, UInt32 dwSizeBuf)

at ProcCmdLine.GetWorkingDirectoryOfProcess(Process proc) in C:\...\Michael789\Desktop\ProcCmdLine-master\DebugProcCmdLineNetFramework\ProcCmdLine.cs:line 113

at DebugProcCmdLineNetFramework.Program.Main(String[] args) in C:\...\Michael789\Desktop\ProcCmdLine-master\DebugProcCmdLineNetFramework\Program.cs:line 23

----- End -----

Anyway, this is a C# project utilizing C# libraries that I don't know how to access from C++. I.E.: It's worthless to me because it would taking me a month, if at all, to port to C++.

At this point, I don't know what to do!
Last edited on
seeplus Wrote: Why do want to know the argument list? Are these console programs each running within their own console window - or windows programs with their top-level window - or even service programs? How you distinguish between different running instances of the same program varies depending upon what type they are. The easiest - for both a console program running in its own console or a windows program is for the program to set the window title to something unique. Then use standard WIN32 API to find the appropriate window from a title.


In a program that monitors other programs, I need to be able to distinguish some of the programs running on the computer. If the same program is running more than once and I know their argument list then I definitely can distinguish between them. I want to add the capability to run the same program many times on the computer and distinguish between them.

Just knowing the PID doesn't help me. I need to compare the argument lists that were used to start each of them.
Last edited on
I'm just repeating myself now. The C# code is just a shell/interface that calls into the C++ code. The information that you are looking for is within the .cpp (C++, not C#) files of the github project. Look at the GetProcessParameter function. Again, it is C++.
Pages: 123