none
C#如何使用ATL/MFC in Native code所開發的COM元件 RRS feed

  • 問題

  • 大家好

    想請教大家,在vs.net 2005/WM5的開發環境下
    C#可以使用ATL/MFC所開發的COM元件嗎?
    如果可以,應該如何使用? 是否有相關文件可參考?
    還請大家不吝賜教,謝謝。
    2007年5月3日 上午 12:15

解答

  • 在 MSDN Library 中有一個範例:

    Walkthrough: Hello World: A COM Interop Example for Smart Devices

     

    就是教你怎麼把 ATL 開發的 COM 元件讓 Smart Device 的專案引用。

     

    另外,MSDN Library 有說明 Smart Device 專案對 COM 互通性的部份:

     

    COM Interoperability for Devices (From MSDN Library Visual Studio 2005 Documentation)

    The .NET Compact Framework supports Runtime Callable Wrappers (also called "Interop Assemblies") for COM objects. This feature includes the marshaling of complex types. COM Interop for devices is based on the desktop implementation. As such, components must be registered on the desktop.

    Supported Scenarios

    The following scenarios are supported for device projects in Visual Studio 2005:

    • You can add an existing COM component as a reference to a managed project. This action creates an interop assembly and automatically adds the assembly as a reference. You can then use the interop assembly just as you could any managed assembly, and the properties, methods, and events of the object are available for Intellisense and in the Object Browser. Legal file types to add are DLL, EXE, and TLB.

    • You can create a native project to generate a COM component, and then create a managed project in the same solution to consume the COM component. The process is the same as for the desktop:

      • Set the native project to generate TLB output.

      • Compile the native project to generate a DLL.

      • In the managed project, add a reference to the DLL. This action generates the interop assembly.

    Unsupported Scenarios

    The following scenarios are not supported in Visual Studio 2005:

    • Referencing an existing ActiveX COM component from within a managed project

    • COM objects with non-system child components

    • COM objects referenced as business objects from within the DataSource Wizard.

     

    2007年5月3日 上午 06:18

所有回覆

  • ATL 開發的元件一定可支援 COM Automation,所以只要加入參考就可以使用。

    MFC 的話就要看是否支援 COM Automation,否則只能當一般的 Win32 DLL,用 P/Invoke 方式存取。

    2007年5月3日 上午 01:57
  • 首先謝謝你的回覆。

    我想應該針對我的問題再進行深入一點的說明
    問題的焦點是針對WM5.0
    如果使用vs.net2005/智慧型裝置/ATL元件
    則產生的是可以在smart device上執行的.dll(for COM元件),而無法被直接在C#中參考

    但是使用C#是在PC上開發,要如何產生一個對應的COM component shell,使得該元件,在PC上使用C#開發AP時可以被參考,這是我想知道的。
    其次是我也想知道其他相關的訊息,因此在問題的提問上比較是廣泛的問題。

    thanks for any msg.
    2007年5月3日 上午 04:53
  • 在 MSDN Library 中有一個範例:

    Walkthrough: Hello World: A COM Interop Example for Smart Devices

     

    就是教你怎麼把 ATL 開發的 COM 元件讓 Smart Device 的專案引用。

     

    另外,MSDN Library 有說明 Smart Device 專案對 COM 互通性的部份:

     

    COM Interoperability for Devices (From MSDN Library Visual Studio 2005 Documentation)

    The .NET Compact Framework supports Runtime Callable Wrappers (also called "Interop Assemblies") for COM objects. This feature includes the marshaling of complex types. COM Interop for devices is based on the desktop implementation. As such, components must be registered on the desktop.

    Supported Scenarios

    The following scenarios are supported for device projects in Visual Studio 2005:

    • You can add an existing COM component as a reference to a managed project. This action creates an interop assembly and automatically adds the assembly as a reference. You can then use the interop assembly just as you could any managed assembly, and the properties, methods, and events of the object are available for Intellisense and in the Object Browser. Legal file types to add are DLL, EXE, and TLB.

    • You can create a native project to generate a COM component, and then create a managed project in the same solution to consume the COM component. The process is the same as for the desktop:

      • Set the native project to generate TLB output.

      • Compile the native project to generate a DLL.

      • In the managed project, add a reference to the DLL. This action generates the interop assembly.

    Unsupported Scenarios

    The following scenarios are not supported in Visual Studio 2005:

    • Referencing an existing ActiveX COM component from within a managed project

    • COM objects with non-system child components

    • COM objects referenced as business objects from within the DataSource Wizard.

     

    2007年5月3日 上午 06:18
  • 再次感謝您迅速回覆
    我將會閱讀相關文件
    謝謝

    2007年5月3日 上午 07:41
  • 關於您所提及的網頁文件
    我已經遵照其上的指示RUN了一次
    的確是我需要的solution

    在閱讀相關文件的過程中,發現了另一個疑問
    其中有文件提及可以建立multi-platform的project type
    但是其所指的platform都是屬於smart device(How to: Create a Multiplatform Device Project Using the Wizard )

    如果我想建立一個ATL專案,而其中的ATL simple object可以同時為smart device & desktop的應用程式專案所用,是否可以辦到?若可以,應該如何做呢?

    thanks for any msg.
    2007年5月3日 上午 08:52