Visual Studio main function

I would like to have my own main function in Visual Studio, to run other threads of legacy C/C++ code. All my code is unmanaged C++/C, some with Windows APIs

I noticed that JUCE uses a macro to create the main() function, and I tried to “lift that out” - but I was unable.

Do anyone have a tip on howto get started with this? Surely it must be possible :slight_smile:

The answer to this question depends on the kind of project you are working on. Is it a GUI application, a command line application or an audio plugin?

I want a command line window running legacy code and a simple gui window, both at the same time :slight_smile:

In contrast to unix based operating systems, Windows GUI applications simply don‘t have a command line as default (which means that also stdout etc. are going nowhere in a GUI application).

You can however explicitly launch a console from your GUI application and can do all kinds of stuff that you would have done in your command line main right after the creation of your console window. This might be your choice if you really need the user to interact with the command line. If you are interessed in going that route, I can search some example code from an older project where I did that.

If you don‘t actually need command line interaction but simply want to execute some functions that were previously executed from your main function you can of course also simply call them from the initialisation functions of your GUI app. Depending on what this functions do you also might want to perform that work async from a new thread in order to not block the GUI.

Last but not least, you can also build your command line app as it used to be without JUCE and start it as a child process from your GUI app.

The best solution depends a lot on what your legacy code is supposed to do

I can change in visual studio to have commandline as default, and all the printf() works.

I just want to launch juce after my own main function

Yes, you can do that. What I wanted to point out is that on Windows you usually decide to either create a command line app or a GUI app and usually not one app that is both at the same time.

However, it’s still not clear to me if you need user interaction via command line or if you just want some thing silently to be executed from your “own main” function before your GUI application launches. In case you don’t need real command line interaction, the initialise function of your juce::JUCEApplicationBase or juce::Application (which inherits juce::JUCEApplicationBase) it the place where you would normally put all setup that you’d find in the main of a typical command line application. The initialise function gets an array of all command line parameters passed, just like the main gets the usual argv and arcg parameters to handle the command line arguments and you can be sure that the GUI won’t start before the code running there has not been finished. So, if you don’t need user interaction on the command line, this would be the function to put that code. Is that a viable solution for you?

One word on the command line parameters passed to initialise: Unlike a usual main where you get an array of all whitespace separated arguments, you’ll get a single string here with the full concatenated command line argument string. You can parse it on your own, tokenize it via the juce::StringArray::fromTokens function, or you can simply ignore it and retrieve the original command line parameters manually via __JUCEApplicationBase::getCommandLineParameterArray()