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. COM
  4. COM Component Late binding trouble

COM Component Late binding trouble

Scheduled Pinned Locked Moved COM
csharpasp-netwpfwcfcom
9 Posts 3 Posters 1 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.
  • N Offline
    N Offline
    nilarya
    wrote on last edited by
    #1

    Hi All, I have posted this message earlier on the ASP.Net forum, I did not notice there is a separate forum for Co Componenets. However I am moving the post to here, please have a look, any help would be much appreciated. I am using a third Party Com Component in my ASP.NET application. I dont have much experience with COM Components and I think I have messed up here. Please help. The Com component has a method which looks like following in the ILDASM: .method public hidebysig newslot specialname virtual instance class [stdole]stdole.IPicture marshal( interface ) get_Preview([in] float64 rWorldMinX, [in] float64 rWorldMinY, [in] float64 rWorldMaxX, [in] float64 rWorldMaxY, [in] int32 dwPicWidth, [in] int32 dwPicHeight, [in] int32 PicType) runtime managed internalcall In my code behind, I am using

    object ComObjLateBound;
    Type ComObjType;
    ComObjType= Type.GetTypeFromCLSID(new Guid("{89251546-3F1C-430D-BA77-F86572FA4EF6}"));
    ComObjLateBound= Activator.CreateInstance(ComObjType);

    //getting the values from the properties. The properties are in the Com Component
    double viewMaxX = (double)ComObjType.InvokeMember("ViewMaxX", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
    double viewMaxX = (double)ComObjType.InvokeMember("ViewMaxY", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
    double viewMaxX = (double)ComObjType.InvokeMember("ViewMinX", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
    double viewMaxX = (double)ComObjType.InvokeMember("ViewMinY", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });

    //geting the image height and width from a local file
    int imagewidth, imageheight, pictype;
    imagewidth = 1280; imageheight = 800; pictype = 1;

    object[] previewDetails = new object[7];
    previewDetails[0] = viewMinX;

    previewDetails[1] = viewMinY;

    previewDetails[2] = viewMaxX;

    previewDetails[3] = viewMaxY;

    previewDetails[4] = imagewidth;

    previewDetails[5] = imageheight;

    previewDetails[6] = pictype;

    IPicture picture = (IPicture)ComObjType.InvokeMember("get_Preview", BindingFlags.Default | BindingFlags.InvokeMethod, null, ComObjLateBound, previewDetails);

    this fails with a COM Exception like {"Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"} with an ErrorCode of -2147352570 if that means something. Now this is strange

    T V 2 Replies Last reply
    0
    • N nilarya

      Hi All, I have posted this message earlier on the ASP.Net forum, I did not notice there is a separate forum for Co Componenets. However I am moving the post to here, please have a look, any help would be much appreciated. I am using a third Party Com Component in my ASP.NET application. I dont have much experience with COM Components and I think I have messed up here. Please help. The Com component has a method which looks like following in the ILDASM: .method public hidebysig newslot specialname virtual instance class [stdole]stdole.IPicture marshal( interface ) get_Preview([in] float64 rWorldMinX, [in] float64 rWorldMinY, [in] float64 rWorldMaxX, [in] float64 rWorldMaxY, [in] int32 dwPicWidth, [in] int32 dwPicHeight, [in] int32 PicType) runtime managed internalcall In my code behind, I am using

      object ComObjLateBound;
      Type ComObjType;
      ComObjType= Type.GetTypeFromCLSID(new Guid("{89251546-3F1C-430D-BA77-F86572FA4EF6}"));
      ComObjLateBound= Activator.CreateInstance(ComObjType);

      //getting the values from the properties. The properties are in the Com Component
      double viewMaxX = (double)ComObjType.InvokeMember("ViewMaxX", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
      double viewMaxX = (double)ComObjType.InvokeMember("ViewMaxY", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
      double viewMaxX = (double)ComObjType.InvokeMember("ViewMinX", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
      double viewMaxX = (double)ComObjType.InvokeMember("ViewMinY", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });

      //geting the image height and width from a local file
      int imagewidth, imageheight, pictype;
      imagewidth = 1280; imageheight = 800; pictype = 1;

      object[] previewDetails = new object[7];
      previewDetails[0] = viewMinX;

      previewDetails[1] = viewMinY;

      previewDetails[2] = viewMaxX;

      previewDetails[3] = viewMaxY;

      previewDetails[4] = imagewidth;

      previewDetails[5] = imageheight;

      previewDetails[6] = pictype;

      IPicture picture = (IPicture)ComObjType.InvokeMember("get_Preview", BindingFlags.Default | BindingFlags.InvokeMethod, null, ComObjLateBound, previewDetails);

      this fails with a COM Exception like {"Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"} with an ErrorCode of -2147352570 if that means something. Now this is strange

      T Offline
      T Offline
      tom englert
      wrote on last edited by
      #2

      Hi, you wrote that you look at the COM component with ILDASM - but you probably examined the generated CLR-interop rather than the real COM component. Since you can't do early binding with that interop, it must have been already messed up, so I would not rely on the information provided by it. To see what's really provided by the COM object you could have a look at the type library, using e.g. OLE-COM Object Viewer (OleView.exe) from Windows SDK.

      N 1 Reply Last reply
      0
      • N nilarya

        Hi All, I have posted this message earlier on the ASP.Net forum, I did not notice there is a separate forum for Co Componenets. However I am moving the post to here, please have a look, any help would be much appreciated. I am using a third Party Com Component in my ASP.NET application. I dont have much experience with COM Components and I think I have messed up here. Please help. The Com component has a method which looks like following in the ILDASM: .method public hidebysig newslot specialname virtual instance class [stdole]stdole.IPicture marshal( interface ) get_Preview([in] float64 rWorldMinX, [in] float64 rWorldMinY, [in] float64 rWorldMaxX, [in] float64 rWorldMaxY, [in] int32 dwPicWidth, [in] int32 dwPicHeight, [in] int32 PicType) runtime managed internalcall In my code behind, I am using

        object ComObjLateBound;
        Type ComObjType;
        ComObjType= Type.GetTypeFromCLSID(new Guid("{89251546-3F1C-430D-BA77-F86572FA4EF6}"));
        ComObjLateBound= Activator.CreateInstance(ComObjType);

        //getting the values from the properties. The properties are in the Com Component
        double viewMaxX = (double)ComObjType.InvokeMember("ViewMaxX", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
        double viewMaxX = (double)ComObjType.InvokeMember("ViewMaxY", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
        double viewMaxX = (double)ComObjType.InvokeMember("ViewMinX", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });
        double viewMaxX = (double)ComObjType.InvokeMember("ViewMinY", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, new object[] { });

        //geting the image height and width from a local file
        int imagewidth, imageheight, pictype;
        imagewidth = 1280; imageheight = 800; pictype = 1;

        object[] previewDetails = new object[7];
        previewDetails[0] = viewMinX;

        previewDetails[1] = viewMinY;

        previewDetails[2] = viewMaxX;

        previewDetails[3] = viewMaxY;

        previewDetails[4] = imagewidth;

        previewDetails[5] = imageheight;

        previewDetails[6] = pictype;

        IPicture picture = (IPicture)ComObjType.InvokeMember("get_Preview", BindingFlags.Default | BindingFlags.InvokeMethod, null, ComObjLateBound, previewDetails);

        this fails with a COM Exception like {"Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"} with an ErrorCode of -2147352570 if that means something. Now this is strange

        V Offline
        V Offline
        Vi2
        wrote on last edited by
        #3

        nilarya wrote:

        I have also tried to use this property to get the IPicture value like:

        IPicture picture = (IPicture)ComObjType.InvokeMember("Preview", BindingFlags.Default | BindingFlags.GetProperty, null, ComObjLateBound, previewDetails);

        But it also falis with a message that Exception has been thrown by the target of an invocation.

        It's right way, but you should use the reversed order of arguments. previewDetails[6] = viewMinX; and so on.

        With best wishes, Vita

        1 Reply Last reply
        0
        • T tom englert

          Hi, you wrote that you look at the COM component with ILDASM - but you probably examined the generated CLR-interop rather than the real COM component. Since you can't do early binding with that interop, it must have been already messed up, so I would not rely on the information provided by it. To see what's really provided by the COM object you could have a look at the type library, using e.g. OLE-COM Object Viewer (OleView.exe) from Windows SDK.

          N Offline
          N Offline
          nilarya
          wrote on last edited by
          #4

          tom-englert wrote:

          Since you can't do early binding with that interop, it must have been already messed up, so I would not rely on the information provided by it.

          Thanks tom-englert for your response. Sorry for replying late. I was working on a different module during this time. you are right, I have examined the CLR-interop version of the component in ILDASM. But I wont say the component has been messed up as I can achieve desired results from the component with the exact same codes in case of a winforms application. In winforms both early binding and latebinding is working for me. I am also able to talk to the Component through Javascript block. But I need this component to run on my codebehind. I've tried installing OleView previously, but I am using Win7 in my development environment, It seems OleView is not supported in Win 7.

          T 1 Reply Last reply
          0
          • N nilarya

            tom-englert wrote:

            Since you can't do early binding with that interop, it must have been already messed up, so I would not rely on the information provided by it.

            Thanks tom-englert for your response. Sorry for replying late. I was working on a different module during this time. you are right, I have examined the CLR-interop version of the component in ILDASM. But I wont say the component has been messed up as I can achieve desired results from the component with the exact same codes in case of a winforms application. In winforms both early binding and latebinding is working for me. I am also able to talk to the Component through Javascript block. But I need this component to run on my codebehind. I've tried installing OleView previously, but I am using Win7 in my development environment, It seems OleView is not supported in Win 7.

            T Offline
            T Offline
            tom englert
            wrote on last edited by
            #5

            If that works in WinForms, then you maybe have a threading problem. The main thread of WinForms runs in STA, and if your object is designed for STA too ("ThreadingModel=Apartment" in the registry) everything is fine. But if you call that code from an MTA thread, the interface for the object has to be marshaled, which I assume is not supported by your interface.

            N 2 Replies Last reply
            0
            • T tom englert

              If that works in WinForms, then you maybe have a threading problem. The main thread of WinForms runs in STA, and if your object is designed for STA too ("ThreadingModel=Apartment" in the registry) everything is fine. But if you call that code from an MTA thread, the interface for the object has to be marshaled, which I assume is not supported by your interface.

              N Offline
              N Offline
              nilarya
              wrote on last edited by
              #6

              Thanks Tom, I checked the registry, and you are right again. The value of ThreadingModel is defined as Apartment in there. So does that mean there is no way I can access the component in an asp.net codebehind? is there a workarround for this issue?

              N 1 Reply Last reply
              0
              • N nilarya

                Thanks Tom, I checked the registry, and you are right again. The value of ThreadingModel is defined as Apartment in there. So does that mean there is no way I can access the component in an asp.net codebehind? is there a workarround for this issue?

                N Offline
                N Offline
                nilarya
                wrote on last edited by
                #7

                Hi, I just found the url http://www.guidanceshare.com/wiki/Interop_(.NET_1.1)_Performance_Guidelines_-_Threading[^] where it talks about using ASPCOMPAT attribute on the page while calling STA Objects from asp.net. I will give it a try.

                1 Reply Last reply
                0
                • T tom englert

                  If that works in WinForms, then you maybe have a threading problem. The main thread of WinForms runs in STA, and if your object is designed for STA too ("ThreadingModel=Apartment" in the registry) everything is fine. But if you call that code from an MTA thread, the interface for the object has to be marshaled, which I assume is not supported by your interface.

                  N Offline
                  N Offline
                  nilarya
                  wrote on last edited by
                  #8

                  Hi again, I have finally got this COM object to work, but some problem still remains. I changed the code as following:

                  ...
                  using System.Threading;

                  namespace...
                  protected void Button1_Click(object sender, EventArgs e)
                  {
                  Thread thread = new Thread(new ThreadStart(STAOperation));
                  thread.SetApartmentState(ApartmentState.STA);
                  thread.Start();
                  thread.Join();
                  }
                  public void STAOperation()
                  {
                  CSViewerRL.ViewerClass csv = new ViewerClass();
                  csv.Open(filePath, 0, 0);//strange error
                  //other codes..
                  }

                  this code works, but once in a while, I am getting a strange error while debugging, which states AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. generally the app runs fine the first time I try to debug, but this error occures mostly on the second or third time I press f5 from my VS while the ASP.Net server is still running. and generally it goes away if I stop the server and run again. I think this problem is because of this new STA thread. Maybe I am not handling the new thread correctly. Or there is some other thing I should look into? Can someone point out whatshould I do under this circumstance?

                  T 1 Reply Last reply
                  0
                  • N nilarya

                    Hi again, I have finally got this COM object to work, but some problem still remains. I changed the code as following:

                    ...
                    using System.Threading;

                    namespace...
                    protected void Button1_Click(object sender, EventArgs e)
                    {
                    Thread thread = new Thread(new ThreadStart(STAOperation));
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                    }
                    public void STAOperation()
                    {
                    CSViewerRL.ViewerClass csv = new ViewerClass();
                    csv.Open(filePath, 0, 0);//strange error
                    //other codes..
                    }

                    this code works, but once in a while, I am getting a strange error while debugging, which states AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. generally the app runs fine the first time I try to debug, but this error occures mostly on the second or third time I press f5 from my VS while the ASP.Net server is still running. and generally it goes away if I stop the server and run again. I think this problem is because of this new STA thread. Maybe I am not handling the new thread correctly. Or there is some other thing I should look into? Can someone point out whatshould I do under this circumstance?

                    T Offline
                    T Offline
                    tom englert
                    wrote on last edited by
                    #9

                    Maybe you should limit the objects lifetime to the lifetime of the thread. Currently the object is destroyed at any time by the GC in the GC thread. Try calling Marshal.ReleaseComObject before the thread ends, to guarantee a deterministic lifetime of the object.

                    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