Announcement

Collapse
No announcement yet.

VB6 Example with PB COM Server

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

  • VB6 Example with PB COM Server

    A simple little example showing how to instantiate a PB COM Server in VB6. All source code is included in a zip file at the bottom of this post.

    You must first register the server vbt.dll with
    Code:
    regsvr32 vbt.dll

    PBWin 9 Server vbt.bas:
    Code:
    #COMPILE DLL
    #DIM ALL
    
    #COM NAME "VBTst"
    #COM TLIB ON
    
    #RESOURCE "vbt.pbr"
    
    CLASS CVBT GUID$("{1F8B0CB3-A491-4336-A133-5BA8CC6F7C8A}") AS COM
      INTERFACE IVBT GUID$("{B1E286E4-0588-43F0-9D94-2F0F5B72218D}")
        INHERIT IDISPATCH
    
        PROPERTY GET GetTime AS STRING
          PROPERTY = UCODE$(TIME$)
        END PROPERTY
    
      END INTERFACE
    END CLASS
    PBWin 9 Resource vbt.rc:
    Code:
    #include "resource.h"
    
    1 typelib "vbt.tlb"
    VB Project1.vbp
    Code:
    Type=Exe
    Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\WINDOWS\system32\stdole2.tlb#OLE Automation
    Reference=*\G{955F2417-1292-451F-88FF-1DE8EE42E989}#1.0#0#vbt.dll#COM Library
    Form=Form1.frm
    Startup="Form1"
    ExeName32="Project1.exe"
    Command32=""
    Name="Project1"
    HelpContextID="0"
    CompatibleMode="0"
    MajorVer=1
    MinorVer=0
    RevisionVer=0
    AutoIncrementVer=0
    ServerSupportFiles=0
    CompilationType=0
    OptimizationType=0
    FavorPentiumPro(tm)=0
    CodeViewDebugInfo=0
    NoAliasing=0
    BoundsCheck=0
    OverflowCheck=0
    FlPointCheck=0
    FDIVCheck=0
    UnroundedFP=0
    StartMode=0
    Unattended=0
    Retained=0
    ThreadPerObject=0
    MaxNumberOfThreads=1
    
    [MS Transaction Server]
    AutoRefresh=1
    VB Form1.frm
    Code:
    VERSION 5.00
    Begin VB.Form Form1 
       Caption         =   "Form1"
       ClientHeight    =   3090
       ClientLeft      =   60
       ClientTop       =   450
       ClientWidth     =   4680
       LinkTopic       =   "Form1"
       ScaleHeight     =   3090
       ScaleWidth      =   4680
       StartUpPosition =   3  'Windows Default
       Begin VB.TextBox Text1 
          Height          =   495
          Left            =   240
          TabIndex        =   1
          Text            =   "Text1"
          Top             =   120
          Width           =   1215
       End
       Begin VB.CommandButton Command1 
          Caption         =   "Command1"
          Height          =   495
          Left            =   1680
          TabIndex        =   0
          Top             =   120
          Width           =   1215
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Private Sub Command1_Click()
        Dim o As CVBT
        Dim s As String
        
        Set o = CreateObject("CVBT")
        
        s = o.GETTIME
        Text1.Text = s
        
    End Sub
    Attached Files
    Last edited by Steve Rossell; 26 Aug 2008, 11:48 AM.
    Sincerely,

    Steve Rossell
    PowerBASIC Staff


  • #2
    It crashes VB

    Sorry Steve, this doesn't work...
    1. Your code crashes VB.
    2. In order to get to this point I had to reference the DLL that should be required only for early binding, yet early binding does not work (the New keyword is not accepted)
    3. When I referenced the DLL its name in the VB Object Browser changed to "COM object"

    Peter Redei

    Comment


    • #3
      Did you register vbt.dll and then run Project1.exe?
      Sincerely,

      Steve Rossell
      PowerBASIC Staff

      Comment


      • #4
        Runs like a champ, I unregistered it and get run error 429 . no crash.

        Peter, Just out of curiousness what flavor of windows are you running ? Have you patch VB (6) to sp6 ? and the ole32 patch(es) ? Not that is should matter. I been seeing strange things with VB and I think it is some how related to patches for VB and OS patches.

        to unregister ... regsvr32 vbt.dll /u
        Last edited by Michael Mayerhoffer; 26 Aug 2008, 02:56 PM.
        A dozen what.

        Comment


        • #5
          More

          Steve,
          I registered the DLL of course. I even tried it with registering the tbl. This removed the DLL registration but gave me identical results. Than I unregistered the tlb and registered the DLL. Still crashed. I have SP5 installed, I also have Visual Studio'2005 installed. I just tried in another machine with SP6 installed with identical results.

          One addition: This happens when I am running within the VB Development Environment (debug mode). If I compile the program it runs. It also runs in the VBDE if I change your code, remove the reference to the DLL and use Dim o As Object instead of Dim o CVBT. (Actually this is how it should be coded for late binding.)

          Early binding is not possible for the dll and in the VB Object Browser its name is changing as I mentioned before.

          Peter Redei

          Comment


          • #6
            VB uses the help string for the reference dialog box, you can set the help string using the #COM HELP metastatement. I am not a VB expert but it is not crashing here when running the program in the VB IDE. If it is crashing while debugging but runs when compiled as an exe, this sounds like a VB problem.
            Sincerely,

            Steve Rossell
            PowerBASIC Staff

            Comment


            • #7
              >but runs when compiled as an exe, this sounds like a VB problem.

              Actually, Steve, it's a programmer problem. It's not like you should expect a patch to VB6, is it?

              PS: It is somewhat unbecoming of an employee ("PB Staff") of a competing product publisher to claim errors in the competition's products. " I am not a VB expert " might suffice for someone who is not a PB employee, but I'm afraid some of your First Amendment rights are restricted as an employee of PowerBASIC Inc.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Helpstring ("COM Library") is what I see compiling any com dll.

                Far as I can see, we have no control over helpstring,
                A dozen what.

                Comment


                • #9
                  Sorry, it is #COM DOC that you want to use to set the help string. #COM HELP is for the help file name.
                  Sincerely,

                  Steve Rossell
                  PowerBASIC Staff

                  Comment


                  • #10
                    The problem goes beyond being able to run in compiled exe but not in the VBDE.:
                    1. VB developers are unable to debug their program that use PB ActiveX component
                    2. Early binding from VB is not available
                    3. Intellisense does not work with PB ActiveX
                    4. The Name if the ActiveX library is changing upon referencing it.

                    I have not yet tried the same from Javascript, VB.NET or from C# but I suspect similar problems.


                    Peter Redei

                    Comment


                    • #11
                      Thank you Steve !

                      Unload VB project

                      If you ran this here is how to fix it.

                      run regsvr32 vbt.dll /u

                      add this to the PB CODE #COM DOC "VBTst".

                      re compile dll

                      open VB prokect ignore warming, open refernce browser and add VBTst.

                      run regsvr32 vbt.dll

                      The dll uses dual interface with IDispatch , this is the correct way to call it.
                      A dozen what.

                      Comment


                      • #12
                        Intellisense - will work with it correctly referenced now.
                        A dozen what.

                        Comment


                        • #13
                          Michael,
                          I did what you suggested. It did not change anything. Intellisense doesn't work because it works only with early binding that is not possible.
                          I also tried binding the ActiveX from Visual Studio'2005. It resulted in this error message:



                          Peter Redei

                          Comment


                          • #14
                            Hi Peter,

                            here are a couple of Access 2003 observations relating to your list above.

                            1. VB developers are unable to debug their program that use PB ActiveX component
                            While testing James Fullers example code from the other thread, I am able to successfully use the debugger [F8] within Access to step through the VBA code.


                            2. 4. The Name if the ActiveX library is changing upon referencing it.
                            I am also seeing the COM library name change upon referencing/de-referencing in the VBA environment. When the library is not referenced, the #COM NAME string appears in the list of available references. When the library is referenced, the #COM DOC string appears.

                            I have just dug out my Visual Studio disks and will try Steves example later in VB6 when I get a chance.

                            David

                            Comment


                            • #15
                              Peter,

                              The correctly referenced file should work now with Intellisense and display as VBTst in the object browser. Intellisense will only work for o.time. If there was more to the dll you could see more Intellisense action.

                              NEW is not going to work or show up with Intellisense, only for early binding stuff. MS also notes to avoid using NEW. !?!?


                              The com object is being called correctly as far as I can tell. It has a dual interface, IDispatch with automation. The documents from Microsoft says this is late binding and the vb code is correct.

                              I am not seeing it getting unreferenced. The example works just fine here.
                              Sorry tuff to catpture Intelliguess on the fly
                              Attached Files
                              A dozen what.

                              Comment


                              • #16
                                Maybe I am restating something that was already said but you need to.

                                1. rem out the resource line
                                2. compile to get the tlb
                                3. compile the rc
                                4. compile the res
                                5. un rem the resource line
                                6. recompile project

                                Easier to.

                                1. compile project with minimal resource file
                                2. run pbtyp to import tlib

                                or am I restating a fact here?
                                Sr. Software Development Engineer and Sr. Information Security Analyst,
                                CEH, Digital Forensic Examiner

                                Comment


                                • #17
                                  Hi Folks,

                                  I have just tested Steves example and have some mixed results on my machine.

                                  1) I modified vbt.bas on my development machine. Changed the com name and doc to differentiate them in the VB references list in the event that the names change on referencing as seen before.

                                  Inserted a new line...
                                  Code:
                                  #COM DOC "VBTstDOC"
                                  amended
                                  Code:
                                  #COM NAME "VBTst" to... 
                                  #COM NAME "VBTstNAME"
                                  compiled vbt.rc then vbt.bas to create new vbt.dll

                                  2) Copied all VB project files to test machine...

                                  (Win XP fully updated from Microsoft Update site
                                  Fresh install of Visual Studio 6 updated to SP6)

                                  3) ran regsvr32.exe vbt.dll

                                  4) Loaded project into VB6 SP6

                                  5) Set the reference to VBTstDOC
                                  * note saw no change in COM library name as I am seeing in Access VBA environment on the other machine.

                                  6) Started With Full Compile CTRL+F5
                                  Clicked Command1 Button... application updated time into text box as expected

                                  7) Edited the Form1.Command1 VB code
                                  debug/stepped into it [F8]
                                  Clicked Command1 Button...

                                  VB6 crashed when executing the line...
                                  Set o = CreateObject("CVBT")

                                  in Original code...

                                  Code:
                                     Private Sub Command1_Click()
                                         Dim o As CVBT
                                         Dim s As String
                                      
                                         Set o = CreateObject("CVBT")
                                      
                                         s = o.GETTIME
                                         Text1.Text = s
                                      
                                     End Sub
                                  two error dialogs produced...

                                  ********************
                                  Visual Basic has encountered a problem and needs to close. We are sorry for the inconvenience.
                                  Error signature
                                  AppName: vb6.exe
                                  AppVer: 6.0.97.82
                                  ModName: vbt.dll
                                  ModVer: 0.0.0.0
                                  Offset: 00003012
                                  ********************

                                  ********************
                                  VB6.exe - Application Error

                                  (X) The instruction at "0x0fa91841" referenced memory at "0x69767269". The memory could not be "read".
                                  Click on OK to terminate the program [OK]
                                  ********************

                                  8) I tried a second code variant which again worked with a full compile
                                  but crashed on the CreateObject when debug/stepping. Producing exactly
                                  the same error dialogs as before.

                                  Code:
                                     Private Sub Command1_Click()
                                         Dim o As VBTstNAME.CVBT
                                         Dim s As String
                                      
                                         Set o = CreateObject("CVBT")
                                      
                                         s = o.GETTIME
                                         Text1.Text = s
                                      
                                     End Sub

                                  9) I tried a third code variant which works with both a full compile
                                  and when debug/stepping.

                                  Code:
                                     Public o As VBTstNAME.CVBT
                                  
                                     Private Sub Command1_Click()
                                  
                                         Dim s As String
                                      
                                         Set o = CreateObject("CVBT")
                                      
                                         s = o.GETTIME
                                         Text1.Text = s
                                      
                                     End Sub

                                  Hope that helps someone.

                                  Regards

                                  David

                                  Comment


                                  • #18
                                    I can see losing reference if recompiling occurs with out care, unfortunately this can lead to all types of registry issues if care is not taken. This is not uncommon while working on com classes and banging away.

                                    I have a fresh XPsp3 vb6sp6 and updated oleaut32.DLL aka VB6-KB946235 KB924053 KB896559 you have to do this manually and it is a critical update not on Windows update. You have to use the correct update, depends on what OS you are using and target machines use your apps.

                                    We all are having different problems using the same code and VB. My experience is PAR for VB.

                                    My suggestion, use PB

                                    I recall working in an office switching from VB 5 to 6 - broke the debugger for some 3rd party stuff. Including some plain old DLLs written in C and PB.

                                    If you want to debug PB and VB and or any mixed stuff ...use OutputDebugString You have to get the debugger program from the Win SDK, it is the only way to debug with mixed tools.

                                    I will get the info together and post it in the source code, how to use OutputDebugString and sample code. I use it all the time, it beats having 2 IDEs open. Just an app and a console if you will.

                                    It would be a work around for debugging and or a better approach.

                                    ADDED Note : you need to get the debugger application to use OutputDebugString in a console window from MS, some IDEs will show OutputDebugString lines in their own debug window as if it was a debug print statement.
                                    Last edited by Michael Mayerhoffer; 27 Aug 2008, 04:34 PM.
                                    A dozen what.

                                    Comment


                                    • #19
                                      Michael,

                                      I can see your point but my hopes for this version of pb was to release alot of my standard dll's as com dll's. If it isnt going to work that well or if there are alot of hoops to go through it doesnt seem worth it. Also, the documentation states that COM can be used with all languages that support COM (IDISPATCH/IUNKNOWN). If it works only with PB then it doesnt truly support COM does it?
                                      Sr. Software Development Engineer and Sr. Information Security Analyst,
                                      CEH, Digital Forensic Examiner

                                      Comment


                                      • #20
                                        it supports com VB is the issue.

                                        I think another problem, is once you find out that there is issues using VB apps on Vista and who knows about "7"

                                        MS really stuck it to VB users, if I was in that boat I would use PB only, again that is just me. PB has stuck to a plan.
                                        Last edited by Michael Mayerhoffer; 27 Aug 2008, 05:30 PM.
                                        A dozen what.

                                        Comment

                                        Working...
                                        X