1

The system we will be modifying consists of a bunch of applications that communicate via files (some write fils, others read them and so on). The applications are written in C++, one of them is now to be rewritten to C#. The problem is that this new app needs to read a file that contains a C++ struct written to it by another app using C++ fwrite. I have a C++ library that contains this struct and I guess I can reference it within my C# code but I have no control over the application producing the file.

Will it be possible, and if yes with how big an effort, to read a known C++ struct with a C# code?

agnieszka
  • 14,897
  • 30
  • 95
  • 113
  • 1
    Might be a duplicate of this question: http://stackoverflow.com/questions/17379492/marshal-c-struct-to-c-sharp – Codor May 12 '14 at 08:03
  • How is the struct written to the file? It may be simpler to use a common data-structure like XML for this. – The Forest And The Trees May 12 '14 at 08:15
  • Am missing what is the problem. If you create same struct in c# with same fields in same order, then read the file and convert it to struct using [Marshal.PtrToStructure](http://stackoverflow.com/questions/3278827/how-to-convert-a-structure-to-a-byte-array-in-c) – Sriram Sakthivel May 12 '14 at 08:15
  • It might be simpler to use XML but if you read the question carefully then it says "but I have no control over the application producing the file" which means I cannot change the app producing the file. Then chaning the format is not possible – agnieszka May 12 '14 at 08:18
  • Define structure in C#, read file into `byte[]`, use [Marshal.PtrToStructure](http://msdn.microsoft.com/en-us/library/vstudio/4ca6d5z7.aspx). And the problem is? – Sinatr May 12 '14 at 08:33
  • Everybody suggests XML, but when you want some textual intermediate form, YAML will be a much better choice. – cubuspl42 May 12 '14 at 09:03
  • @Sinatr : so are you saying I can define a structure with exactly the same fields in my C# code as the structure in this C++ dll and if I read a C++ struct from file and use this Marshal.PtrToStructure then it will magically create a C# struct out of it? – agnieszka May 12 '14 at 10:15
  • Yep, this is a right way to go, see example on msdn, it *magically* creates inner structure. Otherwise use manual approach (@Pete's answer). – Sinatr May 12 '14 at 11:23

1 Answers1

0

How many structs and how big? It may be easier, and probably more robust, to just write your own code to read them, something like

static ReadFoo (BinaryReader reader)
{
    Foo foo = new Foo();
    foo.thing = reader.ReadInt32();
    ...
    return foo;
}
Pete
  • 1,241
  • 2
  • 9
  • 21
  • I don't think so, I read that c++ and C# write data differently (long can have a different size) and you should not just read the data in a custom way... there are marshalling techniques, but I need to know more. – agnieszka May 12 '14 at 08:22
  • You should know what your binary exchange standard is. That probably mans freezing it at whatever defacto standard your C++ compiler has been spitting out. Unless you know that you are at the mercy of compiler changes anyway, even wihtout bring a different langauge into it. Once you do know that, you can write your code to read the appropriate fields. – Pete May 12 '14 at 08:25