471,350 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Activator.CreateInstanceFrom(..) question


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.SearchDeskt opClass");

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){}
}
}
Feb 13 '06 #1
2 3825
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]
- mv*@spam.guard.caspershouse.com

"Michael H" <gf*****@gmail.com> wrote in message
news:2e********************************@4ax.com...

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.SearchDeskt opClass");

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){}
}
}

Feb 13 '06 #2
On Mon, 13 Feb 2006 09:29:45 -0500, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.com> 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.Reflection.Assembly.LoadFrom("WDSQuery.dll" );

Type t = WdsAssembly.GetType(
"Microsoft.Windows.DesktopSearch.Query.SearchDeskt opClass", 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)
{
...
}
Feb 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.