How to open a WPF exe from within a VB6 dll? - wpf

This is a question about design approach. I have limited COM experience and a little WCF experience. My constraints are given by the application environment, but I have some design flexibility.
From within a VB6 dll, I need to start, and communicate with, a WPF application. The WPF application is currently an exe, but I could make it a library if that helped. I would like to provide two way communication between the VB6 dll and the WPF application. I have some flexability to adjust the design of the VB6 dll.
I'm developing C# using in VS2010 and .NET 4.
How many components should I use? Can I start the WPF application In-Proc with the VB6 dll? Should there be a third component between them? Can COM+ play a helpful role? Do I have to make the entire WPF application COM-visible? Is there a down-side to doing this?
I'm looking for a design approach that I can prototype. I'm willing to research the details.

I would
Create a Web Service from the WPF application, using WCF. I would abstract out those aspects of the WPF application which should be accessed remotely. This would explicitly not include any of the user interface code.
I would create a simple class library project, and use "Add Service Reference" to add a reference to the WCF service.
I would make the methods of the class library COM-visible
I would call those methods from VB6
This has the benefit of removing any considerations of user interface from the equation.

Out of the options available I like the COM option more than the 'start another process' option for the simplicity that the inter 'application' communication will be via method calls rather than WCF or anything similar.
I am assuming that your VB DLL lives in a window'd process and not a service or web application. You would only need to mark any exposed types as COM visible, that is the classes, their argument and return types.
You may have to wrap your WPF UI inside the windows forms ElementHost [1] but I'm not sure, try it and see.
I'm not sure if you saw this [2] in your search, it sounds do-able but unsupported, ok as long as you don't have too much going on.
[1] http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx
[2] http://social.msdn.microsoft.com/forums/en-US/wpf/thread/7555ba6a-1359-4dfe-aa23-c31a8f121142/

I work on an application primarily written in VB6 but most of the recent code is written in .net with UI components built in WPF and some WinForms. Datasources for this application are WCF, MSSQL server, and a propritary unix based server. All the WCF calls are made from data access components referenced by the .net UI components.
You can host WPF in VB6 windows or other container controls. Start by getting the Interop Forms Toolkit and build shell user controls to host your WPF controls.
To be able to host WPF in these controls you need to build a WinForms usercontrol which contains a ElementHost, which you can set the content to your WPF usercontrol.
WPF Usercontrol inside an Element Host inside a
WinForms usercontrol inside a VB6 usercontrol or
window
The interop toolkit will want to build VB.Net code but you can do it in C#, although I have not tried this. The usercontrols created by the interop toolkit will be exposed as COM components which you can reference VB6 by adding them as components via Project > Components and then you will find them in the toolbox.
In terms of data sources (WCF, databases) etc, you should build all your data access in .net components referenced directly from the UI components, don't try to call back in to VB6 libraries, you will probably just create a mess.
In my application I also have a configuration section which I call from the VB6 application startup which sets up an IoC container for all the .net components.
From a best practice approach I actually would recommend rewriting your VB6 code to .net and putting VB6 out of the picture. If this is not an option then you have a number of options, my explanation is just one of them.

Related

Is it possible to use Windows Forms controls directly on a VB6 form?

Is it possible to use a .NET Windows Forms control directly in a VB6 form?
The goal here would be to use the more up to date .NET controls in a legacy VB6 application. We already have a mixed .NET / VB6 environment.
I am aware of the Microsoft InteropForms Toolkit but it is described as being used for exporting custom usercontrols to VB6 from .NET. This doesn't seem to provide for preexisting controls (but I could be wrong).
I have tried to simply reference System.Windows.Forms.tlb in a VB6 project as a component library - but it does not allow this to be selected, which seems to imply it has no ActiveX controls inside it.
I'm aware you can easily do the reverse - eg use a VB6 usercontrol on a .NET winforms form. That's not what I need.
You can reference the System.Windows.Forms assembly in a normal way from VB6 - and wind up with a line such as the following in your VBP:
Reference=*\G{215D64D2-031C-33C7-96E3-61794CD1EE61}#2.4#0#C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.tlb#System_Windows_Forms
but this doesn't provide access to place controls on a form.
I did that before.
I created a control (with the forms control you want use) in .net (exported to tlb using InteropFormsToolkit) and created a vb6 control where I put that .net control.
It was necessary to map all the properties, events and methods that I wanted to use.
In forms vb6 I added this vb6 control, with the .net encapsulated.
It worked!

