The mouse DLL now loads Interception.

master
Pacman Ghost 8 years ago
parent 9da6f09729
commit 61a0e691d6
  1. 6
      MainApp/MouseDll.cs
  2. 35
      MouseDll/main.cpp
  3. 28
      MouseDll/utils.cpp
  4. 9
      MouseDll/utils.hpp

@ -5,12 +5,14 @@ namespace MouseInterception
{
class MouseDll
{
// NOTE: The first place DLL's are loaded from are the application directory.
private const string DLL_NAME = "mouse.dll" ;
[DllImport( @"mouse.dll" , CallingConvention=CallingConvention.Cdecl )]
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string open_api() ;
[DllImport( @"mouse.dll" , CallingConvention=CallingConvention.Cdecl )]
[DllImport( @DLL_NAME , CallingConvention=CallingConvention.Cdecl )]
[return: MarshalAs(UnmanagedType.BStr)]
private static extern string close_api() ;

@ -1,21 +1,39 @@
#include <windows.h>
#include <stdexcept>
#include <cassert>
#include "main.hpp"
#include "utils.hpp"
using namespace std ;
// --- LOCAL DATA ------------------------------------------------------
static bool gIsOpen = false ;
static HMODULE ghInterceptionDll = NULL ;
// ---------------------------------------------------------------------
void
openApi()
{
// open the API
if ( gIsOpen )
// check if we are open
if ( ghInterceptionDll != NULL )
throw runtime_error( "API is already open." ) ;
gIsOpen = true ;
// load Interception
wchar_t buf[ _MAX_PATH+1 ] ;
GetModuleFileName( NULL , buf , ARRAY_SIZE(buf) ) ;
wchar_t drive[_MAX_DIR+1] , dir[_MAX_DIR+1] , fname[_MAX_FNAME+1] , extn[_MAX_EXT+1] ;
errno_t rc = _wsplitpath_s( buf , drive , ARRAY_SIZE(drive) , dir , ARRAY_SIZE(dir) , fname , ARRAY_SIZE(fname) , extn , ARRAY_SIZE(extn) ) ;
if ( rc == 0 )
_wmakepath_s( buf , ARRAY_SIZE(buf) , drive , dir , L"interception" , L".dll" ) ;
else
{
assert( false ) ;
wcscpy_s( buf , ARRAY_SIZE(buf) , L"interception.dll" ) ;
}
ghInterceptionDll = LoadLibrary( buf ) ;
if ( ghInterceptionDll == NULL )
throw runtime_error( MAKE_STRING( "Can't load Interception: " << getLastErrorString() ) ) ;
}
// ---------------------------------------------------------------------
@ -23,8 +41,11 @@ openApi()
void
closeApi()
{
// close the API
if ( ! gIsOpen )
// check if we are open
if ( ghInterceptionDll == NULL )
throw runtime_error( "API is not open." ) ;
gIsOpen = false ;
// close Interception
FreeLibrary( ghInterceptionDll ) ;
ghInterceptionDll = NULL ;
}

@ -6,6 +6,34 @@ using namespace std ;
// ---------------------------------------------------------------------
string
getErrorString( int errorCode )
{
// get the WIN32 error message
char buf[ 4*1024 ] = "" ;
if ( errorCode != 0 )
{
DWORD nBytes = FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK ,
NULL ,
errorCode ,
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT) ,
buf , sizeof(buf) ,
NULL
) ;
}
if ( buf[0] != '\0' )
return buf ;
// just return the WIN32 error code
sprintf_s( buf , sizeof(buf) , "WIN32 ERROR: %08lX" , errorCode ) ;
return buf ;
}
string getLastErrorString() { return getErrorString( GetLastError() ) ; }
// ---------------------------------------------------------------------
wstring
fromUtf8( const char* pStr , int len )
{

@ -6,12 +6,19 @@
// ---------------------------------------------------------------------
extern std::string getLastErrorString() ;
extern std::string getErrorString( int errorCode ) ;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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()) ; }
#define MAKE_STRING( msg ) ( ((std::ostringstream&)((std::ostream&)std::ostringstream() << msg)).str() )
// ---------------------------------------------------------------------
#define MAKE_STRING( msg ) ( ((std::ostringstream&)((std::ostream&)std::ostringstream() << msg)).str() )
#define ARRAY_SIZE( arr ) ( sizeof(arr) / sizeof((arr)[0]) )
// ---------------------------------------------------------------------

Loading…
Cancel
Save