Questions about using Active X controls in C#

Answered Questions about using Active X controls in C#

  • mardi 19 avril 2005 05:54
     
     
    Hi,

    I am trying to use a activeX control in C#. I add the control to references.
    I need to call one of the function of the ActiveX Obj. I got confused here.

    It was defined as 
       **.Register(int hWndCtrl, object Flags); 

    VC++ provided sample as 

        HWND hwndTop = GetDlgItem( IDC_EDITTOP );
        hRes = m_pIDgnDictEditTop->Register( (long)hwndTop, CComVariant() );

    In VC#, 

    I used this,

       ...
       private
    System.Windows.Forms.TextBox edtRaw;

       ...

       Form1_load()
       {
          ......
          System.Object a;
          a = 0;
          dictEdit.Register(( (System.Int32)edtRaw.Handle, a);
          ......
       }

    No error, no warning while building the sln. But got error during excution at the Register code,
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in DragonIDX.exe

    Additional information: COM returned an unexpected error code: Details are E_UNEXPECTED 

    Questions:
    1) Do I have the right Window Handle in C#?
    2) What is the thing corresponding to CCOMVariant in C#?

    Thanks a lot.
    Pin
        

Toutes les réponses

  • mardi 19 avril 2005 19:22
    Modérateur
     
     
    Try tinkering with your cast, try casting it to System.Int16, Int64 or just simply int...

    What does the intellisense tells you about the parameter on dicEdit.Register()?




    cheers,


    Paul June A. Domag
  • mardi 19 avril 2005 20:28
    Modérateur
     
     Traitée
     Pin wrote:
    Hi,

    I am trying to use a activeX control in C#. I add the control to references.
    I need to call one of the function of the ActiveX Obj. I got confused here.

    It was defined as 
       **.Register(int hWndCtrl, object Flags); 

    VC++ provided sample as 

        HWND hwndTop = GetDlgItem( IDC_EDITTOP );
        hRes = m_pIDgnDictEditTop->Register( (long)hwndTop, CComVariant() );

    In VC#, 

    I used this,

       ...
       private
    System.Windows.Forms.TextBox edtRaw;

       ...

       Form1_load()
       {
          ......
          System.Object a;
          a = 0;
          dictEdit.Register(( (System.Int32)edtRaw.Handle, a);
          ......
       }

    No error, no warning while building the sln. But got error during excution at the Register code,
    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in DragonIDX.exe

    Additional information: COM returned an unexpected error code: Details are E_UNEXPECTED 

    Questions:
    1) Do I have the right Window Handle in C#?
    2) What is the thing corresponding to CCOMVariant in C#?

    Thanks a lot.
    Pin
        



    Pin,

       The second parameter is of type object, but in COM, this represents the VARIANT, which allows for anything to be passed.  However, it is completely possible that the code inside the method expects something different than the int that you are passing.

       I would try sending System.Reflection.Missing.Value (I believe that is it), and seeing what happens.

       Unfortunately, the E_UNEXPECTED error code is not very descriptive.

       Hope this helps.

              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard.caspershouse.com
  • mercredi 20 avril 2005 03:32
     
     
    Thanks,

    Nicholas' advice works for it. Thanks. But I am not sure the reason to put the "...Missing.Value"? How do I know this if I did not post my question here? Is it described in any section of general C#  cookbooks?

    Thanks a lot.
    Pin
  • jeudi 21 avril 2005 14:00
    Modérateur
     
     
     Pin wrote:
    Thanks,

    Nicholas' advice works for it. Thanks. But I am not sure the reason to put the "...Missing.Value"? How do I know this if I did not post my question here? Is it described in any section of general C#  cookbooks?

    Thanks a lot.
    Pin

    Pin,

       The reason this happens is because of the differences between COM and .NET.  COM does not allow for overloading on methods, however, it does allow for optional parameters, which .NET does not.

       So, when you expose a method in COM with optional parameters to .NET, they are always visible in .NET.  Because of this, if you don't want to pass those parameters, you have to pass System.Missing.Value.

              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard.caspershouse.com