Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C++

Gecko! Embedded C++ scripting for your applications

Rate me:
Please Sign up or sign in to vote.
4.93/5 (23 votes)
31 Aug 20036 min read 240.8K   2.6K   102  
Embed a C++ compiler in your project, use C++ as a compiled "scripting" language!
// Gecko is Copyright (C) PixiGreg <pixigreg@ifrance.com>, 2003.
// Gecko must not be used in a commercial application
// without prior written agreement of its author.


#pragma once

#include "Redirect.h"

class CGecko : protected CRedirect
{
public:
	// ctor does nothing special.
	// dtor will call AbortThread() and Unload() if you ommit them.
	CGecko(void);
	~CGecko(void);

	// Normally initialization is done automatically thanks to GeckoSetup code
	// so you shouldn't use these functions. Anyway they are self-explanatory.
	void SetMinGWDir(LPCSTR szDir);
	void SetGeckoDir(LPCSTR szDir);

	// Tests if a ECK file is up-to-date, by checking the INI written when compiled.
	// If the ECK file or the INI file can't be found, eckIsUpToDate() returns false.
	bool eckIsUpToDate(LPCSTR szName);
	// Tests if ECK file exists.
	bool eckExists(LPCSTR szName);
	// Launches compilation/linkage of the "szName.cpp" file.
	// Compiler/linker outputs will go to virtual functions below.
	// WARNING: if you try to re-compile to an ECK previously loaded, even unloaded,
	// the linker will complain: 'Unable to write(...)'. That's because I don't manage to have
	// the ECK file unmapped from process. See eckUnload() below.
	bool eckCompile(LPCSTR szName);

	// Overridable functions called when compiling.
	virtual void OnCommandStart(LPCSTR lpszCmdLine) {};
	virtual void OnCommandSTDOUT(LPCSTR lpszOutput) {};
	virtual void OnCommandSTDERR(LPCSTR lpszOutput) {};
	virtual void OnCommandEnd(bool bSuccessful) {};

	// Loads an ECK file. If a ECK file is already loaded/running, it will be automatically stopped/unloaded.
	// To execute several ECK's, construct several CGecko.
	bool eckLoad(LPCSTR szName);
	// Unloads an ECK file. Actually it will never be unloaded completely until program termination:
	// variables will remain with the same value at next Load() of the same ECK.
	// That's because I don't manage to have the ECK file unmapped from process, even with several calls
	// to FreeLibrary(). If someone has an idea...
	void eckUnload(void);

	// Runs a previously loaded ECK file (same as calling eckSendMessage(GM_RUN)).
	// Returns what you returns in your script, or -1 in case of error (no ECK loaded for example).
	int eckRun(WPARAM wParam=0, LPARAM lParam=0);
	// Aborts (stops) a previously loaded and running ECK file (same as calling eckSendMessage(GM_ABORT)).
	// Returns what you returns in your script, or -1 in case of error.
	int eckAbort(WPARAM wParam=0, LPARAM lParam=0);
	// Sends a message to currently loaded ECK file.
	// Returns what you returns in your script, or -1 in case of error.
	int eckSendMessage(UINT nMsg, WPARAM wParam=0, LPARAM lParam=0);

	// Creates a thread and runs the loaded ECK file from it. 
	bool eckRunThreaded(WPARAM wParam=0, LPARAM lParam=0, int nPriority=THREAD_PRIORITY_NORMAL);
	// Returns true if a thread is running.
	bool eckIsThreadRunning(void);
	// Returns the thread handle of current running script. Can be NULL if no thread.
	HANDLE GetThreadHandle(void);
	// Aborts running script (sending GM_ABORT), causing thread to return if the script indeed stops.
	// If the running script doesn't stop within the specified time (in milli-seconds), either bAllowKill
	// is true and the thread will be terminated with TerminateThread(), or eckAbortThread() will return.
	// Returns true if the script is aborted and thread is stopped.
	bool eckAbortThread(DWORD dwTimeout=5000, bool bAllowKill=false);
	
protected:
	virtual void OnChildStarted(LPCSTR lpszCmdLine);
	virtual void OnChildStdOutWrite(LPCSTR lpszOutput);
	virtual void OnChildStdErrWrite(LPCSTR lpszOutput);
	virtual void OnChildTerminate(DWORD dwExitCode);

	bool DoCommand(LPCSTR szCommand);
	void CheckGeckoDir(void);

	char m_sMinGWDir[MAX_PATH], m_sGeckoDir[MAX_PATH];

	bool m_bCompileDone, m_bCompileSuccessful, m_bErrorOcurred;//, m_bMagicFlag;

	HANDLE m_hThread;
	
	HANDLE m_handle;
	void *m_proc;
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
France France
Bouh

Comments and Discussions