Skip to content

Instantly share code, notes, and snippets.

@lyf-is-coding
Last active August 21, 2022 05:07
Show Gist options
  • Save lyf-is-coding/576eb6520332d306a1427e53adb5f0f8 to your computer and use it in GitHub Desktop.
Save lyf-is-coding/576eb6520332d306a1427e53adb5f0f8 to your computer and use it in GitHub Desktop.
C# UnmanagedType example
public struct Vector3
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct Enemy
{
public Enemy(Vector3 position, float health, Vector3[] bones)
{
Position = position;
Health = health;
Bones = bones;
}
[MarshalAs(UnmanagedType.Struct)]
public Vector3 Position;
[MarshalAs(UnmanagedType.R4)]
public float Health;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public Vector3[] Bones;
}
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct ResponseEnemyData
{
// Unmanaged array has size: 50
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public Enemy[] Enemies;
[MarshalAs(UnmanagedType.I4)]
public int EnemyCount;
}
// Init unmanaged array
{
...
ResponseEnemyData resp;
resp.Enemies = new Enemy[50];
...
}
public enum CommandID
{
NONE = 0x1,
CLOSE_NAMEDPIPE_SERVER,
};
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct RequestCommand
{
[MarshalAs(UnmanagedType.I4)]
public CommandID CommandID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment