Common General Definitions in C / C ++ (Unmanaged) and C # Managed Code

I have a set of structure definitions that are used by both C # managed components and C / C ++ unmanaged components. At present, identical definitions of structures exist separately in C / C ++ and C # code, which leads to duplication and the associated chaos. What is the best way to maintain separate definitions that can be used from both C # and C / C ++? Thank you Amit

PS: I'm a C / C ++ guy, so if there is an obvious way to do this, I could completely skip it!

+3
source share
3 answers

I am not familiar with your projects, but have you thought about creating a managed bridge for your library in C ++ / CLI ? With the hacker "It Just Works", the C ++ / CLI compiler does it many times for you, you can marshal and exchange managed types using your own code and back and forth.

Again, I do not know if this is right for you, without any particular details, but it might be worth a peek into it.

+3
source

you need IDL ( interface definition language ), try googling:

  • protocol buffers.
  • ICE (Internet connection mechanism).
  • Perhaps Microsoft COM ?.
  • - edit: new entry - it looks like microsoft has an IDL compiler .

It all depends on what you want. All of the above technologies have an IDL element for them and come with their own baggage. I personally would have stayed at a low level of C / C ++: D. Therefore, I would use Google "Imatix GSL" and use the indicated technology to model the problem in XML and create data structures in any programming language - this technology is very simple and thin and requires an experienced programmer, so if that doesn't make sense, you should stick with the IDL.

- change: programming method -

You can solve the problem with clean technology if you want. Chaos occurs when the planing technique breaks. If you decide on a firewall and encapsulate the problem in pure C / C ++ code, you don’t have to worry about the interface dropping in your dependent code - this is because any useful language can interact with your platform's ABI (simple C functions : P). The point is not to expose the internals, but simply an interface with opaque types, such as numeric descriptors that represent objects and functions that can be executed on your types.

+1
source

I once wanted to do this in one of my projects, which had a hard separation between C # code and C code. Ideally, C # code would borrow header files from C code, but:

  • since C # does not support include, I don’t see how you could share the definition of the structure and include it in both your C # code and C / C ++
  • with my definitions of structure in separate headers was inconvenient anyway
  • I didn’t want to rely on IDL or a custom “parsing C headers and only defining structural structures” preprocessing step
+1
source

Source: https://habr.com/ru/post/1338475/


All Articles