#include using namespace std; class ExternalProcess { public : DECLARE_CONOBJECT(ExternalProcess); static void consoleIni"> Create a c++ class that has a consoleMethod | Torque Game Engine | Forums | Community | GarageGames.com

Game Development Community

Create a c++ class that has a consoleMethod

by gamer · in Torque Game Engine · 06/08/2006 (2:18 pm) · 3 replies

Hi, I am trying to add a class in the engine that has a consoleMethod:
#include "Windows.h"
#include <string>
#include <iostream>
using namespace std;
class ExternalProcess
{
public :
	DECLARE_CONOBJECT(ExternalProcess);
	static void consoleInit();
	int ExecuteProcess(string &FullPathToExe, string &Parameters, int SecondsToWait);
	string getExeName(string strFullPathToExe);
	

};

got the following compilation error:
g:\Torque\SDK\engine\extra\process.h(8): warning C4183: 'DECLARE_CONOBJECT': missing return type; assumed to be a member function returning 'int'

how should I set up my class to make it a conobject?
any help and docs are greatly appreciated! Also does torque have some built-in function/classes to communicate with an external exe?
thanks

#1
06/08/2006 (3:00 pm)
You need to include the appropriate header for it to recognize the DECLARE_CONOBJECT macro. I believe that header is /console/simBase.h
#2
06/08/2006 (3:10 pm)
Thank you!
Ok I am able to compile now, however there's an link error:
Torque Demo error LNK2005: "void * __cdecl operator new(unsigned int,void *)" (??2@YAPAXIPAX@Z) already defined in winMemory.obj
Torque Demo error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in platformMemory.obj
Torque Demo error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in platformMemory.obj
Torque Demo error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in platformMemory.obj
Torque Demo error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in platformMemory.obj
Torque Demo fatal error LNK1169: one or more multiply defined symbols found

I am clueless...

#include "Windows.h"
#include <string>
#include "console/console.h"
#include "console/consoleTypes.h"
#include "game/gameBase.h"

using namespace std;
class ExternalProcess:public GameBase
{
public :
	DECLARE_CONOBJECT(ExternalProcess);
	static void consoleInit();
	int ExecuteProcess(string &FullPathToExe, string &Parameters, int SecondsToWait);
	string getExeName(string strFullPathToExe);
	

};
#include "Afx.h"
#include "process.h"

int ExternalProcess::ExecuteProcess(string &FullPathToExe, string &Parameters, int SecondsToWait)
{
    int iMyCounter = 0, iReturnVal = 0;
    DWORD dwExitCode;

    /* - NOTE - You could check here to see if the exe even exists */

    /* Add a space to the beginning of the Parameters */
    if (Parameters.size() != 0 )
    {
        Parameters.insert(0," ");
    }

    /* When using CreateProcess the first parameter needs to be the exe itself */
    Parameters = getExeName(FullPathToExe).append(Parameters);

    /*
        The second parameter to CreateProcess can not be anything but a char !!
        Since we are wrapping this C function with strings, we will create
        the needed memory to hold the parameters
    */

    /* Dynamic Char */
    char * pszParam = new char[Parameters.size() + 1];

    /* Verify memory availability */
    if (pszParam == 0)
    {
        /* Unable to obtain (allocate) memory */
        return 1;
    }
    const char* pchrTemp = Parameters.c_str();
    strcpy(pszParam, pchrTemp);

    /* CreateProcess API initialization */
    STARTUPINFO siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);
	
	CString fullPath=FullPathToExe.c_str();
	
	int nSize1 = MultiByteToWideChar(CP_ACP, 0, pszParam, -1, NULL, 0);
	 LPWSTR pszParams = new WCHAR[nSize1];
	// call again to make the conversion
	MultiByteToWideChar(CP_ACP, 0, pszParam, -1, pszParams, nSize1);

	/* Execute */
    if (CreateProcess(fullPath, pszParams, 0, 0, false,
                            CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo,
                            &piProcessInfo) != false)
    {
       /* A loop to watch the process. Dismissed with SecondsToWait set to 0 */
       GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);

       while (dwExitCode == STILL_ACTIVE && SecondsToWait != 0)
       {
           GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
           Sleep(500);
           iMyCounter += 500;

           if (iMyCounter > (SecondsToWait * 1000))
           {
               dwExitCode = 0;
           }
       }
    }
    else
    {
        /* CreateProcess failed. You could also set the return to GetLastError() */
        iReturnVal = 2;
    }

    /* Release handles */
    CloseHandle(piProcessInfo.hProcess);
    CloseHandle(piProcessInfo.hThread);

    /* Free memory */
    delete[]pszParam;
    pszParam = 0;

    return iReturnVal;
}

/*------------------| getExeName |--------------------------------*
| Description:
| ~~~~~~~~~
| Returns the string after the last "\"
\*-----------------------------------------------------------------*/
string ExternalProcess::getExeName(string strFullPathToExe)
{
    int Position = strFullPathToExe.find_last_of("\");
    strFullPathToExe = strFullPathToExe.erase(0, Position +1);

    return strFullPathToExe;
}

ConsoleMethod( ExternalProcess, exec, bool, 5, 5, "()")
{
   return object->ExecuteProcess(string(argv[2]),string(argv[3]),atoi(argv[4]));
}
#3
06/08/2006 (3:23 pm)
Torque redefines the operator new and thus will not play nicely with the STL. You can either choose not to use the STL, I notice your using , or you can disable the Torque memory manager.

See: tdn.garagegames.com/wiki/MemoryManager