I keep getting this error when I try to run my code, "Object reference not set to an instance of an object." I'm assuming something in my struct isn't done correctly because that's where the error seems to be coming from. Any help on the matter would be appreciated, Thanks.
Code:
public struct MMFKeyboardCommand
{
public int Command;
public fixed char Alias[256];
public fixed char Param1[256];
public fixed char Param2[256];
public fixed char Param3[256];
public fixed char Param4[256];
public fixed char Text[2048];
}
public struct MMFKeyboardControlMapping
{
public ulong NumberOfCommands;
public bool Handled;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=32, ArraySubType= UnmanagedType.I1)]
public MMFKeyboardCommand[] Commands;
}
MMFKeyboardControlMapping mmfkc = new MMFKeyboardControlMapping();
private void button1_Click(object sender, EventArgs e)
{
int TestSize = Marshal.SizeOf(typeof(MMFKeyboardCommand));
MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("Test", TestSize, MemoryMappedFileAccess.ReadWriteExecute);
MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(0, TestSize);
for (long i = 0; i < TestSize; i += TestSize)
{
//accessor.Read(i, out _test);
string str = "True";
fixed (char* buffer = mmfkc.Commands[0].Param1)
{
mmfkc.Commands[0].Command = 0x02;
memset(buffer, 0, 256);
for (int c = 0; c < str.Length; c++)
{
buffer[c] = str[c];
}
}
mmfkc.NumberOfCommands = 1;
mmfkc.Handled = false;
accessor.Write(i, ref mmfkc);
}
}
Comment