This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C# wrapper for BSL430 DLL?



Has anybody done a C# wrapper for the BSL430 DLL, the DLL that controls loading of the application to the MPS430 by using its on-board BSL?

I call BSL_setFamily and BSL_setCom from C# OK which I use to set up for a USB download.

BSL_initialize_BSL is also OK.

However, something that uses the DataBlock, for example, BSL_RX_Password generates exceptions in C#.

I have the structure like this in C#:

[

StructLayout(LayoutKind.Sequential)]
public unsafe struct DataBlock
{
   
public fixed byte data[10240];
   
public uint numberOfBytes;
   
public uint startAddr;
};

The function like this:

[

DllImport("BSL430.dll", EntryPoint = "BSL_RX_Password", CallingConvention = CallingConvention.Cdecl)]
private static extern byte BSL_RX_Password(DataBlock data);

I have tried many combinations of the structure including various "MarshallAs" and "FieldOffset" but everything I try either gives a memory use exception (reading or writing to protected memory) or a mixed managed/unmanaged exception.

Below I have given examples of two C# wrappers for DataBlock and text comments above them for the exceptions I get when using them:

 

// System.AccessViolationException: Attempted to read or write protected memory.
// This is often an indication that other memory is corrupt

[

StructLayout(LayoutKind.Sequential)]
public unsafe struct DataBlock
{
   public fixed byte data[10240];
   public uint numberOfBytes;
   public uint startAddr;
};

// Cannot marshal field 'data' of type 'DataBlock': Invalid managed/unmanaged type
// combination (this value type must be paired with Struct)

[StructLayout(LayoutKind.Explicit, Size = 10248)]
public unsafe struct DataBlock
{
    [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, SizeConst=10240)]
    public fixed byte data[10240];

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(10240)]
    public uint numberOfBytes;

    [MarshalAs(UnmanagedType.U4)]
    [FieldOffset(10244)]
    public uint startAddr;
};

So, does anybody have a C# wrapper for this DLL API?

  • Hi Rob,

    I'm having the same problem when I try to call "BSL_initialize_BSL" (I get the "writing to protected memory" exception).

    Did you found a solution for this?

    Thanks,

    Javi.

  • I am currently trying to get a working C# wrapper for this DLL API as well, but I have had no luck.

    If anyone else is working on something similar feel free to contact me via e-mail at bmoore@optics1.com to discuss implementation.

  • I would like to use the BSL DLL with my C# application also. Has anyone been successful with this?

  • Just a guess, maybe you need to pass a pointe rto the struct, not the struct itself.

    if you pass a struct, it should be read-only. The fact that you get a write error, seems to indicate that there will be something written (which you probably want returned).

    Perhaps the DLL tries to write to the memory which seems to be pointed at by the first data of the struct, instead to the struct itself through its pointer.

    It's also possible that the calling convention is not Cdecl. This is normally not a big problem, as long as single ints are passed and returned, but when passing multiple or complex arguments, these are put on stack and both, the pushing order and the removal are handled differently depending on calling convention.

**Attention** This is a public forum