Evitar que internet explorer pida confirmación al ejecutar un componente ActiveX

La clase debe implementar la interfaz IObjectSafety.

Para compilarlo:

Ir a prompt de visual studio 2013 (abrirlo como administrador)

Si no hay una strong key, generarla con:
      sn -k keyImpresora.snk

cls
call "%VS120COMNTOOLS%..\Tools\vsvars32.bat"
csc /t:library Impresora.cs /keyfile:keyImpresora.snk
regasm Impresora.dll /tlb:ImpresoraNet.dll /codebase


Código C#

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Collections.Generic;

namespace X
{
[ComImport()]
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IObjectSafety
{
[PreserveSig()]
int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);

[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);
}

public enum Estilo
{
Normal,
Bold,
Enlarged,
Condensed
}
public interface IImpresora
{
string Imprimir(string texto, string estilo);
void FormFeed();
void Log(string texto);
string Inicializar(string usuario);
void Agregar(string linea);
void Listar();
}

public class Linea
{
public string Texto {get; set;}
public string Estilo {get; set;}
}

[ProgId("X.Impresora")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
public class Impresora : IObjectSafety
{
bool _fin = false;
string _pathImpresora = "";
List<Linea> lineas;

    private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
    private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
    private const int S_OK = 0;

    [ComVisible(true)]
    public int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
    {
        pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
        return S_OK;
    }

    [ComVisible(true)]
    public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
    {
        return S_OK;
    }

[ComVisible(true)]
public void Agregar(string linea)
{
var l = new Linea();

l.Texto = linea;

lineas.Add(l);
}

[ComVisible(true)]
public void Listar()
{
File.Delete(Environment.GetEnvironmentVariable("TEMP") + "\\outLog.txt");

foreach (var l in lineas)
{
var xx = "";

xx = l.Texto;

xx = xx.Replace((char)13 + "" + (char)10, "");

if (xx.Contains(Convert.ToChar(27) + "(s3B") || xx.Contains(Convert.ToChar(27) + "(s0B"))
{
//evito estas lineas porque hacen bloquear la impresora.
xx = "";
} else
{
xx = xx.Replace( Convert.ToChar(27) + "E" + "", (char)27 + "" + (char)69 + "");
}

Log(xx);
}

EjecutarComando("type %TEMP%\\outLog.txt > " + _pathImpresora);
}

[ComVisible(true)]
public string Inicializar(string usuario)
{
var archivo = @"c:\X_imp\" + usuario + @"\XframePrinter.properties";
var host = "";
var nombreImp = "";


if (File.Exists(archivo))
{
var f = new StreamReader(archivo);
while (!f.EndOfStream)
{
var linea = f.ReadLine();

if (linea.Contains("XframeConnectorService.PrinterDriver.drivers"))
{
var v = linea.Split('=');

host = v[1];
host = host.Substring(0, host.IndexOf(","));

}

if (host != "")
{
if (linea.IndexOf("XframeConnectorService.PrinterDriver." + host + ".port", StringComparison.OrdinalIgnoreCase) >= 0)
{
var v = linea.Split('=');
nombreImp = v[1].Replace("\\", "");
}
}
}
} else
Log("NO EXISTE ARCHIVO DE CONFIG");

var ret = "";

if (host != "" && nombreImp != "")
{
_pathImpresora = @"\\" + host + @"\" + nombreImp;
ret = "OK";
}
else
{
_pathImpresora = "";
ret = "ERROR";
}

if (ret == "OK")
lineas = new List<Linea>();

return ret;
}

private void EjecutarComando(string comando) {
ProcessStartInfo processInfo;
Process proc;
_fin = false;
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.OutputDataReceived += OnOutputDataReceived;
proc.Exited += OnProcessExited;
proc.Start();
proc.BeginOutputReadLine();
while (!_fin) {
Thread.Sleep(1);
}

proc.Close();
}


private void OnProcessExited(object sender, EventArgs e) {
_fin = true;
}

private void OnOutputDataReceived(object sender, DataReceivedEventArgs e) {
}

[ComVisible(true)]
public void Log(string texto)
{
var f = new StreamWriter(Environment.GetEnvironmentVariable("TEMP") + "\\outLog.txt", true);

f.WriteLine(texto);

f.Close();
}
}
}

No hay comentarios:

Publicar un comentario