J
Jon Slaughter
How difficult is it for one to integrate unmanaged C++ into C#? I know for
functions one can use DLLimport but how does one go about doing it for
classes? Do I have to completely reimplement the classes in managed C++ as a
wrapper to the unmanaged C++ classes or is there an easier way?
Essentially what I have done is written a C++ kernel mode driver and I want
to use it from my C# program. Because it requires some setup outside the
kernel(because it has to pass buffers back and forth and extract the
information out of the buffers) I'd rather keep the that specific code
unmanaged because its probably slightly faster.
But I don't want to have a huge number of dll imports and write a C# wrapper
class over each of the imports because it seems a little excessive. (Not
only does the driver internally use a similar class but I'd essentially be
implementing the same class 3 times... one in the driver, which is actually
slightly different but pretty much with same interface, one in unmanaged C++
to interface with the driver, and some type of managed wrapper.)
Is there a better way? Can I just mix unmanaged and managed C++ in the same
project to do all the work and not have to end up with a C# wrapper?
Basically I think I can get away with just using unsafe code to work with
the buffers. I'm mainly worried about the performance hit associated with
doing this. From my initial tests C# is anywhere from 2-5 times slower at
calling kernel mode drivers than unmanaged C++(not sure if I can get any
speed up by indirectly referencing unamanged C++ though so I might take that
hit no matter what if I plan on using C#). Of course I don't want to end up
with having to change 3 classes every time I make a simple change either.
Any ideas?
Thanks,
Jon
functions one can use DLLimport but how does one go about doing it for
classes? Do I have to completely reimplement the classes in managed C++ as a
wrapper to the unmanaged C++ classes or is there an easier way?
Essentially what I have done is written a C++ kernel mode driver and I want
to use it from my C# program. Because it requires some setup outside the
kernel(because it has to pass buffers back and forth and extract the
information out of the buffers) I'd rather keep the that specific code
unmanaged because its probably slightly faster.
But I don't want to have a huge number of dll imports and write a C# wrapper
class over each of the imports because it seems a little excessive. (Not
only does the driver internally use a similar class but I'd essentially be
implementing the same class 3 times... one in the driver, which is actually
slightly different but pretty much with same interface, one in unmanaged C++
to interface with the driver, and some type of managed wrapper.)
Is there a better way? Can I just mix unmanaged and managed C++ in the same
project to do all the work and not have to end up with a C# wrapper?
Basically I think I can get away with just using unsafe code to work with
the buffers. I'm mainly worried about the performance hit associated with
doing this. From my initial tests C# is anywhere from 2-5 times slower at
calling kernel mode drivers than unmanaged C++(not sure if I can get any
speed up by indirectly referencing unamanged C++ though so I might take that
hit no matter what if I plan on using C#). Of course I don't want to end up
with having to change 3 classes every time I make a simple change either.
Any ideas?
Thanks,
Jon