Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [C++]copy text from command prompt  (Read 8548 times)

0 Members and 1 Guest are viewing this topic.

Boozu

    Topic Starter


    Hopeful

    Thanked: 9
    • Yes
    • Yes
  • Certifications: List
  • Experience: Familiar
  • OS: Windows 10
[C++]copy text from command prompt
« on: July 18, 2014, 07:28:47 PM »
Hello everyone. It has been long time. I am trying to make a program to copy a mac address from the command prompt to the clipboard so I can easily ctrl-v it were I need. My first hurdle is finding a way to put the output of the command prompt into a string so I can fiddle with the info and find exactly what I need. Does anyone know a way to get the info from the cmd to a string?
Don't worry about it.  If it's not good at stock, then it's not good.


Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [C++]copy text from command prompt
« Reply #1 on: July 18, 2014, 07:46:35 PM »
Tell more about you objective from a huger level.
Why do you need this?
Is your objective to learn more about C++

What command would you use at the command prompt?
Why not just use what is already in the C++ library?

Are you familiar with any script language?
Pearl? Python? VBScript?

Boozu

    Topic Starter


    Hopeful

    Thanked: 9
    • Yes
    • Yes
  • Certifications: List
  • Experience: Familiar
  • OS: Windows 10
Re: [C++]copy text from command prompt
« Reply #2 on: July 18, 2014, 09:40:50 PM »
I am working on cloning large groups of computers(laptops) and part of the processes requires us to copy the mac address of the wireless card into a program for registration with our server. Normally we use the command
Code: [Select]
getmac /v then copy the correct address and past it into the program. I am trying to make it so that we do not need to manually go to the command prompt>right click>click mark> highlight the correct mac address>copy. Right now we have the getmac command built into a larger batch file so I am trying to make it so the batch just calls the exe and then all we do is ctrl-v were we need. We are working on a few hundred laptops now and this would a big help in the future when we have to do more. Someone else has tried to do it just with batch but it only works with perfectly identical computers. I do not know if batch is powerful enough to do what we need but if it is then that would work fine as well. The main idea I was looking for was:
Code: [Select]
contents of system("getmac /v")>>string1
search string1 for the word "Wireless"
skip x characters*1
copy the next 17 characters to string2*2
string2>>clipboard

*1 x is the characters in between the word wireless and the beginning of the mac address.
*2 17 is the length of the mac address. If I counted correctly
I am partially doing this to learn more myself but it also makes this big job easier. I am not sure what you mean by
Why not just use what is already in the C++ library?
I did intend to only use c++ of this program. Perhaps my earlier explanation of what I am trying to do will clarify that. I am not familiar with any of the languages you mentioned. Any suggestions or if you need more info please let me know.
Don't worry about it.  If it's not good at stock, then it's not good.


Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [C++]copy text from command prompt
« Reply #3 on: July 18, 2014, 10:21:16 PM »
There are a number of ways to get MAC address using C++ along with other tools.
If you are  using Windows, this link may NOT help:
How to get MAC Address in Windows with C++
The moderator did not think it was a good question.

But here is a link where a more generalized answer was given.
Three ways to get your MAC address.

Here is one that tries to use a function.
How to get mac address of pc via c++ function?
Does that help any?  :)



BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: [C++]copy text from command prompt
« Reply #4 on: July 18, 2014, 10:33:13 PM »
You can capture the standard output of a program by redirecting it to your own handle.

C++ and C libraries do not, as far as I'm aware, expose this capability. You would need to directly access the Windows API functions. In this case, the applicable function is CreateProcess(). You pass in a PROCESS_START_INFO structure, and that structure can include setting a handle on the standard output.

The usual approach is to create a new stream and redirect the standard output of the child process to that stream. Then the parent can read from that stream until the EOF and it will have the standard output of the program. If the program requires standard input (eg text-based prompts or something) those can be written to a redirected standard input.

I don't have an example in C/C++ and personally think it would take far too long for me to come up with one that actually works to make it worth it. Here is a C# example though:

Code: [Select]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;

namespace testredirection
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            String strFile = "getmac.exe";
            String strarguments = "/v";


            ProcessStartInfo psi = new ProcessStartInfo()
            {
                FileName = strFile,
                Arguments = strarguments,
                RedirectStandardOutput = true,
                WindowStyle=ProcessWindowStyle.Hidden
            };
            psi.UseShellExecute = false;
            Process p = Process.Start(psi);
            String stdouttext = p.StandardOutput.ReadToEnd();
            Console.WriteLine("Standard output retrieved:\n" + stdouttext);
            Clipboard.SetText(stdouttext);
        }
    }
}
After I run the above C# code I end up with getmac /v output on my clipboard. Obviously a program that does additional parsing can easily parse that string and only set the appropriate text, as per the instructions you gave.

If it needs to be or would be preferred to be in C++/C, you would need to include Windows.h and you would use the PROCESS_START_INFO structure in combination with CreateProcess. You would then create a pipe (using CreatePipe) and set the hStdOutput member of PROCESS_START_INFO to point at the pipe you created, the nread from the Pipe until EOF and use that as the process output. Once you have the output you would then open the clipboard and set the text to it. as we can see here doing the same thing in C or C++ is a bit more complex, but perhaps you can use that as a starting point.
I was trying to dereference Null Pointers before it was cool.

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: [C++]copy text from command prompt
« Reply #5 on: July 20, 2014, 11:47:25 AM »
I work with C++ regularly. What I would do is the following:

You can run getmac /v >whatismacaddress.txt

This will write the information that is normally given to display to a text file.

You can then have the C++ program read in the text file and parse out the unnecessary info and have a clean mac address to work with.

You can then pass that mac address to your program, however as BC stated and offered a C# alternative, C++ is more complex.

If I was given this situation I would actually use a keyboard/mouse macro that is compiled as an EXE that can be called from the batch file you already created. However personally I'd use BC's method that uses C# as for the macro method that I would do would cost money for the macro software.

For stuff like this that is something that I wouldnt want to spend a whole day coding up, i would create a simple macro in 15 seconds and compile it and use that to achieve the same goal. The macro software I use is JitBit Macro Recorder http://www.jitbit.com/macro-recorder/

If this was going to be something created for a client though, I'd charge and take the time to do it as clean and professional as possible, but for a down and dirty quick low cost method, i'd just go with this macro software that I already own a license of and create the macro and call that compiled macro from a batch file etc.

Here is the whatimacaddress.txt file that my Windows 7 64 bit system created, and you would get a similar output to the text file.


Quote
Connection Name Network Adapter Physical Address    Transport Name                                           
=============== =============== =================== ==========================================================
Local Area Conn Realtek PCIe FE 00-25-11-67-76-84   \Device\Tcpip_{A06B0A71-13D4-461E-BAD9-631C1088BE88}