InterviewSolution
| 1. |
Solve : [C++]copy text from command prompt? |
|
Answer» 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?Tell more about you objective from a huger level. 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.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? 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 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 |
|