C++结构体与Delphi结构体相互传参,结构体中包含结构体的嵌套,数组指针

//结构体的声明


typedef struct Mwinddirectbaseline {
    char* p;
    int s;
    int i;
}Mwinddirectbaseline;


typedef struct F {
    char* p;
    int s;
    int i;
}F;

typedef struct H {
    char* p;
    int s;
    int i;
}H;



typedef struct Coordinate1 {
    char* p;
    int s;
    int i;
}Coordinate1;

typedef struct Mwinddirectbar {
    F f;
    H h;
}Mwinddirectbar;


typedef struct Huxianarray {
    float* Params;
}Huxianarray;

typedef struct Hengxianarray {
    float* Params;
}Hengxianarray;


typedef struct Windspeedline {
    char* types;
    Coordinate1** coordinates;
}Windspeedline;


typedef struct Resultwindspeedgrid {
    int HuxianShu;
    int HengxianShu;
    Huxianarray* HuxianArrays;
    Hengxianarray* HengxianArrays;
}Resultwindspeedgrid;


typedef struct Coordinate {
    Mwinddirectbaseline *mWindDirectBaseLine;
    Mwinddirectbar *mWindDirectBars;
}Coordinate;


typedef struct Winddirectline {
    Coordinate* coordinates;
}Winddirectline;

//typedef struct Structtest {
//    int a;
//    int b;
//    int* arr;
//}Structtest;


typedef struct Rootobject {
    char* gridStartTime;
    char* gridEndTime;
    char* resultStartTime;
    char* resultEndTime;
    int gridMethod;
    char* WindDirectBorder;
    Winddirectline WindDirectLine;
    char* WindSpeedBorder;
    Windspeedline WindSpeedLine;
    /*void* LineDateTimeRelations;
    void* RatioConverts;*/
    //void* 不确定类型
    char* resultWindDirectGrid;
    Resultwindspeedgrid resultWindSpeedGrid;


}Rootobject;
C++结构体的声明
__declspec(dllexport) Rootobject __stdcall RefRootobjectValues(char* path)
{
    ifstream t(path);
    Rootobject Root;
    string str((istreambuf_iterator<char>(t)), istreambuf_iterator<char>());
    CJsonObject Objson = CJsonObject(str);
    string strsss;

    Objson.Get("gridStartTime", strsss);
    Root.gridStartTime = ReturnCharArr(strsss);

    Objson.Get("gridEndTime", strsss);
    Root.gridEndTime = ReturnCharArr(strsss);

    Objson.Get("resultStartTime", strsss);
    Root.resultStartTime = ReturnCharArr(strsss);

    Objson.Get("resultEndTime", strsss);
    Root.resultEndTime = ReturnCharArr(strsss);

    Objson.Get("WindSpeedBorder", strsss);
    Root.WindSpeedBorder = ReturnCharArr(strsss);

    Objson.Get("WindDirectBorder", strsss);
    Root.WindDirectBorder = ReturnCharArr(strsss);
    

    Objson.Get("resultWindDirectGrid", strsss);
    Root.resultWindDirectGrid = ReturnCharArr(strsss);


    int nums = Objson["WindDirectLine"]["coordinates"].GetArraySize();

    Root.WindDirectLine.coordinates = (struct Coordinate *)malloc(nums * sizeof(struct Coordinate));
    /*Coordinate* coor=new Coordinate [nums];*/
    /*由于School_T中定义的student是一个结构体指针,必须要对其进行分配内存*/
    //Root.WindDirectLine->coordinates = (coordinates *)malloc(sizeof(Student_T));

    for (int i = 0; i < nums; i++)
    {
        int mWindDirectBaseLine_Length = Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBaseLine"].GetArraySize();
        Root.WindDirectLine.coordinates[i].mWindDirectBaseLine = (struct Mwinddirectbaseline *)malloc(nums * sizeof(struct Mwinddirectbaseline));
        for (int j = 0; j < mWindDirectBaseLine_Length; j++)
        {
            string Str1;
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBaseLine"][j].Get("p", Str1);
            Root.WindDirectLine.coordinates[i].mWindDirectBaseLine[j].p = ReturnCharArr(Str1);
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBaseLine"][j].Get("i", Root.WindDirectLine.coordinates[i].mWindDirectBaseLine[j].i);
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBaseLine"][j].Get("s", Root.WindDirectLine.coordinates[i].mWindDirectBaseLine[j].s);
            Root.WindDirectLine.coordinates[i].mWindDirectBaseLine++;
        }

        int mWindDirectBars_Length = Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"].GetArraySize();
        Root.WindDirectLine.coordinates[i].mWindDirectBars = (struct Mwinddirectbar *)malloc(nums * sizeof(struct Mwinddirectbar));
        for (int k = 0; k < mWindDirectBars_Length; k++)
        {
            string Str2;
            F f;
            H h;
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"][k]["f"].Get("p", Str2);
            f.p = ReturnCharArr(Str2);
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"][k]["f"].Get("s", f.s);
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"][k]["f"].Get("i", f.i);
            Str2 = "";
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"][k]["h"].Get("p", Str2);
            h.p = ReturnCharArr(Str2);
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"][k]["h"].Get("s", h.s);
            Objson["WindDirectLine"]["coordinates"][i]["mWindDirectBars"][k]["h"].Get("i", h.i);
            Root.WindDirectLine.coordinates[i].mWindDirectBars[k].f = f;
            Root.WindDirectLine.coordinates[i].mWindDirectBars[k].h = h;
            Root.WindDirectLine.coordinates[i].mWindDirectBars++;
        }
        Root.WindDirectLine.coordinates++;
    }

    return Root;
    //strs->WindDirectLine.coordinates = coor;
    //strs->WindDirectLine->coordinates = coor;
    //strs->gridMethod = 50;

}
C++赋值并返回
 type
    Huxianarray = record
      Params:^ double;
    end;

  type
    Hengxianarray  = record
      Params:^ double;
    end;

   type
    Resultwindspeedgrid = record
       HuxianShu:Integer;
       HuxianArrays:^ Huxianarray;
       HengxianShu:Integer;
       HengxianArrays:^ Hengxianarray;
    end;

   type
    Coordinate1 = record
       p:pansiChar;
       s:Integer;
       i:Integer;
    end;

   type
    Windspeedline =  record
         types:pansiChar;
         coordinates:^ Coordinate1;
    end;

    type
    H  = record
        p:pansiChar;
        s:Integer;
        i:Integer;
    end;

   type
    F =  record
        p:pansiChar;
        s:Integer;
        i:Integer;
    end;


   type
    Mwinddirectbar = record
        f:F;
        h:H;
    end;
    TMyArraybar = array of Mwinddirectbar;
    PMyArraybar = ^TMyArraybar;

    type
    Mwinddirectbaseline  = record
       p:pansiChar;
       s:Integer;
       i:Integer;
    end;


    type
    Coordinate = record
