Autor Beitrag
xuxu81
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Fr 13.04.18 16:23 
Edit: Problem hat sich erledigt. Habe auf GitHub jemanden mit dem gleichen Problem gefunden, der sich die Arbeit bereits gemacht hatte (Interceptor). Kann geschlossen werden! :)

Hallo zusammen,

ich habe vor einigen Wochen aus Spaß mal angefangen mich in C# einzuarbeiten. Die letzten Jahre habe ich eigentlich ausschließlich mit Scriptsprachen gearbeitet, das letzte mal C im Grundstudium genutzt (absolute Basics und gut 15 Jahre her), mit C++ habe ich nie was gemacht, ansonsten (privat) eigentlich nur mit Java Standalone-Anwendungen geschrieben. Programmiertechnisch würde ich mich irgendwo zwischen Anfänger und fortgeschritten einstufen...

Ich habe nun für mein aktuelles Projekt einen Treiber und passende DLL Anbindung gefunden, die ich gerne nutzen würde, leider ist die Bibliothek aber in C++ (oder gar C?!) geschrieben und ich kann sie nicht als Verweis hinzufügen (Problem unmanaged -> managed). Hatte bei meiner Suche nun einige Möglichkeiten gefunden, weiß aber nicht, wie ich es am geschicktesten anstellen soll:

Ich habe von der Erweiterung die .h, .lib und die .dll. Bei den meisten Suchen wurde eine "Wrapper Klasse" empfohlen, zum einen habe ich das Konzept aber nicht so ganz begriffen, zum anderen klingt das irgendwie mehr nach einer Klempnerarbeit wenn man nur bedingten Zugriff auf den Code hat.

Daher würde ich gerne die Meinung von erfahrenen Entwicklern hören, welches der effektivste (und vor allem auch einfachste ;-)) Weg ist. Hier der Code vom Header
ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
#ifndef _INTERCEPTION_H_
#define _INTERCEPTION_H_

#ifdef INTERCEPTION_STATIC
    #define INTERCEPTION_API
#else
    #if defined _WIN32 || defined __CYGWIN__
        #ifdef INTERCEPTION_EXPORT
            #ifdef __GNUC__
                #define INTERCEPTION_API __attribute__((dllexport))
            #else
                #define INTERCEPTION_API __declspec(dllexport)
            #endif
        #else
            #ifdef __GNUC__
                #define INTERCEPTION_API __attribute__((dllimport))
            #else
                #define INTERCEPTION_API __declspec(dllimport)
            #endif
        #endif
    #else
        #if __GNUC__ >= 4
            #define INTERCEPTION_API __attribute__ ((visibility("default")))
        #else
            #define INTERCEPTION_API
        #endif
    #endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

#define INTERCEPTION_MAX_KEYBOARD 10

#define INTERCEPTION_MAX_MOUSE 10

#define INTERCEPTION_MAX_DEVICE ((INTERCEPTION_MAX_KEYBOARD) + (INTERCEPTION_MAX_MOUSE))

#define INTERCEPTION_KEYBOARD(index) ((index) + 1)

#define INTERCEPTION_MOUSE(index) ((INTERCEPTION_MAX_KEYBOARD) + (index) + 1)

typedef void *InterceptionContext;

typedef int InterceptionDevice;

typedef int InterceptionPrecedence;

typedef unsigned short InterceptionFilter;

typedef int (*InterceptionPredicate)(InterceptionDevice device);

enum InterceptionKeyState
{
    INTERCEPTION_KEY_DOWN             = 0x00,
    INTERCEPTION_KEY_UP               = 0x01,
    INTERCEPTION_KEY_E0               = 0x02,
    INTERCEPTION_KEY_E1               = 0x04,
    INTERCEPTION_KEY_TERMSRV_SET_LED  = 0x08,
    INTERCEPTION_KEY_TERMSRV_SHADOW   = 0x10,
    INTERCEPTION_KEY_TERMSRV_VKPACKET = 0x20
};

enum InterceptionFilterKeyState
{
    INTERCEPTION_FILTER_KEY_NONE             = 0x0000,
    INTERCEPTION_FILTER_KEY_ALL              = 0xFFFF,
    INTERCEPTION_FILTER_KEY_DOWN             = INTERCEPTION_KEY_UP,
    INTERCEPTION_FILTER_KEY_UP               = INTERCEPTION_KEY_UP << 1,
    INTERCEPTION_FILTER_KEY_E0               = INTERCEPTION_KEY_E0 << 1,
    INTERCEPTION_FILTER_KEY_E1               = INTERCEPTION_KEY_E1 << 1,
    INTERCEPTION_FILTER_KEY_TERMSRV_SET_LED  = INTERCEPTION_KEY_TERMSRV_SET_LED << 1,
    INTERCEPTION_FILTER_KEY_TERMSRV_SHADOW   = INTERCEPTION_KEY_TERMSRV_SHADOW << 1,
    INTERCEPTION_FILTER_KEY_TERMSRV_VKPACKET = INTERCEPTION_KEY_TERMSRV_VKPACKET << 1
};

