• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Software Engineer

Is it possible in C# to use the  structs defined in an unmanaged header file?
0
Barbless
Asked:
Barbless
  • 3
  • 2
  • 2
3 Solutions
 
gregoryyoungCommented:
No you can't the syntax is slightly different. Writing a conversion is fairly simple though.
0
 
BarblessAuthor Commented:
Is there any other way out?

What I'm trying to avoid is to have 2 sets of struct definitions, one for C# and the other for C. This way, I don' t have to worry about breaking the SW if one is changed but forgot to make corresponding change to the other.
0
 
gregoryyoungCommented:
Not a good way unfortunately ... In particular since you will also need things like layout attributes in C#
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
josgoodCommented:
Write a conversion utility, as gregoryyoung suggested.  

Pick one header or the other as the header you edit by hand.  

As a pre-build step in your C or C# (whichever is the one you didn't edit by hand), run the conversion utility you wrote.

This way you only have to edit one header and the other will track automatically.
0
 
BarblessAuthor Commented:
Thanks gregoryyoung and josgood, I think I'll write the conversion util, as you both suggested.

One question though, how do I read the converted file in C#? Would it be something simple like using "using"?
0
 
josgoodCommented:
Sounds like you want to write the conversion utility to take a C header and emit a C# .cs file.

I've assumed all along that you would include the .cs file in your project.
0
 
gregoryyoungCommented:
well you could compile it into a .dll and use using ... or you could just include the .cs files in your compile (or add to your project if you build through VS.NET)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now