Mixing C# GUI with C/C++ Core Logic
Hey everyone, I’m working on a project and I’m wondering if it’s doable to build the graphical interface using C# while keeping the main functionality in C or C++.
For instance, let’s say I’m developing a messaging app. I’d like to design the UI with C# but implement the core features in C. Is this a viable approach?
I’ve heard about something called P/Invoke, but I’m not entirely sure how it works or if it’s the best solution. Does anyone have experience with this kind of setup or know of any good resources I could check out?
Thanks in advance for any tips or advice!
yeah, u can totally do that! i’ve done similar stuff before. P/Invoke is the way to go for connecting C# and C/C++. It’s not too hard once u get the hang of it. just make sure ur C functions are exported properly. there’s some good tutorials online if u search for ‘C# P/Invoke examples’. good luck with ur project!
Absolutely, this approach is feasible and quite common in certain scenarios. P/Invoke is indeed the primary method for calling native C/C++ functions from C# code. It allows you to leverage the performance benefits of C/C++ while utilizing C#'s rich UI capabilities.
When implementing this, you’ll need to create a wrapper library in C/C++ that exposes the necessary functions. Then, in your C# code, you’ll declare these functions using the [DllImport] attribute. Be mindful of data marshaling between the two environments, as it can be a source of complexity.
For your messaging app, you could implement the networking, encryption, and core logic in C/C++, while keeping the user interface and application flow in C#. This separation can lead to a well-structured and efficient application.
Hey there! I’m curious, have you considered using C++/CLI as a bridge between C# and C++? it’s another option that might work for ur project. How complex is the core functionality you’re planning to implement in C? i’d love to hear more about ur app idea!