Activator.CreateInstanceFrom(..) question

  • Thread starter Thread starter Michael H
  • Start date Start date
M

Michael H

Hi all,


I'm trying to use some functionality from the Windows Desktop
Search Managed DLL "WDSQuery.dll" 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(string lpcwstrQuery, string
lpcwstrColumn, string lpcwstrSort, string lpcwstrRestriction);
/*_Recordset */ object ExecuteSQLQuery(string lpcwstrSQL);
}


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

public Test()
{
try
{
hObject = Activator.CreateComInstanceFrom("WDSQuery.dll",
"Microsoft.Windows.DesktopSearch.Query.SearchDesktopClass");

myInterface = (ISearchDesktop)hObject.Unwrap();

string lpcwstrQuery = "microsoft"; // search term
string lpcwstrColumn = "DocTitle,DocFormat";
string lpcwstrSort = "DocTitle";
string lpcwstrRestriction = String.Empty;

object o = myInterface.ExecuteQuery(lpcwstrQuery, lpcwstrColumn,
lpcwstrSort, null);

}
catch (System.IO.FileNotFoundException e){ }
catch (System.TypeLoadException e){}
catch (System.Runtime.InteropServices.COMException e){}
}
}
 
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)?
 
On Mon, 13 Feb 2006 09:29:45 -0500, "Nicholas Paldino [.NET/C# MVP]"

-> 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.Reflection.Assembly.LoadFrom("WDSQuery.dll");

Type t = WdsAssembly.GetType(
"Microsoft.Windows.DesktopSearch.Query.SearchDesktopClass", false,
true);

object SearchDesktopClass = System.Activator.CreateInstance(t);

System.Reflection.MethodInfo ExecuteQuery =
t.GetMethod("ExecuteQuery");

Object[] Parameters = new Object[4];
...
...
...
Object o2 = ExecuteQuery.Invoke(SearchDesktopClass, Parameters);
...
...
}
catch (System.Exception)
{
...
}
 
Back
Top