CApplication is from CATIA,>> class CApplication : public COleDispatchDriver

Given that CApplication is a wrapper class generated by Visual Studio to interact with CATIA's COM objects, and it is derived from COleDispatchDriver, you do not need to use __uuidof(CApplication) directly. Instead, you should use the CApplication class as it is designed, leveraging the COleDispatchDriver functionalities provided by MFC (Microsoft Foundation Classes).

Understanding the COleDispatchDriver Wrapper

COleDispatchDriver is a class provided by MFC to simplify the use of COM IDispatch interfaces, which are common in Automation objects like those exposed by CATIA. The COleDispatchDriver class encapsulates the complexities of calling methods and accessing properties on IDispatch-based COM objects.

When you use class CApplication : public COleDispatchDriver, it provides a thin wrapper around the CATIA COM interface, allowing you to call methods and properties directly without dealing with low-level COM details like QueryInterface or __uuidof.

Correct Way to Use CApplication with MFC

Here’s how to correctly instantiate and use the CApplication class derived from COleDispatchDriver:

Step-by-Step Solution
  1. Initialize COM and Obtain the CATIA Application Object:

You should first initialize COM and use GetActiveObject to obtain the running instance of the CATIA application.

  1. Attach the COM Object to CApplication:

Instead of using QueryInterface, use the AttachDispatch method of COleDispatchDriver to bind the IDispatch pointer to the CApplication object.

Updated Code Example

Here’s the correct way to use CApplication:

 

cpp

#include <afxdisp.h> // Required for MFC OLE/COM support
#include <iostream>
#include <windows.h>
#include "CApplication.h" // Include the generated CApplication class

int main() {
    // Initialize COM
    HRESULT hr = CoInitialize(NULL);
    if (FAILED(hr)) {
        std::cerr << "Failed to initialize COM." << std::endl;
        return -1;
    }

    CLSID clsid;
    hr = CLSIDFromProgID(L"CATIA.Application", &clsid);
    if (SUCCEEDED(hr)) {
        IDispatch* pDisp = nullptr;
        hr = GetActiveObject(clsid, NULL, (IUnknown**)&pDisp); // Obtain the running CATIA instance
        if (SUCCEEDED(hr) && pDisp != nullptr) {
            CApplication q_pCatiaApp; // Create an instance of CApplication
            q_pCatiaApp.AttachDispatch(pDisp); // Attach the IDispatch pointer to CApplication

            // Now you can interact with the CATIA application through q_pCatiaApp
            std::cout << "Connected to CATIA successfully." << std::endl;

            // Example: Get the number of open documents
            CDocuments documents = q_pCatiaApp.get_Documents();
            long docCount = documents.GetCount();
            std::cout << "Number of open documents in CATIA: " << docCount << std::endl;

            // Detach and release the dispatch interface when done
            q_pCatiaApp.ReleaseDispatch();
            pDisp->Release(); // Release IDispatch when done
        } else {
            std::cerr << "Failed to connect to CATIA." << std::endl;
        }
    } else {
        std::cerr << "Failed to get CLSID for CATIA.Application." << std::endl;
    }

    CoUninitialize(); // Uninitialize COM
    return 0;
}

cpp

Explanation of Key Changes

  1. Use AttachDispatch Method:

    • The AttachDispatch method from COleDispatchDriver is used to attach the IDispatch pointer obtained from GetActiveObject to your CApplication instance. This method binds the COM object to the MFC wrapper so that you can use the generated methods and properties.
  2. Do Not Use QueryInterface with __uuidof:

    • Since CApplication is derived from COleDispatchDriver and not a direct COM interface, __uuidof is not necessary. The COleDispatchDriver mechanism abstracts away the need for directly querying interfaces.
  3. Release COM Objects Properly:

    • Use ReleaseDispatch() to detach and release the IDispatch pointer before calling Release() on the original IDispatch pointer (pDisp).

Additional Tips

  • MFC Project Settings: Make sure your project is set up to use MFC (Microsoft Foundation Classes). If you are using an MFC application, this setup should already be configured.

  • Error Handling: Always check HRESULT values to handle any errors properly. Use SUCCEEDED(hr) and FAILED(hr) macros to determine success or failure.

Conclusion

By using AttachDispatch with the CApplication class derived from COleDispatchDriver, you correctly bind the CATIA COM object to the generated MFC wrapper class, avoiding the need for direct QueryInterface calls. This simplifies the use of COM objects and leverages the power of MFC to handle COM interactions more intuitively.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值