Haruyuki Mohri

毛利春幸のブログです。

C++BuilderでDelphiのジェネリクスCollections TDictionary__2 などを使う場合

C++BuilderDelphiジェネリクスCollections TDictionary__2などを使う場合

How to Handle Delphi Generics in C++ - RAD Studio
上記URLにもありますが TList__1<TObject* >*こんなのを作るとilink32, ilink64エラーになります
f:id:mojeld:20170125172031g:plain

[ilink32 Error] Error: Unresolved external 'System::Generics::Collections::TList__1<System::Net::Httpclientcomponent::TNetHTTPClient *>::' referenced from C:\***\WIN32\DEBUG\UNIT1.OBJ
[ilink32 Error] Error: Unresolved external '__fastcall System::Generics::Collections::TList__1<System::Net::Httpclientcomponent::TNetHTTPClient *>::TList__1<System::Net::Httpclientcomponent::TNetHTTPClient *>()' referenced from C:\*\*\*\WIN32\DEBUG\UNIT1.OBJ
[ilink32 Error] Error: Unable to perform link

64ビット
[ilink64 Error] Error: Unresolved external 'vtable for System::Generics::Collections::TDictionary__2<int, System::UnicodeString>' referenced from C:\*\*\*\WIN64\DEBUG\UNIT1.O

Delphiジェネリクス型なので Delphi側で内部的に「TList__1<TObject* >*」を作る必要があります
C++プロジェクトにDelphiユニットを追加します。

unit Unit2;

interface
uses
  System.SysUtils, System.Types,  System.Generics.Collections, FMX.StdCtrls,
  System.Net.HttpClient, System.Net.HttpClientComponent;
type
  TDummyNetHttp = TList<TNetHTTPClient>;

Delphiのtype宣言のところだけにダミーを作ります。

 TList__1<TNetHTTPClient* >* net_list = new TList__1<TNetHTTPClient* >();

これでビルドが成功します。

 TList__1<TNetHTTPClient* >* net_list = new TList__1<TNetHTTPClient* >();
    for (auto i = 0; i < 10; i++) {
        net_list->Add(new TNetHTTPClient(nullptr));
    }

    _di_IHTTPResponse res = net_list->Items[0]->Get("http://localhost:8080/version");
    Memo1->Lines->Append(res->ContentAsString(TEncoding::UTF8));

www.youtube.com