Enviar directo a la impresora desde C# (raw printing)



Código Html

<body>
    <object
        name="Hello World"
        classid="clsid:E86A9038-368D-4e8f-B389-FDEF38935B2F"
        codebase="http://localhost/bin/Debug/Test.ActiveX.dll">
    </object>  

    <script type="text/javascript">    
        var hw = new ActiveXObject("Test.ActiveX.PruebaX");
        
alert( hw.imprimir() );
    </script>    
</body>


Código C#

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Reflection;
using System.Diagnostics; 

namespace PruebaX
{
    [Guid("E86A9038-368D-4e8f-B389-FDEF38935B2F")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface IHelloWorld
    {
        [DispId(0)]
        string Prueba();
        [DispId(1)]
        string Imprimir();

    }

    [ComVisible(true)]
    [ProgId("Test.ActiveX.PruebaX")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IHelloWorld))]
    public class HelloWorld : IHelloWorld    
    {
        [ComRegisterFunction()]
        public static void RegisterClass(string key)
        {
            // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\ ", ""); // <-- extra space to preserve prettify only.. not in the real code

            // Open the CLSID\{guid} key for write access
            using (RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true))
            {
                // And create the 'Control' key - this allows it to show up in 
                // the ActiveX control container 
                using (RegistryKey ctrl = k.CreateSubKey("Control"))
                {
                    ctrl.Close();
                }

                // Next create the CodeBase entry - needed if not string named and GACced.
                using (RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true))
                {
                    inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
                    inprocServer32.Close();
                }

                // Finally close the main key
                k.Close();
            }
        }

        public string Prueba()
        {
            return "Prueba";
        }

        public string Imprimir()
        {
            EjecutarComando("dir c:\\temp > " + "\"" + "\\\\X00895\\Brother HL-2270DW series" + "\"");

            return "Impresion enviada";
        }


        private void EjecutarComando(string comando)
        {
            ProcessStartInfo processInfo;
            Process proc;

            processInfo = new ProcessStartInfo("cmd.exe", (Convert.ToString("/c ") + comando));
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardError = true;
            processInfo.RedirectStandardOutput = true;
            processInfo.Verb = "runas";
            proc = new Process();
            proc.EnableRaisingEvents = true;
            proc.StartInfo = processInfo;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.Close();
        }
    }
}

No hay comentarios:

Publicar un comentario