//    TCoordinate = record
         mWindDirectBaseLine: ^Mwinddirectbaseline;
         mWindDirectBars: ^Mwinddirectbar;
    end;



   type
    Winddirectline  = record
      coordinates: ^Coordinate;
    end;

     type
    Rootobject = record
         gridStartTime:pansiChar;
         gridEndTime:pansiChar;
         resultStartTime:pansiChar;
         resultEndTime:pansiChar;
         gridMethod:Integer;
         WindDirectBorder:pansiChar;
         WindDirectLine:Winddirectline;
         WindSpeedBorder:pansiChar;
         WindSpeedLine:Windspeedline;
//         LineDateTimeRelations:pointer;
//         RatioConverts:pointer;
         resultWindDirectGrid:pansiChar;
         resultWindSpeedGrid:Resultwindspeedgrid;
    end;
Delphi结构体的声明
function RetRootobjectValues(b:pansiChar;var n1:pansiChar;var mu:Integer):Rootobject;stdcall;external 'F:\资料文档\20190227\测试文件夹\Win32DLL\Debug\Win32DLL.dll';

procedure TForm5.Button4Click(Sender: TObject);
var
root:Rootobject;
path,res:pansiChar;
mu,I,hsf,hsh:Integer;
nums:Mwinddirectbaseline;
resd:Coordinate;
Mwind: Mwinddirectbaseline;
Mwbar:Mwinddirectbar;
bol,bolbar:boolean;
fs:F;
hs:H;
begin
     root.gridMethod:=0;
     path:=  pansiChar(AnsiString('E:\123.txt'));
     root:= RetRootobjectValues(path,res,mu);
//     resd:= root.WindDirectLine.coordinates^  ;
//     Mwind:=  root.WindDirectLine.coordinates^.mWindDirectBaseLine^;
//     Inc(root.WindDirectLine.coordinates);
         bol:=true;

            while(bol) do
            begin
               resd:= root.WindDirectLine.coordinates^  ;
               try
                 Mwind:= root.WindDirectLine.coordinates^.mWindDirectBaseLine^;
                 Mwbar:= root.WindDirectLine.coordinates^.mWindDirectBars^;
                 fs:=Mwbar.f;
                 hs:=Mwbar.h;
                 hsf:= fs.s;
                 hsh:=hs.s;

                 bolbar:=true;
                 mu:= Mwind.s;

                 while((mu<>-1414812757)) do
                 begin
                  Inc(root.WindDirectLine.coordinates^.mWindDirectBaseLine); {到下一个}
                  Mwind:=  root.WindDirectLine.coordinates^.mWindDirectBaseLine^;
                  mu:= Mwind.s;
                 end;

                 while((hsf<>-1414812757) and (hsh<>-1414812757) ) do
                 begin
                   Inc(root.WindDirectLine.coordinates^.mWindDirectBars); {到下一个}

                      Mwbar:= root.WindDirectLine.coordinates^.mWindDirectBars^;
                      fs:=Mwbar.f;
                      hs:=Mwbar.h;
                      hsf:= fs.s;
                      hsh:=hs.s;

                 end;
               except
               begin
                  bol:=false;
               end;
               end;
               Inc(root.WindDirectLine.coordinates); {到下一个}
            end;


//     resd^:=root.WindDirectLine.coordinates;
//     Mwind[0]:=  resd.mWindDirectBaseLine;
//     Mwbar:=   resd.mWindDirectBars;
//      for I := 0 to 3 do
//      begin
//       resd:= root.WindDirectLine.coordinates;
//
//      end;


     ShowMessage(root.gridStartTime);


end;
Delphi调用方法

 其中Json文件的读取运用了C++ Json解析CJsonObject的详细使用

代码中只展示了部分参数的赋值。

如果出现读取内存地址报错的情况,在C++的ReturnRootobject方法中添加 __stdcall。

你可能感兴趣的:(C++结构体与Delphi结构体相互传参,结构体中包含结构体的嵌套,数组指针)