Announcement

Collapse
No announcement yet.

Are COM DLLs VB compatible ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Are COM DLLs VB compatible ?

    Hello,

    Bob wrote :
    To make a CLASS public:

    1- Make sure you have given the CLASS and each contained interface a GUID to specifically identify it.
    2- Add the words "AS COM" to the end of the CLASS statement.
    3- You now have a COM component.

    The client programs which use this component will probably need a Type Library to get the definitions of your class, so create one with #COM TLIB ON. You mey want to embed the Type Library in the DLL using PBTYP.EXE. You'll probably need to register the COM component on each client computer that uses it. That's done with REGSVR32.EXE.

    Best regards,

    Bob Zale
    Ok, then...

    OK ok I can understand the whole thing but the following code compiles fine...
    Code:
    [FONT=Arial][SIZE=2]#COMPILE DLL[/SIZE][/FONT]
    [SIZE=2][FONT=Arial]#COM TLIB ON[/FONT][/SIZE]
     
    [FONT=Arial][SIZE=2]$MyClassGuid = GUID$("{00000099-0000-0000-0000-000000000008}")[/SIZE][/FONT]
    [SIZE=2][FONT=Arial]$MyIfaceGuid = GUID$("{00000099-0000-0000-0000-000000000009}")[/FONT][/SIZE]
     
    [FONT=Arial][SIZE=2]CLASS TESTCLASS $MyClassGuid AS COM[/SIZE][/FONT]
    [SIZE=2][FONT=Arial]INTERFACE DispatchIface $MyIfaceGuid[/FONT][/SIZE]
    [SIZE=2][FONT=Arial] INHERIT automation[/FONT][/SIZE]
    [FONT=Arial][SIZE=2]   METHOD HideUI( BYVAL msg$ ) AS STRING[/SIZE][/FONT]
    [SIZE=2][FONT=Arial]       METHOD = "Hello !!" & msg$[/FONT][/SIZE]
    [SIZE=2][FONT=Arial]   END METHOD[/FONT][/SIZE]
    [SIZE=2][FONT=Arial]END INTERFACE[/FONT][/SIZE]
    [SIZE=2][FONT=Arial]END CLASS[/FONT][/SIZE]
    ...but does not work perfectly under VB/VBA (tested with Excel, wWord or Access) with the following code (after having registered the DLL and picked the TLB from the browser under Excel)

    VBA Excel code trying to invoke this COM DLL (see the VBA error below)
    Code:
    [FONT=Arial][SIZE=2]Sub test()[/SIZE][/FONT]
    [SIZE=2][FONT=Arial]   Dim o As TESTCLASS[/FONT][/SIZE]
    [FONT=Arial][SIZE=2]   o.HIDEUI '-> VBA Error 91 not an object (that is to say impossible to call the method)[/SIZE][/FONT]
    [SIZE=2][FONT=Arial]End Sub[/FONT][/SIZE]
    Can you tell me what's wrong or may be the limitations within PB compiler regarding the COM support please ?

    Yours,

    Gerome

    #2
    I have no Microsoft tools to test your app, but shouldn't you create an instance of your class in Excel first?
    If so, the reported error would make sense to me.

    Something like:

    Code:
    Sub test()
       Dim o As TESTCLASS
       Set o = New TESTCLASS
       o.HIDEUI '-> VBA Error 91 not an object (that is to say impossible to call the method)
       Set o = NOTHING
    End Sub
    You could also directly create an instance while declaring "o":

    Code:
    Sub test()
       Dim o As NEW TESTCLASS
       o.HIDEUI '-> VBA Error 91 not an object (that is to say impossible to call the method)
       Set o = NOTHING
    End Sub
    Last edited by Vincent van der Leun; 2 Feb 2009, 02:46 PM.

    Comment


      #3
      Maybe try looking at my last post here...



      I did that with late binding and VB6. Don't know anything about VBA. Might be similiar though.
      Fred
      "fharris"+Chr$(64)+"evenlink"+Chr$(46)+"com"

      Comment


        #4
        Vincent,
        Originally posted by Vincent van der Leun View Post
        I have no Microsoft tools to test your app, but shouldn't you create an instance of your class in Excel first?
        If so, the reported error would make sense to me.
        ...
        The 'New' thing does not work, here it refuses to create a new instance of TESTCLASS as if it was impossible to get a newest instance of this object.
        BTW, without the 'as New', Excel/Word & co recognize the TLB and list the 'method' exposed, but are unable to invoke it, error 91 occurs at this time... May be there's an hidden COM trick that PB has, but that I really don't know...

        Comment


          #5
          Also, with

          Code:
          METHOD HideUI( BYVAL msg$ ) AS STRING
              METHOD = "Hello !!" & msg$
          END METHOD
          When used with VB, the method will receive msg$ as an Unicode string, and will return a mix of ansi an Unicode ("Hello !!", as ansi; msg$ as unicode).

          Until PB will add native unicode support, better use variants, that do the unicode conversion automatically.
          Forum: http://www.jose.it-berater.org/smfforum/index.php

          Comment


            #6
            Dear José,
            Originally posted by José Roca View Post
            Also, with

            Code:
            METHOD HideUI( BYVAL msg$ ) AS STRING
                METHOD = "Hello !!" & msg$
            END METHOD
            When used with VB, the method will receive msg$ as an Unicode string, and will return a mix of ansi an Unicode ("Hello !!", as ansi; msg$ as unicode).

            Until PB will add native unicode support, better use variants, that do the unicode conversion automatically.
            Ok, but even replacing the code with this one does not solves the problem under VBA/Excel/Word/VBS
            Code:
               METHOD HideUI( BYVAL msg AS VARIANT ) AS VARIANT
                   METHOD = "Hello !!" & VARIANT$(msg)
               END METHOD
            If someone has a solution that **really** works under VBA/VBS Excel or alike I'll be very happy, but unfortunately I'm trying different solutions and always same problems (for the moment!) :/

            Thanks.

            Comment


              #7
              The UNICODE issue is very easy to work around (obviously not the most efficient way, but then COM was never known for that).

              All you have to do is translate the IN/OUT strings and otherwise work in ANSI...

              Code:
               
              METHOD HideUI( BYVAL msg$ ) AS STRING
                  LOCAL sBuf AS STRING
                  sBuf = ACODE$(msg$) ' Translate input.
                  ' -----
                  ' Do assignment and other ANSI work here
                  sBuf = "Hello !!" & sBuf
                  '------
                  METHOD = UCODE$(sBuf) ' Translate output.
              END METHOD
              If you want to build an object that uses both UNICODE and ANSI (perhaps with a "Unicode=TRUE|FALSE" property), then that is easy to do as well - just check the property and do the conversions as required.
              Last edited by Kev Peel; 3 Feb 2009, 01:17 AM.
              kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

              Comment


                #8
                Potentially useful?

                PB/CC: IsTextUnicode with Microsoft Unicode Layer for Win95/98/ME April 11, 2002

                (Of course, you don't need to use the MSLU if you are not on Win/9x).
                Michael Mattias
                Tal Systems (retired)
                Port Washington WI USA
                [email protected]
                http://www.talsystems.com

                Comment


                  #9
                  Wrong Interface Type - Nothing to do with Unicode!

                  Bit late for this, but I think the 'OP' had setup an 'Automation' interface, which does not work with late-binding.

                  VBA/VBS can only use 'late-binding' so the COM object has to publish an 'IDISPATCH' (or DUAL) interface to make it accessible to VBA/VBS, and you have to use 'CreateObject("classname") to instantiate an object.

                  If a language can't 'see' the object or its methods, then it hasn't been instantiated correctly for a variety of reasons. If the object was instantiated, and a string was passed incorrectly as ANSI/UniCODE, then you would probably see something in the string data, even if it was garbage.

                  +tim+

                  Comment


                    #10
                    Gerome as to your original question. Yes VB (and lil sis VBA) can work with PB-COM but danged if it is not a pain trying to come up with a Dll that works with multiple programming languages. (But it CAN be done)

                    As Tim Mentioned
                    VBA/VBS can only use 'late-binding' so the COM object has to publish an 'IDISPATCH' (or DUAL) interface to make it accessible to VBA/VBS, and you have to use 'CreateObject("classname") to instantiate an object.
                    I do not have VB installed on this machine at the moment but If you change you IAUTOMATION to IDISPATCH (to cover all bases) then you should be able to instantiate your dll.

                    Unicode vs Ansi is a different issue....1st you have to get the VB/VBA code to be able to create your object (Com)
                    Engineer's Motto: If it aint broke take it apart and fix it

                    "If at 1st you don't succeed... call it version 1.0"

                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                    Comment


                      #11
                      >with a Dll that works with multiple programming languages. (But it CAN be done)

                      A DLL is a DLL is a DLL. Your compiler can create any kind of standard interface to your procedures and interfaces in your DLLs.

                      Whether or not a procedure or interface in that DLL "works" with a particular programming language is not a function of the DLL, it's a function of the programming language.
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment

                      Working...
                      X
                      😀
                      🥰
                      🤢
                      😎
                      😡
                      👍
                      👎