Zum Inhalt springen

[C#] Com+ Latebinding innerhalb eines seperaten Threads


Empfohlene Beiträge

Geschrieben

Hallo liebe Leute,

wir binden eine Anwendung eines Drittanbieters über deren COM Objekte an.

Jetzt hatten wir die Anforderung das ganze aufgrund der Dauer in den Hintergrund zu verbannen.

Gesagt getan. Das COM Objekt wird über Latebinding angesteuert und die Klasse die das handelt wird nun innerhalb eines Threads erstellt und dannach vom selben Thread wieder sauber geschlossen und freigegeben.

Jetzt hat sich allerdings folgendes Verhalten ergeben.

Während das Programm aktiv ist geht die Ansteuerung der externen Komponente einwandfrei.

Schließt man nun das Programm, so dauert es ein paar Sekunden und dann kommt folgende Meldung:

post-47975-14430449527914_thumb.png

Laut Spy++ wird diese MessageBox jedoch nicht von unserem Produkt, sonder von dem Prozess CSRSS.exe (System Prozess) geworfen.

Zusätzlich wird folgender Eintrag im Eventlog hinzugefügt:

Name der fehlerhaften Anwendung: [Assemblyname unseres Produktes], Version: 3.407.17236.22021, Zeitstempel: 0x51649824

Name des fehlerhaften Moduls: KERNELBASE.dll, Version: 6.1.7601.17965, Zeitstempel: 0x506dbd3f

Ausnahmecode: 0x0eedfade

Fehleroffset: 0x0000c6e3

ID des fehlerhaften Prozesses: 0x1f18

Startzeit der fehlerhaften Anwendung: 0x01ce36353f76b5fd

Pfad der fehlerhaften Anwendung: [Pfad & Assemblyname unseres Produktes]

Pfad des fehlerhaften Moduls: C:\Windows\system32\KERNELBASE.dll

Berichtskennung: 9434c55d-a228-11e2-a40b-005056c00008

Das ThreadingModel des COM Objektes ist laut Registry "Apartment".

Führt man die Funktionen nicht in einem seperaten Thread aus, so wird die Meldung nicht geworfen.

Muss ich da was beachten, wenn ich ein Com Objekt innerhalb eines Threads per Latebinding ansteuere?

Irgendwelche Metaflags etc.?

Ich bin mit den nerven am Ende und hab allmählich keine Ideen mehr. Nirgends wird eine Exception geworfen, alles läuft so wie es soll nur wenn man das Programm schließt kommt diese MessageBox. Gut man könnte sagen:

Liebe Kunden ignoriert die Meldung, da diese die Funktionalität nicht beeinträchtigt. Aber was macht das für einen Eindruck :D. Habt Ihr gegebenenfalls noch Ideen?

lg

Gateway

Geschrieben

Tja. Leider kann ich Dir nicht weiterhelfen - zumindest nicht direkt.

Allerdings verhält es sich wohl so, das Eure Anwendung mittels CSRSS.exe das Threading erledigen läßt. Wenn Eure Anwendung geschlossen wird und die CSRSS.exe immer noch aktiv ist, ist anzunehmen, dass der Teil »vom selben Thread wieder sauber geschlossen und freigegeben« erstmal der fragwürdige Teil ist. Ist dem tatsächlich so? Insofern wird auch klar, warum das Eventlog so aussieht, wie es aussieht.

Geschrieben (bearbeitet)

Danke euch beiden für die fixe Antwort.

@lilith2k3:

In der Testanwendung kann man das jetzt etwas besser sehn. Dort wird das Objekt innerhalb des Threads lokal erstellt und am Ende wieder zerstört. Wird also schon innerhalb des selben Threads erstellt und freigegeben.

@Klotzkopp:

Ja ich konnte das allerselbe Verhalten in der Testanwendung nachvollziehen.

Es kam wieder die Fehlermeldung (Sekunden nachdem man das Hauptfenster geschlossen hatte).

Zusätzlich kommt jetzt ein MS Fehlerbericht:

post-47975-14430449528247_thumb.png

Die komplette Problemsignatur sieht wie folgt aus:

Problemsignatur:

Problemereignisname: APPCRASH

Anwendungsname: TestApp.vshost.exe

Anwendungsversion: 10.0.30319.1

Anwendungszeitstempel: 4ba2084b

Fehlermodulname: KERNELBASE.dll

Fehlermodulversion: 6.1.7601.18015

Fehlermodulzeitstempel: 50b83c8a

Ausnahmecode: 0eedfade

Ausnahmeoffset: 0000c41f

Betriebsystemversion: 6.1.7601.2.1.0.256.48

Gebietsschema-ID: 1031

Zusatzinformation 1: d3c4

Zusatzinformation 2: d3c4f905b8b37899e5c9fd7e42f7043b

Zusatzinformation 3: 3954

Zusatzinformation 4: 3954f091123169f91a0f249d8464ee47

Lesen Sie unsere Datenschutzbestimmungen online:

Datenschutzbestimmungen für Windows 7 - Microsoft Windows

Wenn die Onlinedatenschutzbestimmungen nicht verfügbar sind, lesen Sie unsere Datenschutzbestimmungen offline:

C:\Windows\system32\de-DE\erofflps.txt

Hier die Klasse die das Latebinding beinhaltet:


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Reflection;


namespace TestApp

{

    /// <summary>

    /// LateBinding D3Archive

    /// </summary>

    public sealed class D3RemoteCall

    {

        #region Properties

        /// <summary>

        /// 

        /// </summary>

        public bool Compress

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return (bool)Instance.GetType().InvokeMember("Compress", BindingFlags.GetProperty, null, Instance, null);

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return false;

                    }

                }

                else

                {

                    return false;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Compress", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        ////Logger.LogException(ex);

                    }

                }

            }


        }

        /// <summary>

        /// 

        /// </summary>

        public string FunctionName

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("FunctionName", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;


                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("FunctionName", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string IPAddr

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("IPAddr", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("IPAddr", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string Language

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("Language", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Language", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string Password

        {

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Password", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public int Port

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return (int)Instance.GetType().InvokeMember("Port", BindingFlags.GetProperty, null, Instance, null);

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return 0;

                    }

                }

                else

                {

                    return 0;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Port", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string ReturnCode

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("ReturnCode", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string Server

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("Server", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Server", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public int Size

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return (int)Instance.GetType().InvokeMember("Size", BindingFlags.GetProperty, null, Instance, null);

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return 0;

                    }

                }

                else

                {

                    return 0;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Size", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public int Table

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return (int)Instance.GetType().InvokeMember("Table", BindingFlags.GetProperty, null, Instance, null);

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return 0;

                    }

                }

                else

                {

                    return 0;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Table", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string TableHeadline

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("TableHeadline", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("TableHeadline", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string User

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("User", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("User", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public string Version

        {

            get

            {

                if (IsInitialized)

                {

                    try

                    {

                        return Instance.GetType().InvokeMember("Version", BindingFlags.GetProperty, null, Instance, null).ToString();

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                        return string.Empty;

                    }

                }

                else

                {

                    return string.Empty;

                }

            }

            set

            {

                if (IsInitialized)

                {

                    try

                    {

                        Instance.GetType().InvokeMember("Version", BindingFlags.SetProperty, null, Instance, new object[] { value });

                    }

                    catch (Exception ex)

                    {

                        //Logger.LogException(ex);

                    }

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public bool IsInitialized

        {

            get

            {

                if (Instance != null && TargetType != null)

                    return true;

                else

                    return false;

            }

        }

        /// <summary>

        /// IsConnected

        /// </summary>

        public bool IsConnected { get; private set; }

        /// <summary>

        /// 

        /// </summary>

        private object Instance

        {

            get;

            set;

        }

        /// <summary>

        /// 

        /// </summary>

        private List<MethodInfo> Methods { get; set; }

        /// <summary>

        /// 

        /// </summary>

        private Type TargetType { get; set; }

        /// <summary>

        /// 

        /// </summary>

        private Assembly TargetAssembly { get; set; }

        #endregion

        #region StaticVars

        public static int ExecuteCount { get; private set; }

        public static bool ExecuteSucceeded { get; private set; }

        #endregion

        #region Constr

        /// <summary>

        /// 

        /// </summary>

        /// <param name="assembly"></param>

        public D3RemoteCall(Assembly assembly)

        {

            TargetAssembly = assembly;

        }

        #endregion

        #region Functions

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public bool CreateInstance()

        {

            bool lresult = false;

            try

            {

                if (TargetAssembly != null)

                {

                    TargetType = TargetAssembly.GetType("d3remote.D3RemoteCallClass");

                    Instance = Activator.CreateInstance(TargetType);

                    Methods = new List<MethodInfo>(Instance.GetType().GetMethods());

                    lresult = true;

                }

            }

            catch (Exception ex)

            {

                //Logger.LogException(ex);

            }


            return lresult;


        }

        /// <summary>

        /// 

        /// </summary>

        public void AboutBox()

        {

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("AboutBox"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        ifo.Invoke(Instance, null);

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "AboutBox" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        public void Add()

        {

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Add"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        ifo.Invoke(Instance, null);

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Add" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public bool Connect()

        {

            bool lresult = false;

            if (IsInitialized && !IsConnected)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Connect"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, null);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                                IsConnected = lresult;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Connect" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        public void Disconnect()

        {

            if (IsInitialized && IsConnected)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Disconnect"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        ifo.Invoke(Instance, null);

                        IsConnected = false;

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Disconnect" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

        }

        /// <summary>

        /// Release

        /// </summary>

        public void Release()

        {

            if (Instance != null && IsInitialized)

            {

                try

                {

                    if (IsConnected)

                        Disconnect();

                    System.Runtime.InteropServices.Marshal.ReleaseComObject(Instance);

                }

                catch

                {


                }

                finally

                {

                    Instance = null;

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public bool Execute()

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Execute"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, null);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                                ExecuteSucceeded = lresult;

                                ExecuteCount++;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Execute" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public bool First()

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("First"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, null);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "First" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Name"></param>

        /// <returns></returns>

        public string get_Exporting(string Name)

        {

            string result = string.Empty;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("get_Exporting"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, new object[] { Name });

                        if (val != null)

                            result = val.ToString();

                        else

                            result = string.Empty;

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "get_Exporting" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return result;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Name"></param>

        /// <returns></returns>

        public object get_Field(string Name)

        {

            object result = DBNull.Value;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("get_Field"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, new object[] { Name });

                        result = val ?? DBNull.Value;

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "get_Field" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return result;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Name"></param>

        /// <returns></returns>

        public string get_Importing(string Name)

        {

            string result = string.Empty;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("get_Importing"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, new object[] { Name });

                        if (val != null)

                            result = val.ToString();

                        else

                            result = string.Empty;

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "get_Importing" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return result;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Filename"></param>

        /// <param name="ErrNo"></param>

        /// <param name="Comment"></param>

        /// <returns></returns>

        public bool GetError(out string Filename, out int ErrNo, out string Comment)

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("GetError"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object[] parameter = new object[] { string.Empty, 0, string.Empty };

                        object val = ifo.Invoke(Instance, parameter);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                        if (lresult)

                        {

                            try

                            {

                                if (parameter[0] != null)

                                    Filename = parameter[0].ToString();

                                else

                                    Filename = string.Empty;

                                if (parameter[1] != null)

                                    ErrNo = (int)parameter[1];

                                else

                                    ErrNo = 0;

                                if (parameter[2] != null)

                                    Comment = parameter[2].ToString();

                                else

                                    Comment = string.Empty;

                            }

                            catch

                            {

                                Filename = string.Empty;

                                ErrNo = 0;

                                Comment = string.Empty;

                            }


                        }

                        else

                        {

                            Filename = string.Empty;

                            ErrNo = 0;

                            Comment = string.Empty;

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "GetError" });

                        Filename = string.Empty;

                        ErrNo = 0;

                        Comment = string.Empty;

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                    Filename = string.Empty;

                    ErrNo = 0;

                    Comment = string.Empty;

                }

            }

            else

            {

                Filename = string.Empty;

                ErrNo = 0;

                Comment = string.Empty;

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Filename"></param>

        /// <param name="Status"></param>

        /// <param name="Key"></param>

        /// <param name="DestFile"></param>

        /// <returns></returns>

        public bool GetFile(string Filename, string Status, string Key, out string DestFile)

        {

            bool lresult = false;


            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("GetFile"));

                    if (m_info.Count() > 0)

                    {

                        object[] parameters = new object[] { Filename, Status, Key, string.Empty };

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, parameters);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                        if (lresult)

                        {

                            if (parameters[3] != null)

                                DestFile = parameters[3].ToString();

                            else

                                DestFile = string.Empty;

                        }

                        else

                        {

                            DestFile = string.Empty;

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "GetFile" });

                        DestFile = string.Empty;

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                    DestFile = string.Empty;

                }

            }

            else

            {

                DestFile = string.Empty;

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Filename"></param>

        /// <returns></returns>

        public bool LoadTableFromFile(string Filename)

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("LoadTableFromFile"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, new object[] { Filename });

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "LoadTableFromFile" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public bool Next()

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Next"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, null);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Next" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public bool Previous()

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Previous"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, null);

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Previous" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public int Process()

        {

            int retval = 0;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("Process"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, null);

                        if (val != null)

                        {

                            try

                            {

                                retval = (int)val;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "Process" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return retval;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Filename"></param>

        /// <returns></returns>

        public bool SaveTableToFile(string Filename)

        {

            bool lresult = false;

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("SaveTableToFile"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, new object[] { Filename });

                        if (val != null)

                        {

                            try

                            {

                                lresult = (bool)val;

                            }

                            catch

                            {


                            }

                        }

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "SaveTableToFile" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

            return lresult;

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Name"></param>

        /// <param name="Value"></param>

        public void set_Exporting(string Name, string Value)

        {

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("set_Exporting"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        ifo.Invoke(Instance, new object[] { Name, Value });

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "set_Exporting" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Name"></param>

        /// <param name="Value"></param>

        public void set_Field(string Name, string Value)

        {

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("set_Field"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        ifo.Invoke(Instance, new object[] { Name, Value });

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "set_Field" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Name"></param>

        /// <param name="Value"></param>

        public void set_Importing(string Name, string Value)

        {

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("set_Importing"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        ifo.Invoke(Instance, new object[] { Name, Value });

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "set_Importing" });

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                }

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="Filename"></param>

        /// <param name="Status"></param>

        /// <param name="Key"></param>

        /// <returns></returns>

        public string vbsGetFile(string Filename, string Status, string Key)

        {

            if (IsInitialized)

            {

                try

                {

                    var m_info = Methods.Where(P => P.Name.Equals("vbsGetFile"));

                    if (m_info.Count() > 0)

                    {

                        MethodInfo ifo = m_info.ToList()[0];

                        object val = ifo.Invoke(Instance, new object[] { Filename, Status, Key });

                        if (val != null)

                            return val.ToString();

                        else

                            return string.Empty;

                    }

                    else

                    {

                        //Logger.WriteLine(LogLevel.Error, LogCategory.Application, LanguageServer.GetInstance().GetPhrase(1059696), new object[] { Instance.GetType().FullName, "vbsGetFile" });

                        return string.Empty;

                    }

                }

                catch (Exception ex)

                {

                    //Logger.LogException(ex);

                    return string.Empty;

                }

            }

            else

            {

                return string.Empty;

            }

        }

        #endregion

        #region Destructor

        /// <summary>

        /// 

        /// </summary>

        ~D3RemoteCall()

        {

            Release();

        }

        #endregion

    }

}


Und hier haben wir den Testaufruf:

    public partial class Form1 : Form

    {

        private Thread m_Thread;

        Assembly assembly = null;

        public Form1()

        {

            InitializeComponent();

            assembly = Assembly.LoadFrom(string.Format("{0}\\{1}", Path.GetDirectoryName(Application.ExecutablePath).TrimEnd(new char[] { '\\' }), "Interop.d3remote.dll"));

        }


        private void button1_Click(object sender, EventArgs e)

        {

            if (m_Thread == null || !m_Thread.IsAlive) 

            {

                m_Thread = new Thread(new ThreadStart(ThreadContext));

                m_Thread.Start();

            }

        }


        private void ThreadContext() 

        {

            D3RemoteCall interop = InitRemoteCall();

            interop.Connect();

            interop.Release();

        }


        protected internal D3RemoteCall InitRemoteCall()

        {

            D3RemoteCall d3RemoteCall = null;


            try

            {

                if (d3RemoteCall == null)

                {

                    //Assembly assembly = null;

                    //assembly = Assembly.LoadFrom(interopFilename);

                    d3RemoteCall = new D3RemoteCall(assembly);

                    if (!d3RemoteCall.CreateInstance())

                    {

                        return null;

                    }

                    d3RemoteCall.IPAddr = "192.168.16.158";

                    d3RemoteCall.Port = 3400;

                    d3RemoteCall.Server = "3rdParty";

                    d3RemoteCall.User = "max";

                    d3RemoteCall.Password = "max";

                    d3RemoteCall.Version = "XXX10001";

                    d3RemoteCall.Language = "049";

                }

            }

            catch (Exception ex)

            {

                return null;

            }

            return d3RemoteCall;

        }

    }

LG

Gateway

Bearbeitet von Gateway_man

Dein Kommentar

Du kannst jetzt schreiben und Dich später registrieren. Wenn Du ein Konto hast, melde Dich jetzt an, um unter Deinem Benutzernamen zu schreiben.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung wiederherstellen

  Nur 75 Emojis sind erlaubt.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Editor leeren

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

Fachinformatiker.de, 2024 by SE Internet Services

fidelogo_small.png

Schicke uns eine Nachricht!

Fachinformatiker.de ist die größte IT-Community
rund um Ausbildung, Job, Weiterbildung für IT-Fachkräfte.

Fachinformatiker.de App

Download on the App Store
Get it on Google Play

Kontakt

Hier werben?
Oder sende eine E-Mail an

Social media u. feeds

Jobboard für Fachinformatiker und IT-Fachkräfte

×
×
  • Neu erstellen...