Generating C# structure from C structure

て烟熏妆下的殇ゞ 提交于 2019-12-13 16:20:11

问题


I have several dozens of C structures and I need to use them in C#. A typical C structure looks like this one:

typedef struct _UM_EVENT
{
   ULONG32 Id;
   ULONG32 Orgin;          
   ULONG32 OperationType;  
   ULONG32 Size;           
   LARGE_INTEGER Time;     
   HANDLE ProcessId;
   HANDLE ThreadId;
} UM_EVENT, *PUM_EVENT;

To use this structure in C# I declare it in C# and then do the marshalling stuff, which can be done roughly in this way:

public struct UM_EVENT
{
   public UInt32 Id;             
   public UInt32 Orgin;          
   public UInt32 OperationType;  
   public UInt32 Size;           
   public UInt64 Time;           
   public IntPtr ProcessId;      
   public IntPtr ThreadId;       
}

private void Load(IntPtr event) {

   unsafe
   {
       UM_EVENT header = *(UM_EVENT*)event;
   }
}

IMHO, this approach has the following disadvantages if the code is written a lot of times:

  1. The coding is not fast and does not require much thinking
  2. The code will likely contain many bugs that can remain undetected for long time
  3. The code is not very maintainable, because a change in a C structure requires the same change in the C# structure

So, does anybody know a util that would generate the presented C# code from given C structures automatically? Or is there a completely different way how to comfortably use a large number of C structures in C#?

来源:https://stackoverflow.com/questions/12117407/generating-c-sharp-structure-from-c-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!