将多维数组从c#编组到delphi

将多维数组从c#编组到delphi,c#,arrays,delphi,marshalling,C#,Arrays,Delphi,Marshalling,我正在将库从delphi重写为c#。在delphi客户端中使用以下参数调用库方法 T:=VarArrayCreate([1,2,1,1],varDouble); T[1,1]:=Tmin; T[2,1]:=Tmax; 我尝试将相同的数组传递给c#,如下所示: Array tVector = Array.CreateInstance(typeof(double), new[] { 2, 1 }, new[] { 1, 1 }); tVector.SetValue(Tmin, 1, 1);

我正在将库从delphi重写为c#。在delphi客户端中使用以下参数调用库方法

T:=VarArrayCreate([1,2,1,1],varDouble);
T[1,1]:=Tmin;
T[2,1]:=Tmax;
我尝试将相同的数组传递给c#,如下所示:

 Array tVector = Array.CreateInstance(typeof(double), new[] { 2, 1 }, new[] { 1, 1 });
 tVector.SetValue(Tmin, 1, 1);
 tVector.SetValue(Tmax, 2, 1);

 object T = tVector;
 Array tVector1 = Array.CreateInstance(typeof(double), new[] { 1 }, new[] { 1 });
 Array tVector2 = Array.CreateInstance(typeof(double), new[] { 1 }, new[] { 1 });
 Array tVector = Array.CreateInstance(tVector1.GetType(), new[] { 2 }, new[] { 1 });

 tVector.SetValue(tVector1, 1);
 tVector.SetValue(tVector2, 2);

 object T = tVector;
然后扔掉

System.Runtime.InteropServices.SEHException:“外部组件引发了异常。”

试着这样做:

 Array tVector = Array.CreateInstance(typeof(double), new[] { 2, 1 }, new[] { 1, 1 });
 tVector.SetValue(Tmin, 1, 1);
 tVector.SetValue(Tmax, 2, 1);

 object T = tVector;
 Array tVector1 = Array.CreateInstance(typeof(double), new[] { 1 }, new[] { 1 });
 Array tVector2 = Array.CreateInstance(typeof(double), new[] { 1 }, new[] { 1 });
 Array tVector = Array.CreateInstance(tVector1.GetType(), new[] { 2 }, new[] { 1 });

 tVector.SetValue(tVector1, 1);
 tVector.SetValue(tVector2, 2);

 object T = tVector;
然后扔掉

System.Runtime.InteropServices.SafeArrayTypeMismatchException:“指定的数组不是预期的类型。”

初始条件:

c#调用德尔菲法:

function  call (hModel: TModelHandle; const FunName: variant; var Params: variant; var Res: variant): TRetCode; stdcall;
Params是一个一维变量数组。我准备如下:

public object Call(string functionName, params object[] parameters)
{
        object res = new();

        object funName = functionName;
        
        Array endParamsArray = Array.CreateInstance(typeof(object), new[] {parameters.Length}, new[] {1});
        Array.Copy(parameters, endParamsArray, parameters.Length);
        object endParams = endParamsArray;
        
        var rc = call(hModel, ref funName, ref endParams, ref res);

        if (rc != TRetCode.rcOK)
            error(rc);

        return res;
}
然后我打电话:

 object T = tVector;

 PM.Call("funname", ID, Name, T, RamStab, Ktrans, sec_overcoming);
当传递的参数很简单时:int、string等,一切正常。但是通过数组我得到一个异常

这是Delphi上的客户机代码,一切都在其中运行:

 w:='funname'; FunName:=w;
Params:=VarArrayCreate([1,6],varVariant);
Params[1]:=ID;
Params[2]:=Name;
T:=VarArrayCreate([1,2,1,1],varDouble);
T[1,1]:=Tmin;
T[2,1]:=Tmax;
Params[3]:=T;
Params[4]:=RamStab;
Params[5]:=Ktrans;
Params[6]:=sec_overcoming;
rc:=PM.call(PM.hModel,FunName,Params,Res);
Bar:=PM.getObject(Name);
if Bar=nil then
  error('create');

找到您正在引用的内容:

在哪里

TRetCode=整数–完成代码

TModelHandle=THandle–指向模型的指针

所以我想说
TModelHandle
是一个
IntPtr
(32位带32位进程,64位带64位进程)。因此:

<>我已经用C++提出了一个C++的C++代码,它工作正常。参数正确传递,
res
正确传递

double Tmin = double.MinValue;
double Tmax = double.MaxValue;

int ID = 1;
string Name = "Foo";
double RamStab = 5;
long Ktrans = 3;
int sec_overcoming = 2;

Array tVector = Array.CreateInstance(typeof(double), new[] { 2, 1 }, new[] { 1, 1 });
tVector.SetValue(Tmin, 1, 1);
tVector.SetValue(Tmax, 2, 1);

object response = Call("Hello world", ID, Name, tVector, RamStab, Ktrans, sec_overcoming);

@Renat将此参数传递给delphi Library时引发异常不清楚谁在调用谁。。。如果是delphi调用c#,您可以尝试将您的方法定义为接收
[MarshalAs(UnmanagedType.SafeArray)]数组ar
,并查看它是否中断。如果它没有中断,那么我们可以研究如何提取数据,甚至
[Marshallas(UnmanagedType.SafeArray,SafeArraySubType=VarEnum.VT_R8)]数组ar
@xanatos相反,c#调用delphi方法。签名如下:函数调用(hModel:TModelHandle;const-FunName:variant;var-Params:variant;var-Res:variant):TRetCode;stdcall;Params在示例中是对象T。@Egorosh它传递的是变量,而不是safearray。。。。您可以像在第一个示例中那样创建数组,然后像这样传递它:
[marshallas(UnmanagedType.LPStruct)object obj]
。。。但我对第一个参数很不确定。(从技术上讲,它是在一个变体中传递一个安全数组)@xanatos我更改了问题的描述-添加的初始条件对我来说仍然不起作用((我检查了delphi,所有东西都在那里工作:我将写信给RMD开发人员……@Egorosh此时我很好奇问题是什么:-)所以当你发现它时,写下它:-)这是一个内部模型错误。在调用call之前,我没有在模型中设置所需的变量,结果抛出了一个未处理的异常there=)@Egorosh