Question

I have a pretty complex class which extends System.Windows.Forms.UserControl. It's written in C++/CLI with the /clr switch, because it uses native C++ dlls. The class's main responsibility is acting as a wrapper for an unmanaged Direct3D9 dll which renders images over textures, zooms, pans, etc.

Is there any way to reuse this class in a .NET web application? In other words, I want to get my user control to run inside a browser. The main uncertainties here are the access to native dlls, and the use of Direct3D. I'm setting up a small test to see if I can get this to work, but I'd like some input from someone else.

Update: I followed this guide. On the first try, I got an error saying that it could not load the control's dll or one of its dependencies. So, just for testing, I copied all the unmanaged dlls it uses to system32. Now I can load the page without errors, but I still cannot see the control. I placed the reference to it in the About page from the default C# Web App project, like so:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        About
    </h2>
    <p>
        Put content here.
    </p>
    <object id="MyWinControl1" height="200" width="200" classid="http:MNeoConsole.dll#MNeoConsole.MNeoConsoleControl">
        <param name="Title" value="Locura loca" />
        <param name="Visible" value="true" />
    </object>
</asp:Content>

I'll try the ActiveX approach now, but even if that works, I guess that would force my clients to use Internet Explorer, or install some plugin, wouldn't it? That would be really bothersome.

Edit: By now, I'm willing to try other approaches. For example, since my C++/CLI class is just a wrapper, I'm willing to write another wrapper in some other laguage. If I go that way, that language should be as friendly with web applications as possible.

Was it helpful?

Solution

runat="server" is not going to make your control run inside a browser. I think you have a fundamental misunderstanding of how ASP.NET works. Web controls don't run inside the browser. They run on the server and produce HTML code that is sent to the browser, and may include various HTML controls and Javascript snippets.

Any mechanism for client-side code other than HTML5 (Javascript) is going to require client-side support, i.e. the plugin you were trying to avoid. Of the possible plugins, the one that's closest to WinForms and most portable is going to be Silverlight. That uses WPF, not WinForms, for user interface components. There is a WPF host for WinForms controls, but I don't know if you can use that from inside Silverlight.

Also, you need a bunch of special compile options in order to use C++/CLI with Silverlight. By default C++/CLI generates machine-specific native code which runs outside the browser sandbox. That's going to be blocked by any user with a sane security configuration. You're going to need to emit pure MSIL code and enable "Partial Trust". Then you're going to run into trouble with the native component you're wrapping.

There's pretty much no advantage to doing this in a web browser. Just have the user download a WinForms application. You can use ClickOnce or another packaging technology to keep the application updated to the latest version on your site, but neither ASP.NET nor any of the browser technologies is going to play nicely with your control. Your other option is a rewrite, not of the wrapper, but of the whole OpenGL rendering layer, to use WebGL instead.

OTHER TIPS

I can't say if this gonna work in your particular scenario, but you can try to load it as an ActiveX. See this:

http://www.codeproject.com/Articles/4953/Simple-way-to-expose-a-NET-WinForm-control-as-an-A

Think about it for just a second, you want to call an unmanaged code (that can do exactly anything that user that run it can do, for example create a new user or even format your hard drive) from within a control that run in a web page and you want to use an standard approach that work with any browser (or at least most of them?). in my opinion there is no such thing available, you should fallback to OCX that as you said only supported by MSIE and some version of Geko (FireFox) through window.GeckoActiveXObject or use a more portable plugin such as Flash or Silverlight that work on any (or at least most platforms) and replace your native functions calls with those that is available in those plugins.

C++/CLI IS a .NET language, so why not? If you mean you want to use it from a C# Web Application project, that's also easy, just compile the C++/CLI project as a Class Library project (a DLL) and add a reference to it from the C# project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top