It seems very simple to do that from .NET - such as this question: Running MSBuild at runtime.
But my program is C++.
I want to find the easiest way of doing this trans world connection easily.
Calling .Net from C++ has never been my thing before.
The question that you link to, Running MSBuild at runtime, is C# code using the ProcessStartInfo class. But you don't need to "call .Net from C++"; you can use the Win32 API directly. Essentially ProcessStartInfo is wrapping CreateProcessW. See "Creating Processes" for an overview.
Related
We have written an ActiveX application in VB 6.0 which hosts in VC++ dll. We want to debug that code using the VB classic IDE. We are unable to debug it, because it does not hit the breakpoint we have set. How do I debug an ActiveX application using the VB classic IDE?
If you start your ActiveX DLL project in the VB 6 IDE, and then create components from some other application (your C++ application, or testing it via another VB Project or any other language which can use the COM object), it should load the object in the debugger and you can set breakpoints and look at variables while running. Refer to "Testing and Debugging ActiveX Components" in the Visual Basic 6 Concepts Guide.
If that's not working for you, can you make a MCVE of the problem you're having, by creating a new simple ActiveX DLL component and calling it from a separate project (you may want to try calling it from more than one language), and reproduce the issue you're having to clarify what isn't working for you?
You probably have "Break on Unhandled Errors" selected in the IDE. If you do, then any error in your ActiveX object will set the breakpoint on the line of the client that accesses the object, typically a method call. If this is the behavior that you are getting ("does not hit the breakpoint that we have set" doesn't make this clear), then that is almost certainly the problem.
In any case, I would suggest that you check this. You should have "Break in Class Module" selected. For a full explanation of the different ways to handle breakpoints, and how to change the settings, see this post. Here also is the doc on the subject.
I made a program in c++ (visual studio 2010) that looks for serial com ports and compares their friendly names with defined text. When there is a match that port is opened/connected and serial communication starts.
The program notifies the user when a com port is found, whether the connection was successful or not and if the data send was successful or not and other useful information. The program uses cout to notify the user.
I want to replace console output window with windows form but cant find much resources online on how to do this. To illustrate, I want this:
To become this:
I included form1.h and other files and tried replacing cout with below line but code is not compiling:
Form1::textBox1->Text = L" Text I want to display";
Can anyone explain how to use textBox1, or a tutorial for this?
"I included form1.h" - You can't just grab random files and hope it works. That is not how C++ works, or computers in general.
How then do you do something like this? The Standard Library provides std::cout and Visual Studio by default includes the Standard Library, so using it is fairly easy. But for graphics, you will need another library. I recommend Qt, if only because there are good tutorials for beginners.
So I finally did achieve the functionality I described above in my question and thought I should post my findings here.
To convert my code from console output to Windows Form I basically had to migrate from c++ to C++/cli.
holowczak.com has a great tutorial on how to get started with windows form (c++/cli) in visual studio.
Next if there is any busy looping(like infinite while-loop) in your c++ code then you would need to run that busy loop on a separate thread or the program can hang. Dr.Dobb's tutorial on how to create and mange threads in c++/cli, can help a lot.
Finally, if you need to access resources (such as textbox and other controls) of windows form from another thread then a thread-safe call has to be made. Microsoft's "HowTo:Make thread-safe call to windows Form controls" explains how the invoke method can be used for updating textbox from another thread.
I have a library with classes and public methods written in C++.
I would like, from inside an interactive program written in C++ and Qt, to send commands to a parser in Python language which in turn converts them to call to methods and functions of my library.
Something similar to what is done in Octave/Matlab, a string is processed by a parser which then executes internally the commands.
Somewhere in my C++ library I have a function
int myFooCPPfunction(int value)
{
return value*value;
}
then during the execution of my program, I want to start a console and type in Python syntax:
for i in range(0,20):
print("%d" % myFooCPPfunction(i))
The command I gave then updates the internal state of my program for example.
I think it is a matter of writing Python code that links to C++. It seems to me that things like boost::python already do it...I ask your suggestion on which is the better way to write the bindings.
Second point: how to integrate that thing in an interactive shell launched from a Qt application?
Some online projects like QConsole should do something similar, but QConsole appears to be a very outdated project.
Thank you!
PythonQt was created for exactly this purpose.
Ok so I am learning C++ slowly. I am familiar with all the console syntax and everything, but now I'm moving on to windows programming. Now what im trying to do, is create a DLL that I inject into a process, so it's hooked in. All I want the C++ application to do, is have text in it, that says "Hooked" if it's successfully injected, and an error if something wrong happened. Or even if I can do it without a DLL, Just open an executable, and when the certain process I'm trying to hook is opened, the status is changed to "Hooked". Also I have a safaribooksonline.com account so if there is any good reads you would recommend, just write it down. thanks
I think you might be looking at this backwards. In C/C++ an application 'pulls' a DLL in rather than having a DLL 'injected' into an application. Typically for plugins/hooks, there is some mechanism to inform an application of a DLL's availability (often just its presence in a specific directory) and a configuration file or some other logic is used to instruct the application to explicitly load the library, extract a function or two, and call them.
For Windows programming, I'd suggest doing a search for examples of the LoadLibrary() API call. You'll likely find a tutorial or two on how to do it.
If by "hooked" you mean, "have my DLL run in that processes' address space", you want CreateRemoteThread(). This is fairly advanced and difficult to debug, because your bugs make the other program crash. It's how a lot of malware works, by the way.
If you mean "have my DLL get notified of activity in the other process", you want SetWindowsHookEx().
Sounds like you want to inject as soon as the application starts? You can do that with Microsoft's Detours DetourCreateProcessWithDll(). Example here.
We have a command line application that could benefit from a GUI. We want to add some plotting functionality and have identified a plotting library that uses MFC. Initially we developed a separate app, but we'd rather have the GUI in the same process space.
I was thinking of possibly a GUI in an MFC DLL that could be hosted in the production app AND in a testing app.
The questions are:
What are the steps necessary to add an MFC GUI to a win32 command line app
Is it possible to make a GUI in an MFC DLL and how can it be done? (so that different apps can reuse the same GUI)
EDIT
I should add that this is an unmanaged app (and needs to stay that way - it needs to be highly performant, makes extensive use of templates, boost, custom allocators, internally developed thread serialization, etc)
RESULTS:
Nick D's answer worked great - especially the follow-up link in his comment with the details about a regular MFC DLL.
Note that we will be using Qt for the next iteration. Modifying our build environment and getting used to a a new framework was just too much this time around.
You can call/reuse GUI code in a dll. (I even use Delphi forms in my C++ projects)
A very simple dll example:
// The DLL exports foo() function
void foo()
{
AFX_MANAGE_STATE( AfxGetStaticModuleState() );
CDlgFoo dlg;
dlg.DoModal();
}
In the console program you'll have code like this:
h = ::LoadLibrary( "my.dll" );
::DisableThreadLibraryCalls( h );
pfoo = (foo_type*)::GetProcAddress( h, (const char*)1 );
if ( pfoo ) pfoo();
First, you will have to surrender WinMain().
If you still want to retain the command-line arguments functionality, process command arguments in InitInstance() of your App class.
The straight forward approach would be to add a switch to your program and given a certain value it will launch the gui, otherwise use the command line options. Something like "app.exe -mode=gui". If you don't see this command arg on program launch, fall back to the old command line behavior.
Regarding the DLL, you could write all the UI functionality in a DLL and use it from your "production app" where you have a message loop running and a WinMain. But what's the point? If it's for testing purposes why not just separate the presentation from the logic and test the logic alone. How do you intend to test the UI in your test app anyway? Simulate button clicks?