Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. COM Interop

COM Interop

Scheduled Pinned Locked Moved C#
comhelpcsharpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Ungi
    wrote on last edited by
    #1

    I have a big Problem: I want to use a OCX in my C# Application. To achive this I used the Project/Add Reference... menu and browsed for the Component in the COM Tab. There are no problems on viewing the component in the Object Browser or to instance any class of the component. But if I try to call a method or set any property of any instanced class I get the following error: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Catastrophic failure Does any one of you know, what that could be?

    H U 2 Replies Last reply
    0
    • U Ungi

      I have a big Problem: I want to use a OCX in my C# Application. To achive this I used the Project/Add Reference... menu and browsed for the Component in the COM Tab. There are no problems on viewing the component in the Object Browser or to instance any class of the component. But if I try to call a method or set any property of any instanced class I get the following error: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Catastrophic failure Does any one of you know, what that could be?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Could be many things. If you're not running the application on the same machine, make sure the target machine has the OCX and any of its dependencies installed (making sure that its dependencies are installed is required on the development machine as well). The threading model of the COM control might be a factory, too, but since the extension is OCX I'm guessing you used VB to create it and could never stomach getting deep enough into it many years ago to know if you can control the thread model. To better assist you, could you perhaps post a short code snippet of how you're instantiating the control and calling the method? Also, the object browser only views Type information. If you want to test the initialization of the control without factoring in your code (not saying that you did something wrong, but you must eliminate that possibility first), use the ActiveX Control Test Container, which should be in your tools menu. Add an instance of the control and try running a few methods on it to make sure that it's working correctly.

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      U 1 Reply Last reply
      0
      • H Heath Stewart

        Could be many things. If you're not running the application on the same machine, make sure the target machine has the OCX and any of its dependencies installed (making sure that its dependencies are installed is required on the development machine as well). The threading model of the COM control might be a factory, too, but since the extension is OCX I'm guessing you used VB to create it and could never stomach getting deep enough into it many years ago to know if you can control the thread model. To better assist you, could you perhaps post a short code snippet of how you're instantiating the control and calling the method? Also, the object browser only views Type information. If you want to test the initialization of the control without factoring in your code (not saying that you did something wrong, but you must eliminate that possibility first), use the ActiveX Control Test Container, which should be in your tools menu. Add an instance of the control and try running a few methods on it to make sure that it's working correctly.

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        U Offline
        U Offline
        Ungi
        wrote on last edited by
        #3

        Thank you for your answer! The component is running on the same machine. The threading model could really be a factory. A colleague created the component with C++/MFC. What could there be a problem? I instance the class like: MasDspMetadata.MasDspClass mMasDsp = new MasDspMetadata.MasDspClass(); mMasDsp.Connect (strHostname,dPort,dTimeout); //raises the error With the ActiveX Control Test Container I have no problems on calling methods.

        H 1 Reply Last reply
        0
        • U Ungi

          Thank you for your answer! The component is running on the same machine. The threading model could really be a factory. A colleague created the component with C++/MFC. What could there be a problem? I instance the class like: MasDspMetadata.MasDspClass mMasDsp = new MasDspMetadata.MasDspClass(); mMasDsp.Connect (strHostname,dPort,dTimeout); //raises the error With the ActiveX Control Test Container I have no problems on calling methods.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It doesn't really matter in what language a COM component was written, so long as the runtime for the language exists on the target machine, so it wouldn't be a problem if it was written in MFC (in fact, most components are written in C++, of which MFC is just a wrapper). Take a look at tlbimp.exe in the .NET Framework SDK. It imports a typelib to an interop assembly (Runtime Callable Wrapper, or RCW). Perhaps by manually creating this assembly you can set some additional options. If this is an ActiveX control, you can also use aximp.exe to not only generate an RCW but also the source that would be compiled. I'm wondering if that string parameter (judging by your Hungarian notation) isn't being marshaled correctly. Remember that strings can be either ASCII or Unicode, and that Windows only natively supports ASCII (as opposed to Windows NT, which uses Unicode but can use ASCII). You can control how the string should be marshaled using the MarshalAsAttribute (see docs for details). If the MFC COM component only handles ASCII or Unicode, you should use UnmanagedType.LPStr or UnmanagedType.LPWStr respectively. If the guy built two COM components for Windows and Windows NT and they both support native strings, then you can use UnmanagedType.LPTStr. The only other thing I can think of is the threading model (a factory merely creates components). Is this single-, apartment-, or free-threaded (or both latter models)?

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          U 1 Reply Last reply
          0
          • H Heath Stewart

            It doesn't really matter in what language a COM component was written, so long as the runtime for the language exists on the target machine, so it wouldn't be a problem if it was written in MFC (in fact, most components are written in C++, of which MFC is just a wrapper). Take a look at tlbimp.exe in the .NET Framework SDK. It imports a typelib to an interop assembly (Runtime Callable Wrapper, or RCW). Perhaps by manually creating this assembly you can set some additional options. If this is an ActiveX control, you can also use aximp.exe to not only generate an RCW but also the source that would be compiled. I'm wondering if that string parameter (judging by your Hungarian notation) isn't being marshaled correctly. Remember that strings can be either ASCII or Unicode, and that Windows only natively supports ASCII (as opposed to Windows NT, which uses Unicode but can use ASCII). You can control how the string should be marshaled using the MarshalAsAttribute (see docs for details). If the MFC COM component only handles ASCII or Unicode, you should use UnmanagedType.LPStr or UnmanagedType.LPWStr respectively. If the guy built two COM components for Windows and Windows NT and they both support native strings, then you can use UnmanagedType.LPTStr. The only other thing I can think of is the threading model (a factory merely creates components). Is this single-, apartment-, or free-threaded (or both latter models)?

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            U Offline
            U Offline
            Ungi
            wrote on last edited by
            #5

            I tried to use tlbimp before and it took the same error. I don't think that it is an error on marshalling because the same error is raised if I call a method with no arguments or if I want to set a property of the class. Perhaps something on threading... Will try to get more information. Thank you very much for your help!

            1 Reply Last reply
            0
            • U Ungi

              I have a big Problem: I want to use a OCX in my C# Application. To achive this I used the Project/Add Reference... menu and browsed for the Component in the COM Tab. There are no problems on viewing the component in the Object Browser or to instance any class of the component. But if I try to call a method or set any property of any instanced class I get the following error: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Catastrophic failure Does any one of you know, what that could be?

              U Offline
              U Offline
              Ungi
              wrote on last edited by
              #6

              http://support.microsoft.com/default.aspx?scid=kb;EN-US;146120

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups