COM Question

S

Steven Edison

Hi,

We currently have a large library of COM objects written
in C++ that talk to different devices. Then we have several
applications that can host these objects. The simplest of
which just wraps the objects and displays their custom
GUIs. If you register a new dll, then it shows up in the
list as an available device in the various host applications.

I'm trying to duplicate this behavior for a mobile version of
our software. I'm afraid you may not be able to do what
I want in Compact Framework. The new app is C#.
What I'd like to do is get a list from the registry of installed
objects, and load them dynamically. To give a layman's
example, I'd have several implementations of IVCR in
various DLLs, and I could list the name brands listed in
the app for the ones installed, when they click on, say
Sony - X1234, then it would load the proper COM
object, which would display it's GUI. This way I can
just install a new DLL when we add a new device
to the software. Then I can just call IVCR.Record()
on each of them, and they'd each work. The apps would
have no prior knowledge of Sony, but would IVCR.

We've done this many times in the world of PC C++, but
not sure if we can do it for our PocketPC apps.

I have implemented a UserControl Library dll and a dll
that defines the interface, and had the UserControl implement
the interface, but so far I'm just placing the control on
the gui in form designer of Visual Studio. Ideally I'd like
to do something like:

//psuedo code
OnDeviceSelectionChange(){
IVCR obj;
//App doesn't know about "SonyX1234"
//instead gets list of installed "devices" from registry
//so user can choose at runtime
obj.CoCreateInstance("SonyX1234");
obj.Show(x,y,cx,cy);
obj.Record();
}

