ActiveX - CREATEOBJECT and singleton pattern

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby Antonio Linares » Wed Sep 29, 2021 11:03 am

Please try this:

local o1 := CreateObject( "PaymentService.BluetoothConnector" )
local o2 := TOleAuto():New( o1:Invoke( "GetInstance" ) )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby metro2 » Wed Sep 29, 2021 11:17 am

////////////////////////////////////////////
o1 := CreateObject( "PaymentService.BluetoothConnector" )
o2 := TOleAuto():New( o1:Invoke( "GetInstance" ) )

MsgInfo( ValType( o1 ) ) // -> "O"

MsgInfo( ValType( o2 ) ) // -> "O"


////////////////////////////////////////////
metro2
 
Posts: 12
Joined: Tue Sep 07, 2021 4:34 am
Location: Croatia

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby Antonio Linares » Wed Sep 29, 2021 12:20 pm

> oPOS := CREATEOBJECT("BluetoothConnector.GetInstance('TestConnectionDLL()' ) or something ?

o2 should be the oPOS you were looking for
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby metro2 » Wed Sep 29, 2021 1:26 pm

Ok, i'll try to call TestConnectionDLL with an argument ...

I will report the result ...
metro2
 
Posts: 12
Joined: Tue Sep 07, 2021 4:34 am
Location: Croatia

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby metro2 » Mon Nov 15, 2021 6:32 am

Greeting,
I was away for a while.

If an o2 instance is called from BluetoothConnector,

How to call the TestConnectionDll method eg:

ret: = o2: TestConnectionDll ("") // -> error
ret: = o2: BluetoothConnector: TestConnectionDll ("") // -> error

In other words, no matter how I try to call the method, I get an error. I'm probably wrong in syntax.

Does anyone have an idea how to call a certain method?

Antonio, thanks for the prior help.

Image
metro2
 
Posts: 12
Joined: Tue Sep 07, 2021 4:34 am
Location: Croatia

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby Antonio Linares » Mon Nov 15, 2021 7:59 am

Please try using:

o2:Invoke( "TestConnectionDll", "" )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42122
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: ActiveX - CREATEOBJECT and singleton pattern

Postby metro2 » Mon Nov 15, 2021 1:24 pm

hi,

Antonio thanks for taking care of my problem ....

Yes, i tried this too, but it reports an error
o2:Invoke( "TestConnectionDll", "" )

Just today I intended to send a description of what was happening in the meantime.

On the forum https://stackoverflow.com/questions/6252334/singleton-com-object-required
I found the following:

COM does not directly support the Singleton pattern, but it does not strictly prohibit it either. It's just that there's no registry setting that says "always serves the same object." In fact, the standard COM instance mechanism requires that a truly new object be returned each time you call it (this mechanism is used internally by new operators and CreateInstance (). This means that to create a valid COM singleton, you cannot allow your clients to do it themselves all of this can be done, but it is tricky and rarely necessary.



From this I concluded that the classic way of using singleton technology, cannot be achieved by a direct call to GetInstance.

The idea is to add a method, which I will call, internally raising the singleton instance.

I made a test program with c #:
class1.cs
Code: Select all  Expand view  RUN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTest    
{
    public class Singleton
    {

        public string GetCardType()
        {
            return "MoneyBack";
        }
        public int GetCreditLimit()
        {
            return 15000;
        }
        public int GetAnnualCharge()
        {
            return 500;
        }

        public string GetCardTypeRet( string ret )
        {
            //Console.WriteLine("Counter Value " + ret);  // due to testing from the console application ....
            return ret;
        }

    }
    //---------------------------------------------
    public class Car
    {

        public string GetCardColorRet(string ret)
        {
            return ret;
        }



        public object MyFunction(int something = 0, string somethingelse = "zzz")   // the arguments are due to future testing
        {
            var ret = new Singleton();
            return ret;
        }

        public object GetObj()                     //    the method, which I will call, internally raises the singleton instance.                            
        {
              SingletonTest fromEmployee = SingletonTest.GetInstance;

              return fromEmployee;
        }


    }
    //---------------------------------------------
    public sealed class SingletonTest   //
    {
        private static int counter = 0;

        /*
         * Private property initilized with null
         * ensures that only one instance of the object is created
         * based on the null condition
         */

        private static SingletonTest instance = null;

        /*
         * public property is used to return only one instance of the class
         * leveraging on the private property
         */

        public static SingletonTest GetInstance
        {
            get
            {
                if (instance == null)
                    instance = new SingletonTest();
                return instance;
            }
        }
        /*
         * Private constructor ensures that object is not
         * instantiated other than with in the class itself
         */

        private SingletonTest()  
        {
            counter++;
            //Console.WriteLine("Counter Value " + counter.ToString() + " formirao instancu Singleton");   // due to testing from the console application ....

        }
        /*
         * Public method which can be invoked through the singleton instance !!!
         */

        public void PrintDetails(string message)   // due to testing from the console application ....
        {
            //Console.WriteLine(message);              
        }
       
       
        // added ... test function !!!
        public string GetCardTypeRet(string ret)
        {
            return ret;
        }


        public string TestConnectionDLL(string ret)
        {
            return ret;
        }

    }

}
 


olebrow:

Image


Register COM
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm /codebase C:\Users\Dubravko\source\repos\MyTest\MyTest\bin\Release\MyTest.dll


prg code:

Code: Select all  Expand view  RUN

     // visible class  
     o1   := CREATEOBJECT("MyTest.Singleton")                //    namespace->MyTest  public class->Singleton
      alert(valtype(o1))                                                   // "O"
      alert(o1:GetCardTypeRet("Tu sam 2!!!!!!"))              // -> "Tu sam 2!!!!!!"
           

      o2   := CREATEOBJECT("MyTest.Car")                        //    
      alert(valtype(o2))                                                   // -> "O"
      alert(valtype(   o2:GetObj()     ) )                            // -> "O"            
     
      o3 :=  o2:GetObj()                                                   // -> objekt - dignuta instanca
      alert( o3:TestConnectionDLL("string_ret") )              // -> "string_ret" the test calls methods from a singleton instance

             


Now I expect TPS900 terminal vendors to add a method that will internally boot up the singleton instance and try it all out.

Here it is additionally good, which can be solved in this way, to manage the bluetooth port from FWH.












regards
Dubravko Basic
http://www.inteh.hr
metro2
 
Posts: 12
Joined: Tue Sep 07, 2021 4:34 am
Location: Croatia

Previous

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 73 guests