Added the ability to have debug settings.

master
Pacman Ghost 8 years ago
parent 5968fb8e17
commit cc2da58655
  1. 41
      MainApp/DebugConfig.cs
  2. 14
      MainApp/MouseDll.cs
  3. 1
      MainApp/MouseInterception.csproj
  4. 25
      MainApp/Program.cs
  5. 17
      MouseDll/api.cpp
  6. 39
      MouseDll/main.cpp
  7. 9
      MouseDll/main.hpp

@ -0,0 +1,41 @@
using System.IO ;
using System.Xml ;
using System.Runtime.InteropServices ;
namespace MouseInterception
{
class DebugConfig
{
[StructLayout( LayoutKind.Sequential , CharSet=CharSet.Unicode , Pack=1 )]
public struct Settings
{
public string mLogFilename ;
}
private Settings mSettings ;
public Settings settings { get { return mSettings ; } }
public DebugConfig( string fname )
{
// load the DebugConfig
mSettings = new Settings() ;
if ( ! File.Exists( fname ) )
return ;
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings() ;
xmlReaderSettings.IgnoreComments = true ;
xmlReaderSettings.IgnoreProcessingInstructions = true ;
xmlReaderSettings.IgnoreWhitespace = true ;
XmlDocument xmlDoc = new XmlDocument() ;
using ( XmlReader xmlReader = XmlReader.Create( fname , xmlReaderSettings ) )
xmlDoc.Load( xmlReader ) ;
// parse the values
XmlNode configXmlNode = xmlDoc.SelectSingleNode( "/debug" ) ;
XmlNode xmlNode = configXmlNode.SelectSingleNode( "logFilename" ) ;
if ( xmlNode != null )
mSettings.mLogFilename = xmlNode.InnerText.Trim() ;
}
}
}

@ -16,12 +16,18 @@ namespace MouseInterception
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string close_api() ;
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string reload_config( ref DebugConfig.Settings pDebugSettings ) ;
public MouseDll( bool initConsole )
{
// open the mouse API
string errorMsg = open_api( initConsole ? 1 : 0 ) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
// initialize
reloadConfig() ;
}
~MouseDll()
@ -32,5 +38,13 @@ namespace MouseInterception
throw new Exception( errorMsg ) ;
}
public void reloadConfig()
{
// reload the config
DebugConfig.Settings settings = Program.debugConfig.settings ;
string errorMsg = reload_config( ref settings ) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
}
}
}

@ -49,6 +49,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="DebugConfig.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>

@ -1,4 +1,5 @@
using System ;
using System.IO ;
using System.Runtime.InteropServices ;
using System.Windows.Forms ;
@ -6,6 +7,9 @@ namespace MouseInterception
{
static class Program
{
private static string mBaseDir ;
private static DebugConfig mDebugConfig ;
[DllImport( "kernel32.dll" )]
static extern bool AttachConsole( int processId ) ;
private const int ATTACH_PARENT_PROCESS = -1 ;
@ -13,6 +17,17 @@ namespace MouseInterception
[STAThread]
static void Main( string[] args )
{
// initialize
mBaseDir = Application.StartupPath ;
#if DEBUG
string baseDir = System.IO.Path.Combine( mBaseDir , "../../../_TEST_" ) ;
if ( Directory.Exists( baseDir ) )
mBaseDir = baseDir ;
#endif // DEBUG
// load the debug config
string fname = getAppRelativePath( "debug.xml" ) ; // FIXME! make this configurable
mDebugConfig = new DebugConfig( fname ) ;
if ( args.Length > 0 )
{
@ -32,5 +47,15 @@ namespace MouseInterception
Application.Run( new MainForm() ) ;
}
}
public static string getAppRelativePath( string relPath )
{
// return the full application-relative path
string path = System.IO.Path.Combine( mBaseDir , relPath );
return System.IO.Path.GetFullPath( path );
}
public static DebugConfig debugConfig { get { return mDebugConfig ; } }
}
}

@ -38,3 +38,20 @@ close_api()
return SysAllocString( fromUtf8( MAKE_STRING( xcptn.what() ) ).c_str() ) ;
}
}
// ---------------------------------------------------------------------
extern "C" __declspec(dllexport) BSTR
reload_config( const DebugConfig* pDebugConfig )
{
// reload the config
try
{
reloadConfig( pDebugConfig ) ;
return NULL ;
}
catch ( exception& xcptn )
{
return SysAllocString( fromUtf8( MAKE_STRING( xcptn.what() ) ).c_str() ) ;
}
}

@ -1,11 +1,11 @@
#include <windows.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cassert>
#include "main.hpp"
#include "utils.hpp"
using namespace std ;
// --- LOCAL DATA ------------------------------------------------------
@ -13,13 +13,21 @@ using namespace std ;
static HMODULE ghInterceptionDll = NULL ;
static bool gEnableConsole = false ;
static wstring gLogFilename ;
static ofstream gLogFile ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define LOG_MSG( msg ) \
{ \
if ( gEnableConsole ) \
cout << msg << endl ; \
if ( gEnableConsole || gLogFile.is_open() ) \
{ \
string _buf_ = MAKE_STRING( msg ) ; \
if ( gEnableConsole ) \
cout << _buf_ << endl ; \
if ( gLogFile.is_open() ) \
gLogFile << _buf_ << endl ; \
} \
}
// ---------------------------------------------------------------------
@ -65,3 +73,28 @@ closeApi()
FreeLibrary( ghInterceptionDll ) ;
ghInterceptionDll = NULL ;
}
// ---------------------------------------------------------------------
void
reloadConfig( const DebugConfig* pDebugConfig )
{
// validate the parameters
if ( pDebugConfig == NULL )
throw runtime_error( "Missing DebugConfig." ) ;
const wchar_t* pLogFilename = pDebugConfig->mpLogFilename ;
if ( pLogFilename == NULL )
pLogFilename = L"" ;
// initialize the log file
if ( _wcsicmp( gLogFilename.c_str() , pLogFilename ) != 0 )
{
if ( gLogFile.is_open() )
gLogFile.close() ;
if ( pLogFilename[0] != L'\0' )
{
gLogFile.open( pLogFilename ) ;
gLogFilename = pLogFilename ;
}
}
}

@ -3,9 +3,18 @@
// ---------------------------------------------------------------------
struct DebugConfig
{
const wchar_t* mpLogFilename ;
} ;
// ---------------------------------------------------------------------
extern void openApi( bool initConsole ) ;
extern void closeApi() ;
extern void reloadConfig( const DebugConfig* pDebugConfig ) ;
// ---------------------------------------------------------------------
#endif // MAIN_HPP
Loading…
Cancel
Save