Added the ability to run from the console (with logging).

master
Pacman Ghost 8 years ago
parent 61a0e691d6
commit 5968fb8e17
  1. 6
      MainApp/MouseDll.cs
  2. 39
      MainApp/Program.cs
  3. 4
      MouseDll/api.cpp
  4. 18
      MouseDll/main.cpp
  5. 2
      MouseDll/main.hpp
  6. 25
      MouseDll/utils.cpp
  7. 2
      MouseDll/utils.hpp

@ -10,16 +10,16 @@ namespace MouseInterception
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string open_api() ;
private static extern string open_api( int initConsole ) ;
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string close_api() ;
public MouseDll()
public MouseDll( bool initConsole )
{
// open the mouse API
string errorMsg = open_api() ;
string errorMsg = open_api( initConsole ? 1 : 0 ) ;
if ( errorMsg != null )
throw new Exception( errorMsg ) ;
}

@ -1,25 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System ;
using System.Runtime.InteropServices ;
using System.Windows.Forms ;
namespace MouseInterception
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[DllImport( "kernel32.dll" )]
static extern bool AttachConsole( int processId ) ;
private const int ATTACH_PARENT_PROCESS = -1 ;
[STAThread]
static void Main()
static void Main( string[] args )
{
// FIXME!
MouseDll mouseDll = new MouseDll() ;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new MainForm() );
if ( args.Length > 0 )
{
// run in console mode
AttachConsole( ATTACH_PARENT_PROCESS ) ;
System.Console.WriteLine( "\nInitialized the C# console." ) ; // FIXME!
MouseDll mouseDll = new MouseDll( true ) ;
System.Windows.Forms.SendKeys.SendWait( "{ENTER}" ) ;
Application.Exit() ;
}
else
{
// run with a GUI
MouseDll mouseDll = new MouseDll( false ) ;
Application.EnableVisualStyles() ;
Application.SetCompatibleTextRenderingDefault( false ) ;
Application.Run( new MainForm() ) ;
}
}
}
}

@ -8,12 +8,12 @@ using namespace std ;
// ---------------------------------------------------------------------
extern "C" __declspec(dllexport) BSTR
open_api()
open_api( int initConsole )
{
// open the API
try
{
openApi() ;
openApi( initConsole != 0 ) ;
return NULL ;
}
catch ( exception& xcptn )

@ -1,24 +1,39 @@
#include <windows.h>
#include <iostream>
#include <stdexcept>
#include <cassert>
#include "main.hpp"
#include "utils.hpp"
using namespace std ;
// --- LOCAL DATA ------------------------------------------------------
static HMODULE ghInterceptionDll = NULL ;
static bool gEnableConsole = false ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#define LOG_MSG( msg ) \
{ \
if ( gEnableConsole ) \
cout << msg << endl ; \
}
// ---------------------------------------------------------------------
void
openApi()
openApi( bool initConsole )
{
// check if we are open
if ( ghInterceptionDll != NULL )
throw runtime_error( "API is already open." ) ;
// initialize
gEnableConsole = initConsole ;
// load Interception
wchar_t buf[ _MAX_PATH+1 ] ;
GetModuleFileName( NULL , buf , ARRAY_SIZE(buf) ) ;
@ -31,6 +46,7 @@ openApi()
assert( false ) ;
wcscpy_s( buf , ARRAY_SIZE(buf) , L"interception.dll" ) ;
}
LOG_MSG( "Loading Interception: " << toUtf8(buf) ) ;
ghInterceptionDll = LoadLibrary( buf ) ;
if ( ghInterceptionDll == NULL )
throw runtime_error( MAKE_STRING( "Can't load Interception: " << getLastErrorString() ) ) ;

@ -3,7 +3,7 @@
// ---------------------------------------------------------------------
extern void openApi() ;
extern void openApi( bool initConsole ) ;
extern void closeApi() ;
// ---------------------------------------------------------------------

@ -34,6 +34,31 @@ string getLastErrorString() { return getErrorString( GetLastError() ) ; }
// ---------------------------------------------------------------------
string
toUtf8( const wchar_t* pStr , int len )
{
if ( pStr == NULL || len == 0 )
return "" ;
assert( len > 0 || len == -1 ) ;
// figure out how many UTF-8 characters we are going to get
int nChars = WideCharToMultiByte( CP_UTF8 , 0 , pStr , len , NULL , 0 , NULL , NULL ) ;
if ( len == -1 )
-- nChars ;
if ( nChars == 0 )
return "" ;
// convert the string to UTF-8
// nb: slightly naughty to write directly into the string like this
string buf ;
buf.resize( nChars ) ;
WideCharToMultiByte( CP_UTF8 , 0 , pStr , len , const_cast<char*>(buf.c_str()) , nChars , NULL , NULL ) ;
return buf ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
wstring
fromUtf8( const char* pStr , int len )
{

@ -11,6 +11,8 @@ extern std::string getErrorString( int errorCode ) ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
extern std::string toUtf8( const wchar_t* pStr , int len=-1 ) ;
inline std::string toUtf8( const std::wstring& str ) { return toUtf8(str.c_str(),str.length()) ; }
extern std::wstring fromUtf8( const char* pStr , int len=-1 ) ;
inline std::wstring fromUtf8( const std::string& str ) { return fromUtf8(str.c_str(),str.length()) ; }

Loading…
Cancel
Save