I'm currently trying to use sam-ba.dll (documentation here) in one of our team's C# Windows Forms Apps for programming the bootloader and microcode. In order to simplify its usage, I've written a wrapper class (attached) to handle all of the pointers and unsafe code. Unfortunately, two functions that I keep having trouble with are AT91Boot_Scan and AT91Boot_Open.
AT91Boot_Scan: The purpose of this function is to scan and return a list of connected devices. Whenever I call the AT91Boot_Scan function, the code in the dll throws an "A heap has been corrupted" error and crashes my program. I suspect the problem is in the way I allocated memory for the buffers, or the way I passed the pointers to the function, but I can't seem to figure out exactly what's causing this error. The following is code for how I created the array of buffers, and how I passed the pointer for said array to AT91Boot_Scan.
// Allocate an array (of size maxNumDevices ) of pointers
for (int n = 0; n < maxNumDevices; n++)
// Assign the buffer to a pointer in the deviceList
// Create a pointer to the deviceList. The byte is at the first byte in the array.
AT91Boot_Open: Depending on the given device name, this function is supposed to return a communication handle with the device. However, currently the AT91Boot_Open function is always returning 0x00000000 as the communication handle. What I've found is that it will do this when it is given a name that doesn't match any of the connected devices, i.e. "foobar". However, I can't figure out how I could possibly be passing it an incorrect name. The names that I am typically passing are "\jlink\ARM0\SN:<serial number>"
|ISAMBADLL SamBA = new SAMBADLL();
byte byteName = Encoding.ASCII.GetBytes(deviceName);
fixed (byte* byteNamePtr = byteName)
SamBA.AT91Boot_Open(ref *byteNamePtr, out deviceHandle);
Console.WriteLine("Error getting Handle");
deviceHandle = -2; // Arbitrary error code
Other Thoughts: In my installation of SAM-BA V2.16,there are code examples for C++ . Would there happen to be any for C#?