Can I use C headers for protocol in C #

I am having a technical problem with the new C # program that I am developing. In this project, I need to establish a connection with another Windows-based system on a TCP / IP network. Al software written on another system is running in C, and any other future development will also be done in C / C ++. All protocols are run in C by another engineer, and protocol definitions are done using a C typedef struct that defines all the variables, and using memcpy to retrieve / place data packets that are great for C. All my protocols come in C header files with all typesdef and struct types in them, and any changes made to the protocol in the future will be performed in a similar way.

My question is, is there a way to use them in C #?

I tried to compile them as a class into a DLL, but did not work, because C # can only use managed C dlls. If I try to compile C as a managed class, it’s just a mess because there are a lot of arrays in the protocol, and since the C code must match the set of mill specifications, many of the variables were typedef. Now I can go and redo all the structures on C #, but it will take a lot of time, and I will have to redo it every time a change or something added to the protocol occurs. Not to mention the danger of slipping errors every time I do this.

How this works with my C projects is that another engineer will just provide me with updated header files.

So, is there a way to use these header files directly in C # or an automatic conversion that I can do every time the protocol is updated? Well, I basically need to use this header file to extract data from a data stream coming through a TCP / IP connection (without the possibility of using memcpy).

The reason for using C # is because I use a lot of graphics in WPF, and Visual C ++ does not support WPF

Any help or suggestions would be greatly appreciated?

+7
source share
3 answers

I once had to use the C headers in C # to get the definitions of sorted structures sent over TCP / IP. The approach we used was to analyze the header files using a T4 text template. This is a slightly more complicated task, but you need to write a C parser well enough for your headers and use it to create a .cs file, so there are a lot of clutter. It was a good enough solution for us, so it can also help you.

Take a look at T4 here: http://msdn.microsoft.com/en-us/library/bb126445.aspx

+1
source

Not quite the answer, more like one possible good solution:

Create a definition file containing the information that is now in the C header file. Then use this to generate both the .h header and the appropriate C # source code.

If the data is quite simple, then you can use a simple key file format or even a csv file. But if it is more complicated, then it is best to use XML, which is easier to parse programmatically.

If there is resistance to having a language-independent definition file, then you can try to get the .c header file to follow some string formatting rules, so you can just parse it and generate C # code (just make sure that the one who writes .h, understands that it is no longer C, this is actually your own C-shaped definition language, and any additional C material should go to another file).

+1
source

You cannot and cannot use the header file in C #, which you need to compile it in dll and C #

in file c you need to define #define DLLAPI __declspec(dllexport) and define methods like the following DLLAPI *return-value-data-type function-name* and with C # you need to name it as shown below

 [DllImport(@"*dll-path*")] public static extern *return-value-datatype function-name* 

and if necessary, you will sort by data type, for example, the following

 [DllImport(@"*dll-path*")] public static extern void InitParam([MarshalAs(UnmanagedType.LPWStr)] string inputFile, [MarshalAs(UnmanagedType.LPWStr)] string outputFile, [MarshalAs(UnmanagedType.LPWStr)] string templateFile, [MarshalAs(UnmanagedType.LPWStr)] string userName, [MarshalAs(UnmanagedType.LPWStr)] string manifestFilePath, [MarshalAs(UnmanagedType.LPWStr)] string usersRightList); [DllImport(@"*dll-path*")] public static extern Int32 ProtectDocument( [MarshalAs(UnmanagedType.LPStr)]string validToDate); [DllImport(@"*dll-path*")] public static extern void DebugGeneratedFiles( [MarshalAs(UnmanagedType.LPWStr)] string singedIssuenceLicenseFilePath, [MarshalAs(UnmanagedType.LPWStr)] string encryptedContentOutputFilePath); 
0
source

All Articles