Activator.CreateInstanceFrom(..) question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael H

    #1

    Activator.CreateInstanceFrom(..) question


    Hi all,


    I'm trying to use some functionality from the Windows Desktop
    Search Managed DLL "WDSQuery.d ll" within a library I'm writing. I
    can't be guaranteed this DLL will be present on the target machine so
    I'm trying to write code to take this into account. So far, I seem to
    be able to create an ObjectHandle. I then call the ObjectHandle's
    Unwrap() method to return an object that I can work with. I don't know
    if I'm doing this part right or not but it compiles and runs w/o
    throwing any exceptions. When I try to call a method from this object,
    I get a System.Runtime. InteropServices .COMException exception with
    error code 0x80040E14. I've found some mention of this code resulting
    from an SQL error but I'm tested the exact same input to this
    ExecuteQuery method in another project that assumes the WDSQuery.dll
    is available and all works fine.

    Am I going about this all the wrong way? I would like to create an
    Object then call one of its methods without knowing ahead of time if
    the DLL is present.

    Thanks... I pasted some sample code below.





    [InterfaceType(1 )]
    [Guid("A227843C-1D92-48BC-AED6-DCA566F1790E")]
    public interface ISearchDesktop
    {
    /*_Recordset */ object ExecuteQuery(st ring lpcwstrQuery, string
    lpcwstrColumn, string lpcwstrSort, string lpcwstrRestrict ion);
    /*_Recordset */ object ExecuteSQLQuery (string lpcwstrSQL);
    }


    public class Test
    {
    System.Runtime. Remoting.Object Handle hObject;
    ISearchDesktop myInterface;

    public Test()
    {
    try
    {
    hObject = Activator.Creat eComInstanceFro m("WDSQuery.dll ",
    "Microsoft.Wind ows.DesktopSear ch.Query.Search DesktopClass");

    myInterface = (ISearchDesktop )hObject.Unwrap ();

    string lpcwstrQuery = "microsoft" ; // search term
    string lpcwstrColumn = "DocTitle,DocFo rmat";
    string lpcwstrSort = "DocTitle";
    string lpcwstrRestrict ion = String.Empty;

    object o = myInterface.Exe cuteQuery(lpcws trQuery, lpcwstrColumn,
    lpcwstrSort, null);

    }
    catch (System.IO.File NotFoundExcepti on e){ }
    catch (System.TypeLoa dException e){}
    catch (System.Runtime .InteropService s.COMException e){}
    }
    }
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Activator.Creat eInstanceFrom(. .) question

    Michael,

    Why don't you just distribute WDSQuery.dll with your app, and make it a
    private assembly (meaning, it is in the same directory as your app)?


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

    "Michael H" <gfemail@gmail. com> wrote in message
    news:2em0v1hbkg va317q02jpmke0h bfsbamtei@4ax.c om...[color=blue]
    >
    > Hi all,
    >
    >
    > I'm trying to use some functionality from the Windows Desktop
    > Search Managed DLL "WDSQuery.d ll" within a library I'm writing. I
    > can't be guaranteed this DLL will be present on the target machine so
    > I'm trying to write code to take this into account. So far, I seem to
    > be able to create an ObjectHandle. I then call the ObjectHandle's
    > Unwrap() method to return an object that I can work with. I don't know
    > if I'm doing this part right or not but it compiles and runs w/o
    > throwing any exceptions. When I try to call a method from this object,
    > I get a System.Runtime. InteropServices .COMException exception with
    > error code 0x80040E14. I've found some mention of this code resulting
    > from an SQL error but I'm tested the exact same input to this
    > ExecuteQuery method in another project that assumes the WDSQuery.dll
    > is available and all works fine.
    >
    > Am I going about this all the wrong way? I would like to create an
    > Object then call one of its methods without knowing ahead of time if
    > the DLL is present.
    >
    > Thanks... I pasted some sample code below.
    >
    >
    >
    >
    >
    > [InterfaceType(1 )]
    > [Guid("A227843C-1D92-48BC-AED6-DCA566F1790E")]
    > public interface ISearchDesktop
    > {
    > /*_Recordset */ object ExecuteQuery(st ring lpcwstrQuery, string
    > lpcwstrColumn, string lpcwstrSort, string lpcwstrRestrict ion);
    > /*_Recordset */ object ExecuteSQLQuery (string lpcwstrSQL);
    > }
    >
    >
    > public class Test
    > {
    > System.Runtime. Remoting.Object Handle hObject;
    > ISearchDesktop myInterface;
    >
    > public Test()
    > {
    > try
    > {
    > hObject = Activator.Creat eComInstanceFro m("WDSQuery.dll ",
    > "Microsoft.Wind ows.DesktopSear ch.Query.Search DesktopClass");
    >
    > myInterface = (ISearchDesktop )hObject.Unwrap ();
    >
    > string lpcwstrQuery = "microsoft" ; // search term
    > string lpcwstrColumn = "DocTitle,DocFo rmat";
    > string lpcwstrSort = "DocTitle";
    > string lpcwstrRestrict ion = String.Empty;
    >
    > object o = myInterface.Exe cuteQuery(lpcws trQuery, lpcwstrColumn,
    > lpcwstrSort, null);
    >
    > }
    > catch (System.IO.File NotFoundExcepti on e){ }
    > catch (System.TypeLoa dException e){}
    > catch (System.Runtime .InteropService s.COMException e){}
    > }
    > }[/color]


    Comment

    • Michael H

      #3
      Re: Activator.Creat eInstanceFrom(. .) question

      On Mon, 13 Feb 2006 09:29:45 -0500, "Nicholas Paldino [.NET/C# MVP]"
      <mvp@spam.guard .caspershouse.c om> wrote:

      -> Michael,
      ->
      -> Why don't you just distribute WDSQuery.dll with your app, and
      make it a
      -> private assembly (meaning, it is in the same directory as your
      app)?


      I may have to distribute it but it was more of a general question.
      I just approached it like this.



      try
      {
      WdsAssembly = System.Reflecti on.Assembly.Loa dFrom("WDSQuery .dll");

      Type t = WdsAssembly.Get Type(
      "Microsoft.Wind ows.DesktopSear ch.Query.Search DesktopClass", false,
      true);

      object SearchDesktopCl ass = System.Activato r.CreateInstanc e(t);

      System.Reflecti on.MethodInfo ExecuteQuery =
      t.GetMethod("Ex ecuteQuery");

      Object[] Parameters = new Object[4];
      ...
      ...
      ...
      Object o2 = ExecuteQuery.In voke(SearchDesk topClass, Parameters);
      ...
      ...
      }
      catch (System.Excepti on)
      {
      ...
      }

      Comment

      Working...