enum InterceptionMouseState
{
    INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN   = 0x001,
    INTERCEPTION_MOUSE_LEFT_BUTTON_UP     = 0x002,
    INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN  = 0x004,
    INTERCEPTION_MOUSE_RIGHT_BUTTON_UP    = 0x008,
    INTERCEPTION_MOUSE_MIDDLE_BUTTON_DOWN = 0x010,
    INTERCEPTION_MOUSE_MIDDLE_BUTTON_UP   = 0x020,

    INTERCEPTION_MOUSE_BUTTON_1_DOWN      = INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN,
    INTERCEPTION_MOUSE_BUTTON_1_UP        = INTERCEPTION_MOUSE_LEFT_BUTTON_UP,
    INTERCEPTION_MOUSE_BUTTON_2_DOWN      = INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN,
    INTERCEPTION_MOUSE_BUTTON_2_UP        = INTERCEPTION_MOUSE_RIGHT_BUTTON_UP,
    INTERCEPTION_MOUSE_BUTTON_3_DOWN      = INTERCEPTION_MOUSE_MIDDLE_BUTTON_DOWN,
    INTERCEPTION_MOUSE_BUTTON_3_UP        = INTERCEPTION_MOUSE_MIDDLE_BUTTON_UP,

    INTERCEPTION_MOUSE_BUTTON_4_DOWN      = 0x040,
    INTERCEPTION_MOUSE_BUTTON_4_UP        = 0x080,
    INTERCEPTION_MOUSE_BUTTON_5_DOWN      = 0x100,
    INTERCEPTION_MOUSE_BUTTON_5_UP        = 0x200,

    INTERCEPTION_MOUSE_WHEEL              = 0x400,
    INTERCEPTION_MOUSE_HWHEEL             = 0x800
};

enum InterceptionFilterMouseState
{
    INTERCEPTION_FILTER_MOUSE_NONE               = 0x0000,
    INTERCEPTION_FILTER_MOUSE_ALL                = 0xFFFF,

    INTERCEPTION_FILTER_MOUSE_LEFT_BUTTON_DOWN   = INTERCEPTION_MOUSE_LEFT_BUTTON_DOWN,
    INTERCEPTION_FILTER_MOUSE_LEFT_BUTTON_UP     = INTERCEPTION_MOUSE_LEFT_BUTTON_UP,
    INTERCEPTION_FILTER_MOUSE_RIGHT_BUTTON_DOWN  = INTERCEPTION_MOUSE_RIGHT_BUTTON_DOWN,
    INTERCEPTION_FILTER_MOUSE_RIGHT_BUTTON_UP    = INTERCEPTION_MOUSE_RIGHT_BUTTON_UP,
    INTERCEPTION_FILTER_MOUSE_MIDDLE_BUTTON_DOWN = INTERCEPTION_MOUSE_MIDDLE_BUTTON_DOWN,
    INTERCEPTION_FILTER_MOUSE_MIDDLE_BUTTON_UP   = INTERCEPTION_MOUSE_MIDDLE_BUTTON_UP,

    INTERCEPTION_FILTER_MOUSE_BUTTON_1_DOWN      = INTERCEPTION_MOUSE_BUTTON_1_DOWN,
    INTERCEPTION_FILTER_MOUSE_BUTTON_1_UP        = INTERCEPTION_MOUSE_BUTTON_1_UP,
    INTERCEPTION_FILTER_MOUSE_BUTTON_2_DOWN      = INTERCEPTION_MOUSE_BUTTON_2_DOWN,
    INTERCEPTION_FILTER_MOUSE_BUTTON_2_UP        = INTERCEPTION_MOUSE_BUTTON_2_UP,
    INTERCEPTION_FILTER_MOUSE_BUTTON_3_DOWN      = INTERCEPTION_MOUSE_BUTTON_3_DOWN,
    INTERCEPTION_FILTER_MOUSE_BUTTON_3_UP        = INTERCEPTION_MOUSE_BUTTON_3_UP,

    INTERCEPTION_FILTER_MOUSE_BUTTON_4_DOWN      = INTERCEPTION_MOUSE_BUTTON_4_DOWN,
    INTERCEPTION_FILTER_MOUSE_BUTTON_4_UP        = INTERCEPTION_MOUSE_BUTTON_4_UP,
    INTERCEPTION_FILTER_MOUSE_BUTTON_5_DOWN      = INTERCEPTION_MOUSE_BUTTON_5_DOWN,
    INTERCEPTION_FILTER_MOUSE_BUTTON_5_UP        = INTERCEPTION_MOUSE_BUTTON_5_UP,

