• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//结构体的声明


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。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小心,Delphi函数的返回值你必须管!发布时间:2022-07-18
下一篇:
delphi中if与else发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap