skip to Main Content

I have a simple AI program I have made and I want to add the command “add command” which would actually chance the source code from the executed program. Is there anyway to do this? I was thinking I would have pre-built strings with the blocks of code I have for commands such as:

else if (input == "what are you?" || input == "What are you?")
            {
                cout << "I am a multi-purpose, artificial intelligence program designed to help simplify life." << endl;
                ISpVoice * pVoice = NULL;

                if (FAILED(::CoInitialize(NULL)))
                    return FALSE;

                HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
                if (SUCCEEDED(hr))
                {
                    hr = pVoice->Speak(L"I am a multi-purpose, artificial intelligence program designed to help simplify life.", 0, NULL);
                }
            }

And just make the command ask for input possibilities, and output.

Is this something that is doable or is this just my inner StarTrek fan taking over my practicallity?

3

Answers


  1. C++ is compiled, not interpreted. So, you have basically no hope of changing the code after the compilation happens.

    However, you can use callbacks / delegate patterns as well as passing pointers to functions and classes around…

    Login or Signup to reply.
  2. Yes, you can change your programs execution during run-time. These are known as hooks.

    For example, when an OS receives an event, it iterates through it’s list of “hooks”, notifying them of the event.

    Some hooks can be appended while other hooks are daisy-chained.

    Take a keyboard driver for example. Let’s assume there is a vector that is called when a keypress occurs. An installer saves the address in the vector and writes the address of the driver into the vector. The installer then gives the original address from the vector to the driver. This gives the driver first preference over keypress. The driver then calls the address from from the vector with the keypress data (original data or modified data). Some viruses also work this way.

    The big, difficult issue, is creating new code on the fly. If you want to generate new C++ code on the fly, you will have run the compiler to generate the code and figure out how to replace or connect it to your program. Interpretive languages are much better suited for this.

    Your project is not impossible, just a lot of work when using C++.

    Login or Signup to reply.
  3. The easiest solution is to alter the source code of your program as you’d like, and then when your program exits, start the compiler to rebuild your executable. This is a bit timing-sensitive as the build should not finish before your application has shut down entirely. using an atexit handler can help to ensure that you start the build late in the shutdown procedure.

    In this way, you get a new program every time you run it, but the program does not change while running.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search