S
suiram
Guest
In the C++ dll documentation I have the function prototype:
Int Download_Firmware(Uint Handle, char pcFilePath[]);
This function programs the uC. The pcFilePath is the path where is located the hexadecimal file (included in the attached file) to program the uC. The char array should be null terminated (finish with 0).
I read all the discussions similar to this subject and I thought I understood how to declare and use the function in C# but I wasn’t able to get a good result. I know the problem is with marshaling parameters from C++ dll to C# because I have other functions in the dll that works. I know the pcFilePath[] buffer is 14924 bytes. Adding one 0 byte make it 14925.
I tried to use the dll function in two ways: 1) using an IntPtr; 2) using unsafe-fixed
1 - using an IntPtr:
[DllImport(@"\ADI_CYUSB_USB4.dll", EntryPoint = "Download_Firmware")]
public static extern int Download_Firmware_Imp(uint Handle, IntPtr array);
public byte[] FirmWareAddress = new byte[14925];
public void ParseString(string Str)
{
int i;
for(i = 0;i<(Str.Count());i++) // Str.Count() = 14924
{
FirmWareAddress = (byte)Str;
}
//it has to end with a 0!
FirmWareAddress = (byte)0;
}
string text = System.IO.File.ReadAllText(@"E:\Projects\ AD5360\bin\Debug\AD5371SPI.hex");
int size = Marshal.SizeOf(FirmWareAddress[0]) * FirmWareAddress.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(FirmWareAddress, 0, pnt, FirmWareAddress.Length);
last_error = Download_Firmware_Imp(Handle, pnt);
Thread.Sleep(100);
Marshal.FreeHGlobal(pnt);
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
2 - using unsafe-fixed:
[DllImport(@"\ADI_CYUSB_USB4.dll", EntryPoint = "Download_Firmware")]
public static unsafe extern int Download_Firmware_Imp(uint Handle, byte* array);
public byte[] FirmWareAddress = new byte[14925];
public void ParseString(string Str)
{
int i;
for(i = 0;i<(Str.Count());i++) // Str.Count() = 14924
{
FirmWareAddress = (byte)Str;
}
//it has to end with a 0!
FirmWareAddress = (byte)0;
}
unsafe
{
fixed (byte* outBuf = FirmWareAddress)
{
last_error = Download_Firmware_Imp(Handle, outBuf);
}
}
1) And 2) make the program to crash when Download_Firmware_Imp is called: “vshost32.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.”
Please help me.
Continue reading...
Int Download_Firmware(Uint Handle, char pcFilePath[]);
This function programs the uC. The pcFilePath is the path where is located the hexadecimal file (included in the attached file) to program the uC. The char array should be null terminated (finish with 0).
I read all the discussions similar to this subject and I thought I understood how to declare and use the function in C# but I wasn’t able to get a good result. I know the problem is with marshaling parameters from C++ dll to C# because I have other functions in the dll that works. I know the pcFilePath[] buffer is 14924 bytes. Adding one 0 byte make it 14925.
I tried to use the dll function in two ways: 1) using an IntPtr; 2) using unsafe-fixed
1 - using an IntPtr:
[DllImport(@"\ADI_CYUSB_USB4.dll", EntryPoint = "Download_Firmware")]
public static extern int Download_Firmware_Imp(uint Handle, IntPtr array);
public byte[] FirmWareAddress = new byte[14925];
public void ParseString(string Str)
{
int i;
for(i = 0;i<(Str.Count());i++) // Str.Count() = 14924
{
FirmWareAddress = (byte)Str;
}
//it has to end with a 0!
FirmWareAddress = (byte)0;
}
string text = System.IO.File.ReadAllText(@"E:\Projects\ AD5360\bin\Debug\AD5371SPI.hex");
int size = Marshal.SizeOf(FirmWareAddress[0]) * FirmWareAddress.Length;
IntPtr pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(FirmWareAddress, 0, pnt, FirmWareAddress.Length);
last_error = Download_Firmware_Imp(Handle, pnt);
Thread.Sleep(100);
Marshal.FreeHGlobal(pnt);
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
2 - using unsafe-fixed:
[DllImport(@"\ADI_CYUSB_USB4.dll", EntryPoint = "Download_Firmware")]
public static unsafe extern int Download_Firmware_Imp(uint Handle, byte* array);
public byte[] FirmWareAddress = new byte[14925];
public void ParseString(string Str)
{
int i;
for(i = 0;i<(Str.Count());i++) // Str.Count() = 14924
{
FirmWareAddress = (byte)Str;
}
//it has to end with a 0!
FirmWareAddress = (byte)0;
}
unsafe
{
fixed (byte* outBuf = FirmWareAddress)
{
last_error = Download_Firmware_Imp(Handle, outBuf);
}
}
1) And 2) make the program to crash when Download_Firmware_Imp is called: “vshost32.exe has stopped working. A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available.”
Please help me.
Continue reading...