1

I have a stream of bytes that I sort into structs. Now I need to update a C# GUI using values from those structs. Now I've found things about pinvoke and dllimport using Google but everything is calling c code from c#, I need to do the opposite.

The c# code has functions that spin dials/updates values, so once I have the values I need I want to just call those functions, with those values as parameters, my c code is all written and the gui is all coded and functional using dials that I drag with my mouse, I just need to glue these together.

Thanks for any help!

Cool Joe
  • 315
  • 1
  • 3
  • 11
  • 1
    All the functions you need to call are in a dll, (as opposed to an exe), right? I would think you could reference that DLL from C so I'm not sure what you're problem is exactly... though it looks like you might want to make use of events. When you say "C code" are you talking about visual c++ or are you actually talking about a c compiler? – Brandon Moore Feb 14 '12 at 04:56
  • I get the impression that you didn't try the search feature before asking this question: [Possible to call C++ code from C#?](http://stackoverflow.com/questions/935664/possible-to-call-c-code-from-c), [Calling C# code from C++](http://stackoverflow.com/questions/778590/calling-c-sharp-code-from-c), [How to call C# code from C code?](http://stackoverflow.com/questions/9056048/how-to-call-c-sharp-code-from-c-code), [Passing a C# callback function through Interop/pinvoke](http://stackoverflow.com/questions/7970128/passing-a-c-sharp-callback-function-through-interop-pinvoke), among others. – Cody Gray - on strike Feb 14 '12 at 04:57
  • @cody Lol, I get the impression you didn't read my whole post. everyone: im using c and c# only – Cool Joe Feb 14 '12 at 05:01
  • @Brandon: You can't add a "reference" to a managed C# DLL in a native C++ project. He doesn't mention that he's using C++/CLI. – Cody Gray - on strike Feb 14 '12 at 05:01
  • @CodyGray As you can tell I don't do much with C++, but I think everyone agrees events (or callbacks) are needed. – Brandon Moore Feb 14 '12 at 06:00

2 Answers2

2

Generally speaking, you cannot call C# code from C code. (You can call back into C# code from C code, but the C# side needs to initiate the process and pass in a callback.)

The "right way" is for the C# code to call into the C code and provide a callback function (look up the UnmanagedFunctionPointer attribute for how to turn a managed delegate into a function pointer.)

If that option isn't possible, you have a few alternatives, but none of them are going to be as easy as just "gluing thing together":

  • Use a mixed-mode C++ library to bridge the C and C# code. This is a mess, IMO, and I only suggest it because its the least amount of change to your C code.
  • Expose a COM object from your C# code and communicate with that from C. Slightly less messy than option A but at the cost of introducing COM into the mix.
  • Use some form of inter-process communications (TCP sockets, named pipes, etc.) and communicate between the two code elements as if they were separate programs.
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
0

How about you call your C code from C# using PInvoke, make the C code return some data, and then have the C# application use that data to update the GUI? Is there some reason you can't have the C# code just be "in charge", only calling the C code when it is needed and otherwise just taking over the UI thread? Why do you want your C code to be able to update the GUI?

David Grayson
  • 84,103
  • 24
  • 152
  • 189
  • I could do that but I feel like that is slow and with this data is polled every half second, could even be less. There is already going to be some lag, and the gui really doesn't have anything to do with the main interface, just the math for rotating images. – Cool Joe Feb 14 '12 at 04:54
  • It's a telemetry system btw. Live data stream of AHRS which updates the c# gui with a few instruments found in a plane cockpit. – Cool Joe Feb 14 '12 at 04:57