Host WPF Application(Caliburn Framework) Inside Winforms

We have an existing Legacy Winforms application which is old and hard to maintain. So we decided to re-write the application using WPF. Due to lack of resource, we plan to have a hybrid type, where new features will be developed in WPF and over the course of time will migrate everything from Winforms to WPF.
Now the problem is, how can we integrate WPF application that uses MVVM framework to be launched from Winforms application. I tried "ElementHost" control to host WPF application. But I dont know how to achieve it with MVVM framework. Tried with Caliburn.Micro and getting an null exception when the BootStrapperBase.Initialize() method is called.
Please advice.
After reading the Caliburn documentation see here, there is an option where we can pass “false” to the base constructor’s “useApplication” parameter.
This worked for me.
This launches the application. I tried playing around passing a reference to the WPF applicaton which contains event and method.

How do you launch a WPF front end as a DLL from another assembly?

How does one create a WPF front-end that has an Application object from within another application (an existing MFC / CLR application, but that's another story)? The existence of the Application as the entry point of the assembly seems to prevent such a use. Certainly, you can use a WPF application as an assembly. Can't you?
Yes you can. I have made an plugin in WPF for winforms app (I used ElementHost Class). It was in another assembly due to a few reasons.

Using ActiveX control in WPF

I'm attempting to host an ActiveX control in a WPF app. After attempting to use existing info on the web and here, I've hit a dead-end.
I need to use an ActiveX control provided to communicate with a UV power meter. They provide an application that registers and uses the control and even includes some useful demo apps. I stripped out the OCX file and put it here if needed. You won't have the power meter to talk to, but the app and demos will still load the ActiveX control successfully.
I created a simple Windows Forms application. I was able to bring the ActiveX control into the toolbar, drop it into my form, and everything is fine. The demo apps they provide do this as well.
However, getting this to work in a WPF environment is another story. The control can't be added to the toolbox and "dragged" into the app.
So far I've tried two techniques:
Technique found here. I am able to add a reference to the control, but then I enter namespace hell. The xmlns:ax namespace it suggests making cannot find the information. Here's my attempt based on what the object viewer tells me: xmlns:ax="clr-namespace:OphirUsbXLib;assembly=Interop.OphirUsbXLib"
Technique found here. This is essentially to create a new project that creates a library based on Windows Forms, which contains the ActiveX control (yikes). I am able to add the Windows Forms Host, but I cannot get access to the ActiveX control within. I can make the control public, but I still cannot call methods etc. This doesn't look like the right solution.
In short, I have an ActiveX control that works beautifully in Forms, but is a real bitch to get working in WPF.
Any insight is appreciated!
In this situation I would consider making a WinForms usercontrol which wraps the ActiveX control you are trying to use.
You could make public properties and methods which expose each of the required properties and methods on the ActiveX control, and then host this WinForms UC on the in a WPF WinFormsHost control.
I have already done something similar to this, in reverse, hosting a WPF UserControl in a WinForms UserControl, then hosting that on a VB6 Form in a legacy application.

Guidelines for Sharing code between Silverlight and WPF

I want to share as much code as possible between Silverlight and WPF.
My lowest level handles the difference between calling the database directly and calling a web service.
My data layer on top of that is compiled with both .NET and Silverlight.
My GUI layer is currently designed as a WPF Control Library.
For sivlerlight...
Is it possible to reuse my XAML for my silverlight class library?
Do I need to create a "Silverlight Application" or can I just display controls from my class library directly?
What things do I need to be careful about? (For example, I already learned that Silverlight can't use DataTables in web service calls.)

Resources