titanium-as / TitaniumAS.Opc.Client

Open source .NET client library for OPC DA
MIT License
193 stars 94 forks source link

Bootstrap error in Winforms app #30

Open karlhm76 opened 6 years ago

karlhm76 commented 6 years ago

Hi,

I've installed the package into my project I have changed threading model to MTAThread. I call bootstrap in the main() function before the program is executed

namespace BoningRoomVisionSystemTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        //[STAThread]
        static void Main()
        {
            System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.MTA);

            TitaniumAS.Opc.Client.Bootstrap.Initialize(); <-- exception here!

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new VisionSystemTest());
        }
    }
}

however when I try to call the bootstrap I receive the error: -2147417831 CoInitializeSecurity: Security must be initialized before any interfaces are marshalled or unmarshalled. It cannot be changed once initialized.

However if I follow the link provided in the Bootstrap.cs to pinvoke.net the example provided shows that the threading model should be STA. When I try this I get an exception saying that I cannot set this threading model. I think it may already be set to this, as it seemed to be the default before I commented it out in the code.

Even with STA model I still receive the same COM exception error.

After this error I cannot connect to a local OPCEnum server. I get an error saying the class is not registered, but I think it is because bootstrap has not been called.

How can I get this to work properly?

Thanks

Karl.

timverwaal commented 5 years ago

Bootstrap.Initialize() should be run first, and then a new thread should be started. At least, that is working for me.

Below code is how I've done it:

static void Main(string[] args) 
        {
            Bootstrap.Initialize();

            var thread = new Thread(RunApplication);
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();     
        }

        static void RunApplication()
        {
            Console.WriteLine("Application running...");    

            // Write your program

            }
        }    
    }
commik commented 5 years ago

Hi all I have the same issue

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

using TitaniumAS.Opc.Client.Common; using TitaniumAS.Opc.Client.Da; using TitaniumAS.Opc.Client.Interop.Da; using TitaniumAS.Opc.Client.Interop.Helpers; using TitaniumAS.Opc.Client; using TitaniumAS; using System.CodeDom;

using System.Threading;

namespace ConsoleApplication1 {

class Program
{

    public enum RpcAuthnLevel
    {
        Default = 0,
        None = 1,
        Connect = 2,
        Call = 3,
        Pkt = 4,
        PktIntegrity = 5,
        PktPrivacy = 6
    }

    public enum RpcImpLevel
    {
        Default = 0,
        Anonymous = 1,
        Identify = 2,
        Impersonate = 3,
        Delegate = 4
    }

    public enum EoAuthnCap
    {
        None = 0x00,
        MutualAuth = 0x01,
        StaticCloaking = 0x20,
        DynamicCloaking = 0x40,
        AnyAuthority = 0x80,
        MakeFullSIC = 0x100,
        Default = 0x800,
        SecureRefs = 0x02,
        AccessControl = 0x04,
        AppID = 0x08,
        Dynamic = 0x10,
        RequireFullSIC = 0x200,
        AutoImpersonate = 0x400,
        NoCustomMarshal = 0x2000,
        DisableAAA = 0x1000
    }

   /* struct SOLE_AUTHENTICATION_SERVICE
    {
        public int dwAuthnSvc;
        public int dwAuthzSvc;
        [MarshalAs(UnmanagedType.BStr)]
        public string pPrincipalName;
        public int hr;
    }
    */

    [System.Runtime.InteropServices.DllImport("ole32.dll")]
    public static extern int CoInitializeSecurity(IntPtr pVoid, int
        cAuthSvc, IntPtr asAuthSvc, IntPtr pReserved1, RpcAuthnLevel level,
        RpcImpLevel impers, IntPtr pAuthList, EoAuthnCap dwCapabilities, IntPtr
        pReserved3); 

    static void Main(string[] args)
    {
        Bootstrap.Initialize();

second try to run was like : System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.MTA); Bootstrap.Initialize();

even when I change the apartment state to MTA its still not work. The system tells me that it is MTA but trhrow exeption that it is not MTA

I really try to look everywhere but still didnt found why it is not running

commik commented 5 years ago

One point I found out for following code its work

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TitaniumAS.Opc.Client.Common; using TitaniumAS.Opc.Client.Da; using TitaniumAS.Opc.Client.Interop.Da; using TitaniumAS.Opc.Client.Interop.Helpers; using TitaniumAS.Opc.Client; using TitaniumAS; using System.CodeDom; using System.Threading; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Bootstrap.Initialize();

before to lunch need to be switch off => Enable the Visual Studio hosting process to switch it off you need to open properties of your application in Solution Explorer in the section Debug.

After switch it Off the problem will be gone.

But After that another problem will show when I try to create new OPCclinet then I have got another error

karlhm76 commented 5 years ago

Thanks for the reply.

I never was able to get this to work at all so i used a 32bit COM library instead which would do everything except receive tag update messages. I fixed this by polling all the tags in a separate thread but this would make the OPCenum server stop working after a few months.

Some time later i managed to find a copy of the official OPC foundation OPC.NET library and used that. This was provided by a contractor who was doing some work for the company i was working for at the time. I think i still have a copy somewhere.

Best of luck with it.

Cheers

Karl.

crxmaniac commented 3 years ago

Thanks for the reply.

I never was able to get this to work at all so i used a 32bit COM library instead which would do everything except receive tag update messages. I fixed this by polling all the tags in a separate thread but this would make the OPCenum server stop working after a few months.

Some time later i managed to find a copy of the official OPC foundation OPC.NET library and used that. This was provided by a contractor who was doing some work for the company i was working for at the time. I think i still have a copy somewhere.

Best of luck with it.

Cheers

Karl.

Hi Karl I am in the sam situation Do you still have the copy ?

best regards

Vegard

karlhm76 commented 3 years ago

I'll look around my many cloud stores and share and post a link if i find it.

crxmaniac commented 3 years ago

I'll look around my many cloud stores and share and post a link if i find it.

No luck?

karlhm76 commented 3 years ago

I'll look around my many cloud stores and share and post a link if i find it.

No luck?

https://mega.nz/folder/s6BVxAoR#GV2pxV1SzoVbo4c0DVQTFA

See if this link works. I'll take it down in a few days. There's plenty of info on how to use it around the place.