(the vcr is just an example, not a true representation
of the industry specific devices we're dealing with)

Is this possible?

Thanks in advance,

Steven
 
G

Guest

Do you plan to port all of these COM objects to CE as COM objects or managed
objects?
 
S

Steven Edison

I planned to rewrite them in C# as the GUIs will have to
be completely different from the ones for the PC
(they're written in unmanaged C++).

I've kinda done one, but not sure I did it the way I should
have:

1-Created VCR Control Library DLL to define interface
2-Created UserControl Library DLL for gui
3-Made UserControl implement COM interface:
public partial class Sony_X1234_Mobile : UserControl, VCR
4-Dropped Sony_X1234_Mobile onto host app to test it. (also had to
reference the control AND interface dll statically)

It's working, but not dynamicly loaded for now, and I'm not accessing
functions from VCR, just the member variable.

Steven
 
G

Guest

Then yes, this is very doable. Create a base class library that holds the
interface (or base class - it doesn't have to specifically be an interface).
Each new object will reference this same DLL to get the interface. You can
create a class library for each new device, or have libraries with multiple
objects.

Your "consumer" app also references the original base that holds the
interface. Use reflection to load each of the object assemblies and look to
see if it contains classes that derive from the interface. If it does,
create it with Activator.CreateInstance as an interface implementation:

IMyInterface instance = Activator.CreateInstance(...);

You then use the intstance by calling methods on the interface, so how it's
implemented is not part of what the consumer knows.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
 
S

Steven Edison

Thanks, I think Activator.CreateInstance() was the key I was missing.
Up to this point I had only seen foo = new Foo();
Your reply was very helpful!


Steven
 
S

Steven Edison

Chris,

I might have spoken too soon.
So if I have an obj like this:

public class AFooObj : IFoo

Then in my consumer app I have:

class ConsumeApp{
private IFoo MyFoo;

void OnClick{
MyFoo = (IFoo)Activator.CreateInstance();
}
}

But what I've ran into is you can't tell it which file to pull it from.
There are only two overloads that can be used with .NET Compact
Framework, CreateInstance(Type) and CreateInstance(). How
can I tell it to use AFooObj, especially without any prior references
to AFooObj?

Thanks again,

Steven
 
G

Guest

Use Assembly.LoadFrom(path) to load the assembly, iterate through the types
and the CreateInstance on the assembly to instatiate a class.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com


Steven Edison said:
Chris,

I might have spoken too soon.
So if I have an obj like this:

public class AFooObj : IFoo

Then in my consumer app I have:

class ConsumeApp{
private IFoo MyFoo;

void OnClick{
MyFoo = (IFoo)Activator.CreateInstance();
}
}

But what I've ran into is you can't tell it which file to pull it from.
There are only two overloads that can be used with .NET Compact
Framework, CreateInstance(Type) and CreateInstance(). How
can I tell it to use AFooObj, especially without any prior references
to AFooObj?

Thanks again,

Steven

Then yes, this is very doable. Create a base class library that holds
the interface (or base class - it doesn't have to specifically be an
interface). Each new object will reference this same DLL to get the
interface. You can create a class library for each new device, or have
libraries with multiple objects.

Your "consumer" app also references the original base that holds the
interface. Use reflection to load each of the object assemblies and look
to see if it contains classes that derive from the interface. If it
does, create it with Activator.CreateInstance as an interface
implementation:

IMyInterface instance = Activator.CreateInstance(...);

You then use the intstance by calling methods on the interface, so how
it's implemented is not part of what the consumer knows.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com



Steven Edison said:
I planned to rewrite them in C# as the GUIs will have to
be completely different from the ones for the PC
(they're written in unmanaged C++).

I've kinda done one, but not sure I did it the way I should
have:

1-Created VCR Control Library DLL to define interface
2-Created UserControl Library DLL for gui
3-Made UserControl implement COM interface:
public partial class Sony_X1234_Mobile : UserControl, VCR
4-Dropped Sony_X1234_Mobile onto host app to test it. (also had to
reference the control AND interface dll statically)

It's working, but not dynamicly loaded for now, and I'm not accessing
functions from VCR, just the member variable.

Steven


"<ctacke/>" <ctacke[at]opennetcf[dot]com> wrote in message
Do you plan to port all of these COM objects to CE as COM objects or
managed objects?


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com



Hi,

We currently have a large library of COM objects written
in C++ that talk to different devices. Then we have several
applications that can host these objects. The simplest of
which just wraps the objects and displays their custom
GUIs. If you register a new dll, then it shows up in the
list as an available device in the various host applications.

I'm trying to duplicate this behavior for a mobile version of
our software. I'm afraid you may not be able to do what
I want in Compact Framework. The new app is C#.
What I'd like to do is get a list from the registry of installed
objects, and load them dynamically. To give a layman's
example, I'd have several implementations of IVCR in
various DLLs, and I could list the name brands listed in
the app for the ones installed, when they click on, say
Sony - X1234, then it would load the proper COM
object, which would display it's GUI. This way I can
just install a new DLL when we add a new device
to the software. Then I can just call IVCR.Record()
on each of them, and they'd each work. The apps would
have no prior knowledge of Sony, but would IVCR.

We've done this many times in the world of PC C++, but
not sure if we can do it for our PocketPC apps.

I have implemented a UserControl Library dll and a dll
that defines the interface, and had the UserControl implement
the interface, but so far I'm just placing the control on
the gui in form designer of Visual Studio. Ideally I'd like
to do something like:

//psuedo code
OnDeviceSelectionChange(){
IVCR obj;
//App doesn't know about "SonyX1234"
//instead gets list of installed "devices" from registry
//so user can choose at runtime
obj.CoCreateInstance("SonyX1234");
obj.Show(x,y,cx,cy);
obj.Record();
}

(the vcr is just an example, not a true representation
of the industry specific devices we're dealing with)

Is this possible?

Thanks in advance,

Steven
 
S

Steven Edison

Once again I must thank you. 100% success.
I learned quite a bit too.

Thanks Chris!

Steven

Use Assembly.LoadFrom(path) to load the assembly, iterate through the
types and the CreateInstance on the assembly to instatiate a class.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com


Steven Edison said:
Chris,

I might have spoken too soon.
So if I have an obj like this:

public class AFooObj : IFoo

Then in my consumer app I have:

class ConsumeApp{
private IFoo MyFoo;

void OnClick{
MyFoo = (IFoo)Activator.CreateInstance();
}
}

But what I've ran into is you can't tell it which file to pull it from.
There are only two overloads that can be used with .NET Compact
Framework, CreateInstance(Type) and CreateInstance(). How
can I tell it to use AFooObj, especially without any prior references
to AFooObj?

Thanks again,

Steven

Then yes, this is very doable. Create a base class library that holds
the interface (or base class - it doesn't have to specifically be an
interface). Each new object will reference this same DLL to get the
interface. You can create a class library for each new device, or have
libraries with multiple objects.

Your "consumer" app also references the original base that holds the
interface. Use reflection to load each of the object assemblies and
look to see if it contains classes that derive from the interface. If
it does, create it with Activator.CreateInstance as an interface
implementation:

IMyInterface instance = Activator.CreateInstance(...);

You then use the intstance by calling methods on the interface, so how
it's implemented is not part of what the consumer knows.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com



I planned to rewrite them in C# as the GUIs will have to
be completely different from the ones for the PC
(they're written in unmanaged C++).

I've kinda done one, but not sure I did it the way I should
have:

1-Created VCR Control Library DLL to define interface
2-Created UserControl Library DLL for gui
3-Made UserControl implement COM interface:
public partial class Sony_X1234_Mobile : UserControl, VCR
4-Dropped Sony_X1234_Mobile onto host app to test it. (also had to
reference the control AND interface dll statically)

It's working, but not dynamicly loaded for now, and I'm not accessing
functions from VCR, just the member variable.

Steven


"<ctacke/>" <ctacke[at]opennetcf[dot]com> wrote in message
Do you plan to port all of these COM objects to CE as COM objects or
managed objects?


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com



Hi,

We currently have a large library of COM objects written
in C++ that talk to different devices. Then we have several
applications that can host these objects. The simplest of
which just wraps the objects and displays their custom
GUIs. If you register a new dll, then it shows up in the
list as an available device in the various host applications.

I'm trying to duplicate this behavior for a mobile version of
our software. I'm afraid you may not be able to do what
I want in Compact Framework. The new app is C#.
What I'd like to do is get a list from the registry of installed
objects, and load them dynamically. To give a layman's
example, I'd have several implementations of IVCR in
various DLLs, and I could list the name brands listed in
the app for the ones installed, when they click on, say
Sony - X1234, then it would load the proper COM
object, which would display it's GUI. This way I can
just install a new DLL when we add a new device
to the software. Then I can just call IVCR.Record()
on each of them, and they'd each work. The apps would
have no prior knowledge of Sony, but would IVCR.

We've done this many times in the world of PC C++, but
not sure if we can do it for our PocketPC apps.

I have implemented a UserControl Library dll and a dll
that defines the interface, and had the UserControl implement
the interface, but so far I'm just placing the control on
the gui in form designer of Visual Studio. Ideally I'd like
to do something like:

//psuedo code
OnDeviceSelectionChange(){
IVCR obj;
//App doesn't know about "SonyX1234"
//instead gets list of installed "devices" from registry
//so user can choose at runtime
obj.CoCreateInstance("SonyX1234");
obj.Show(x,y,cx,cy);
obj.Record();
}

(the vcr is just an example, not a true representation
of the industry specific devices we're dealing with)

Is this possible?

Thanks in advance,

Steven
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top