Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am currently building an audio streamer and I have a CPP .dll that I use functions of inside the WPF C# GUI.

The program needs to deal with sorts of events such as

  • Lower/Increase Volume
  • Manipulate Equalizer Bands

To this point I have used named events to deal with this issue. What would be a better way of handling all those events from the GUI to the .dll? The events system I use (.dll spawns a thread which while-s on a WaitForMultipleObjects), or would it be better/smarter/more-beneficial or just better-practice to use a named pipe instead?

share|improve this question
    
Interop? .. . . –  Robert Harvey Jan 25 '13 at 0:07
1  
If you are ok being beholden to Windows platform COM would probably be the way to go. –  whatsisname Jan 25 '13 at 6:16
1  
@whatsisname: You don't need COM. You can call DLL's with C calling convention from C#. –  Robert Harvey Jan 25 '13 at 20:36
1  
@RobertHarvey: that is true, but you have the hassle of creating a C wrapper API around your objects. While COM isn't hassle free either, it can provide a better C# <-> C++ interface for objects. –  whatsisname Feb 7 '13 at 0:49
    
If they live in different OS processes, named pipe is indeed a suitable choice. –  rwong Apr 3 '13 at 2:56
add comment

2 Answers 2

You can call from C# to C/C++ directly using a technology known as P/Invoke. With P/Invoke, a C++ function can be made to look just like a C# function.

Here's a simple example from this article in MSDN Magazine:

C Method Definition

BOOL MessageBeep(
  UINT uType   // beep type
);

P/Invoke Definition in C# of method in C

[DllImport("User32.dll")]
static extern Boolean MessageBeep(UInt32 beepType);

Calling the C method from C#

MessageBeep(0);

Now, isn't that simple & clean? Much of the .NET Base Class Library is implemented as C/C++ code with a P/Invoke wrapper and a C# facade. The .NET team itself uses P/Invoke rather than COM for this type of interop, it's simpler and more efficient.

A great resource for finding how to write P/Invoke method definitions is pinvoke.net.

share|improve this answer
1  
This only works well if the API only exhibits C types. Using an API that exhibits C++ types that way is problematic. –  CodesInChaos Mar 3 '13 at 15:36
add comment

There are a number of examples out there on calling a C++ dll from C#.

One from stack overflow that outlines some gotchas like the architecture the C++ dll is compiled on.

The above answer also referances MSDN which provides a pretty indepth example. Honestly I haven't read it top to bottom, but its on my list.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.