Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANSI C as core of a C# project? Is this possible?

Tags:

c

c#

architecture

I'm writing a NON-GUI app which I want to be cross platform between OS X and Windows. I'm looking at the following architecture, but I don't know if it will work on the windows side:

(Platform specific entry point) -> ANSI C main loop => ANSI C model code doing data processing / logic => (Platform specific helpers)

So the core stuff I'm planning to write in regular ANSI C, because A) it should be platform independent, B) I'm extremely comfortable with C, C) It can do the job and do it well

(Platform specific entry point) can be written in whatever necessary to get the job done, this is a small amount of code, doesn't matter to me.

(Platform specific helpers) is the sticky thing. This is stuff like parsing XML, accessing databases, graphics toolkit stuff, whatever. Things that aren't easy in C. Things that modern languages/frameworks will give for free. On OS X this code will be written in Objective-C interfacing with Cocoa. On Windows I'm thinking my best bet is to use C#

So on Windows my architecture (simplified) looks like

(C# or C?) -> ANSI C -> C#

Is this possible? Some thoughts/suggestions so far..

1) Compile my C core as a .dll -- this is fine, but seems there's no way to call my C# helpers unless I can somehow get function pointers and pass them to my core, but that seems unlikely

2) Compile a C .exe and a C# .exe and have them talk via shared memory or some kind of IPC. I'm not entirely opposed to this but it obviously introduces a lot of complexity so it doesn't seem ideal

3) Instead of C# use C++, it gets me some nice data management stuff and nice helper code. And I can mix it pretty easily. And the work I do could probably easily port to Linux. But I really don't like C++, and I don't want this to turn in to a 3rd-party-library-fest. Not that it's a huge deal, but it's 2010.. anything for basic data management should be built in. And targetting Linux is really not a priority.

Note that no "total" alternatives are OK as suggested in other similar questions on SO I've seen; java, RealBasic, mono.. this is an extremely performance intensive application doing soft realtime for game/simulation purposes, I need C & friends here to do it right (maybe you don't, but I do)

like image 945
Nektarios Avatar asked Jan 22 '23 21:01

Nektarios


1 Answers

Short answer: Yes. You can access non-managed code quite easily from .NET, but you'll have to marshal your data.

Long answer: Don't do it. You know about the mono project? It's a x-platform implementation of .NET. They're a bit behind Microsoft, but they still offer a solution that works for many. I'd highly suggest keeping in managed code if at all possible, you're defeating the purpose of .NET if you're in-and-outing to C code. This will also help reduce your project complexity tenfold.

If you require C ANSI for low-level access, I suggest you expose a small & well-tested C ANSI api to your .NET core to do any low-level stuff.

C# Core <==> Small C ANSI Helper Library

like image 176
Aren Avatar answered Jan 24 '23 11:01

Aren