I have created a .dll file using SIMULINK's embedded coder. (System target file is set to : ert_shrlib.tlc) It successfully builds into Model_win64.dll. Now I want to reference it from Visual studios.
I first tried using the "add reference" tool but got the following error: A reference to 'file path\\Model_win64.dll'
I then searched around for a solution online and am getting that this error is cause by the .dll file being a native .dll so I should use the DllImportAttribute Class https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx
So the code I have currently is
using System;
using System.Runtime.InteropServices;
namespace GUI_Interface
{
class Main
{
[DllImport("Modbot_Model_win64.dll", CharSet = CharSet.Unicode)]
public static extern int[] MPC(double x, double y, double theta, double Vx, double Vy, double Vtheta);
public static BackgroundWorker test()
{
ints = MPC(0, 0, 0, 0, 0, 0);
}
}
And I get the runtime error:Cannot marshal 'return value': Invalid managed/unmanaged type combination. I've tried reading up on managed/unmanaged types but I just can't wrap my head around it in order to solve this issue. Any proposed solutions or info on managed/unmanaged would be greatly appreciated.
You can not directly use int[] as return type. It's managed but the dll returned is not.
Change return type to "IntPtr" to receive a pointer.
[DllImport("Modbot_Model_win64.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr MPC(double x, double y, double theta, double Vx, double Vy, double Vtheta);
Then use Marshall copy to get the data
IntPtr ints = MPC(0, 0, 0, 0, 0, 0);
int ret[] = new int[N]; //N is the number of elements
Marshall.Copy(ints, ret, 0, N);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.