    INTERCEPTION_FILTER_MOUSE_WHEEL              = INTERCEPTION_MOUSE_WHEEL,
    INTERCEPTION_FILTER_MOUSE_HWHEEL             = INTERCEPTION_MOUSE_HWHEEL,

    INTERCEPTION_FILTER_MOUSE_MOVE               = 0x1000
};

enum InterceptionMouseFlag
{
    INTERCEPTION_MOUSE_MOVE_RELATIVE      = 0x000,
    INTERCEPTION_MOUSE_MOVE_ABSOLUTE      = 0x001,
    INTERCEPTION_MOUSE_VIRTUAL_DESKTOP    = 0x002,
    INTERCEPTION_MOUSE_ATTRIBUTES_CHANGED = 0x004,
    INTERCEPTION_MOUSE_MOVE_NOCOALESCE    = 0x008,
    INTERCEPTION_MOUSE_TERMSRV_SRC_SHADOW = 0x100
};

typedef struct
{
    unsigned short state;
    unsigned short flags;
    short rolling;
    int x;
    int y;
    unsigned int information;
} InterceptionMouseStroke;

typedef struct
{
    unsigned short code;
    unsigned short state;
    unsigned int information;
} InterceptionKeyStroke;

typedef char InterceptionStroke[sizeof(InterceptionMouseStroke)];

InterceptionContext INTERCEPTION_API interception_create_context(void);

void INTERCEPTION_API interception_destroy_context(InterceptionContext context);

InterceptionPrecedence INTERCEPTION_API interception_get_precedence(InterceptionContext context, InterceptionDevice device);

void INTERCEPTION_API interception_set_precedence(InterceptionContext context, InterceptionDevice device, InterceptionPrecedence precedence);

InterceptionFilter INTERCEPTION_API interception_get_filter(InterceptionContext context, InterceptionDevice device);

void INTERCEPTION_API interception_set_filter(InterceptionContext context, InterceptionPredicate predicate, InterceptionFilter filter);

InterceptionDevice INTERCEPTION_API interception_wait(InterceptionContext context);

InterceptionDevice INTERCEPTION_API interception_wait_with_timeout(InterceptionContext context, unsigned long milliseconds);

int INTERCEPTION_API interception_send(InterceptionContext context, InterceptionDevice device, const InterceptionStroke *stroke, unsigned int nstroke);

int INTERCEPTION_API interception_receive(InterceptionContext context, InterceptionDevice device, InterceptionStroke *stroke, unsigned int nstroke);

unsigned int INTERCEPTION_API interception_get_hardware_id(InterceptionContext context, InterceptionDevice device, void *hardware_id_buffer, unsigned int buffer_size);

int INTERCEPTION_API interception_is_invalid(InterceptionDevice device);

int INTERCEPTION_API interception_is_keyboard(InterceptionDevice device);

int INTERCEPTION_API interception_is_mouse(InterceptionDevice device);

#ifdef __cplusplus
}
#endif

#endif


Wie würdet ihr vorgehen? Eine Bitte: Ich bin wie geschrieben noch recht unbeholfen - deshalb wäre ich über jedes kleine Beispiel, an dem ich mich orientieren kann, sehr sehr dankbar!

Vielen Dank für eure Zeit und Mühe und vorab schon mal ein wunderschönes Wochenende.

Moderiert von user profile iconTh69: URL-Titel hinzugefügt.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Fr 13.04.18 17:24 
Hallo und :welcome:

da gibt es 2 Möglichkeiten:
1. (was du ja auch schon herausgefunden hast): Wrapper-Klasse mittels C++/CLI, d.h. du erzeugst dir selber eine passende Schnittstelle für dein C#-Programm (und konvertierst die Daten selber)
2. mittels P/Invoke
Dafür gibt es ein Tool namens "P/Invoke Interop Assistant", was dir bei der Datentypenkonvertierung helfen kann. Es gibt leider nur noch dir Archivseite dazu: clrinterop (dazu dann mittels "download archive" alles herunterladen und entpacken). Eine kurze Beschreibung dazu: P/Invoke Interop Assistant (für dich wichtig wäre dann der letzte Tab "SigImp Translate Snippet": einfach den Code der Headerdatei ins links-obere Fenster kopieren, dann konvertieren lassen und den C#-Code in dein Projekt integrieren).

Edit: Ich habe, durch dein Edit, daher dieses Thema auf "Frage beantwortet" gesetzt.

Für diesen Beitrag haben gedankt: xuxu81