Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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.

Code Block
languagecpp
using System.Runtime.InteropServices; //add to top of source code to include Marshal

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

...

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()

Code Block
languagecpp
int response;
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);
}
Code Block
languagecpp
int response;
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);
}

...