Skip to end of banner
Go to start of banner

C# Code Sequence particulars

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Calling library functions in C# is very similar to the C/C++ example below from the Basic Demo Code Sequence. The only significant difference is the necessity to use the Marshal class when passing pointers.

For example, when getting the F_Report_Message() string value, it should be retrieved as follows:

String text;
text = Marshal.PtrToStringAnsi(FLASHPROARM_API.F_Report_Message());	

Another commonly used example is reading and writing using byte arrays, i.e. using functions F_Read_Bytes_Block() and F_Write_Bytes_Block_to_RAM()

const Int32 size = 64;
Int32 addr;
byte []data_block = new byte[size];
IntPtr data_block_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(data_block[0]) * data_block.Length);

addr = mcu.x.RAMStartAddr;//or use custom address
for(k=0; k<size; k++)
  data_block[k] = (byte)(0x30 + k);

try
{
    Marshal.Copy(data_block, 0, data_block_ptr, size);
    unsafe
    {
        //----------------------------------------------------
        response = FLASHPROARM_API.F_Write_Bytes_Block_to_RAM(addr, size, (byte*)data_block_ptr);
        //----------------------------------------------------
    }
}
finally
{
    Marshal.FreeHGlobal(data_block_ptr);
}
const Int32 size = 64;
byte[] data_block = new byte[size];
IntPtr data_block_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(data_block[0]) * data_block.Length);

addr = mcu.x.RAMStartAddr;//or use custom address
//----------------------------------------------------
try
{
    unsafe
    {
        response = FLASHPROARM_API.F_Read_Bytes_Block(addr, size, (byte*)data_block_ptr);                    
    }
    Marshal.Copy(data_block_ptr, data_block, 0, data_block.Length);
}
finally
{
    Marshal.FreeHGlobal(data_block_ptr);
}

Basic code example below is compatible with C#:

C/C++ Basic Demo Code Sequence

  • No labels