4

私はCOMライブラリを初めて使用し、C ++ Builder(XE2)アプリケーションでCOMDLLを使用することに固執しています。DLLが登録されています。そのようなDLLに属するオブジェクトを作成し、それらのメソッドを呼び出すことができる手順はどれですか?私は静的に意味します。

さまざまな方法を見ましたが、チュートリアルが見つかりませんでした。

  1. コンポーネント>コンポーネントのインポート>新しいラッパーユニットを生成します...そして何ですか?
  2. 絶対パスでDLLをインポートします(なぜですか?システムに登録されています)

    #import "C:\Path\to\the\LIB1.dll" rename_namespace ("LIB1")
    

    ...そして何?

  3. 使用CoCreateInstance...どのくらい正確に?インポート/インクルードなし?

usingVisual C#では、参照と!を追加するだけで処理します。

私はとても混乱しています!どんな助けでも大歓迎です。

4

1 に答える 1

1

I found a way (but tell me if there are better ones):

  • Component > Import component... > Import a Type Library > select the library
  • Unit Dir Name = and uncheck "Generate Component Wrappers"
  • "Add unit to MyProject.cbproj project" > Finish
  • in the client class > File > Use Unit... > select the unit that was created
  • in the client class write this code for using the COM DLL:

    CoInitialize(NULL); //Init COM library DLLs  
    
    ICompany *company;        
    
    HRESULT hr = CoCreateInstance ( CLSID_Company,  
                                    NULL,  
                                    CLSCTX_INPROC_SERVER,  
                                    IID_ICompany,  
                                    (void**) &company );  
    if (SUCCEEDED (hr)) {  
         //TODO here you can use your company object!
         //and finally release such resource
         company->Release();  
    }  
    
    CoUninitialize();
    

Where Company was the original class, exposed by the DLL, which I wanted to intantiate.

Introduction to COM - What It Is and How to Use It. helped me a lot.

Note that this requires the creation of *_TLB.* and *_OCX.* units. Is there a way that avoids it?

于 2012-11-20T10:59:05.997 に答える