Question

I need to use this library (Interception). It is written in C++. Briefly, it lets you manipulate input from keyboard or mouse. What I need to do is to use some of those calls from C#.

I'm not sure which path to follow. So far I've read that you could use Pinvoke to use C++ code inside C#.

I have many questions regarding PInvoke:
1) Do Pinvoke libraries need to be in a specific system path to be found (e.g. system32)?

I've read that getting the right PInvoke signatures can be hard and error prone, and since I couldn't find any PInvoke signature for Interception in PInvoke.net,
2) Does that mean that I should desist on using PInvoke?

I've also seen, but didn't go much into detail, that in Visual Studio, when you create a project, you can create a Visual C++->CLR->Class Library.
3) Should I use this to build an assembly linking Interception? Could this assembly be used within C#?

Anyways, if a C# alternative to Interception exists, I would use that instead.

Was it helpful?

Solution

1) No, if the library is in the same path as the executable, it will work.

2) You only use PInvoke with C functions, not C++ objects. So using PInvoke to directly call Interception library is probably out of the question.

3) You can create a C++/CLI library that links to Interception. But you will have to create your own .NET objects to wrap the pure C++ objects in Interception.

I would recommend you read a bit about coding CLR in C++. Making a CLR wrapper in C++/CLI is probably the cleanest way to go.

OTHER TIPS

A p/invoke import is resolved using the system DLL search path. So the DLL need not be in the system directory. It just needs to be in the DLL search path. Indeed, you should not put it in the system directory because the system owns that. The best place is the same directory as the executable. That location is searched first.

The library in question has a rather small header file that presents a C style interface. It's a rather simple candidate for p/invoke. You certainly won't find any translations on pinvoke.net but it's also not too hard to translate this library.

You could certainly use C++/CLI for your interop. This would add another layer and another module. On the other hand you would have type safety right the way down the chain. If you are not confident and experienced with interop and p/invoke then it is possible that C++/CLI will be more productive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top