Announcement

Collapse
No announcement yet.

Creating COM from Scratch (Complete Confusion)

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

  • Creating COM from Scratch (Complete Confusion)

    Does ANYONE have a well-commented example of COM from scratch?
    The more I read, the more confused, and trying to find the "hidden" parts that I do not understand, and the more I read, the more confused I get.

    To start, I just want to write and learn from a basic example of
    1. Write a "COM" dll
    2. Use it with VB, or some other programming language


    Basics and basics ONLY (cause I think I am in over my head in documentation)

    A simple example, that does NOT rely on a pre-existing "COM" object, nor an all PB (aka, I need PB for the DLL, but need to see how it is accessed from VB) sort of thing, without over-rides, and all the extras

    Just something as simple as....do this, comment why you do this, and I can figure it out from there....

    Sorry if I sound needy or despondent, or just totally confused, but to tell the truth....I really do NOT get how to create and then use COM, and a simple example would really open my eyes
    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? "

  • #2


    Note that the small assembler code in the example is to use an intrinsic FPU function, so you could replace with pure PB trig functions to do the same thing.

    To get unique GUIDS, use the right click menu and select to insert a GUID.
    Be sure to use the AS COM (or as appropriate AS EVENT) declaration for the class if you intend to publish it. Here's a revision of the order of appearance of the CLASS elements as shown in the PB Win Help. Maybe in a while an expanded template, but the reference link is fairly well commented. Probably best to see the first 2 examples.

    Code:
    CLASS name  [$GUID]  [AS COM | AS EVENT]
         Class Method  code blocks...
         INSTANCE ClassName AS  STRING
         INTERFACE name $GUID   [AS EVENT]
             INHERIT IUNKNOWN
             Method and Property  code blocks...
          END INTERFACE
          EVENT SOURCE interface-name
    END CLASS
    Rick Angell

    Comment


    • #3
      Ms

      The PBWin Help file has more than a few illustrations showing typical coding of CLASSes. What the following code will try to illustrate is the typical skeleton for a COM DLL that can be published and registered. The link in the foregoing post shows working code for a publishable and registrable COM DLL. Some things to be aware of are the few datatypes compliant COM classes do not support. Again see the link in the last post for more notes on that subject.

      Code:
      'start with standard PBWin template for DLL's
      #COMPILE DLL
      #DIM ALL
      
      %USEMACROS = 1
      #INCLUDE "Win32API.inc"
      
      GLOBAL ghInstance AS DWORD
      
      'GUID equates, if you want them fixed values, must be outside of a function or class
      'You can get a unique "GUID(number)" inserted from the right click menu (PB IDE's)
      $MyClassGUID        =   GUID$("{BF1FD6B3-6151-45BC-9173-CDD0BA08C305}")
      $MyFirstInterface   =   GUID$("{F235C6C8-7CD4-420F-8C0D-BDD5841E70BD}")
      
      '-------------------------------------------------------------------------------
      ' Main DLL entry point called by Windows...
      '
      FUNCTION LIBMAIN (BYVAL hInstance   AS LONG, _
                        BYVAL fwdReason   AS LONG, _
                        BYVAL lpvReserved AS LONG) AS LONG
      
          SELECT CASE fwdReason
      
          CASE %DLL_PROCESS_ATTACH
              'Indicates that the DLL is being loaded by another process (a DLL
              'or EXE is loading the DLL).  DLLs can use this opportunity to
              'initialize any instance or global data, such as arrays.
      
              ghInstance = hInstance
      
              FUNCTION = 1   'success!
      
              'FUNCTION = 0   'failure!  This will prevent the EXE from running.
      
          CASE %DLL_PROCESS_DETACH
              'Indicates that the DLL is being unloaded or detached from the
              'calling application.  DLLs can take this opportunity to clean
              'up all resources for all threads attached and known to the DLL.
      
              FUNCTION = 1   'success!
      
              'FUNCTION = 0   'failure!
      
          CASE %DLL_THREAD_ATTACH
              'Indicates that the DLL is being loaded by a new thread in the
              'calling application.  DLLs can use this opportunity to
              'initialize any thread local storage (TLS).
      
              FUNCTION = 1   'success!
      
              'FUNCTION = 0   'failure!
      
          CASE %DLL_THREAD_DETACH
              'Indicates that the thread is exiting cleanly.  If the DLL has
              'allocated any thread local storage, it should be released.
      
              FUNCTION = 1   'success!
      
              'FUNCTION = 0   'failure!
      
          END SELECT
      
      END FUNCTION
      '
      '**** BUILD YOUR CLASS HERE ****
      'CLASS name  [$GUID]  [AS COM | AS EVENT]
      CLASS MyClass $MyClassGUID AS COM
      '   'NOTE: Any Instance Variable declares need to be first, not per 9.0 help file.
      '   example:
          INSTANCE ClassName AS STRING
          INSTANCE Something AS LONG
      '   ... additional INSTANCE declarations if any
      '
      '   Any Class Method code blocks follow the instance variable section, if any.
      '   example:
          CLASS METHOD Initialize
               ClassName = "MyFirstClass"
               Something = 0
          END METHOD
      '   ... additional CLASS METHOD blocks  added in succession
      '
      '   Then the interface blocks follow
      '   INTERFACE name $GUID  [AS EVENT]
      '       INHERIT IUNKNOWN                  'see help for options
      '       Method and Property code blocks follow the INHERIT statement...
      '  END INTERFACE
      '   example:
          INTERFACE MyFirstInterface $MyFirstInterface
              INHERIT IUNKNOWN
              METHOD Startup()
                  ME.Initialize
              END METHOD
      '       ... additional METHOD blocks
              PROPERTY GET SomeValue() AS LONG
                  PROPERTY = Something
              END PROPERTY
              ' if a corresponding PROPERTY SET is required, then it MUST immediately
              ' follow the corresponding PROPERTY GET block
              PROPERTY SET SomeValue(BYVAL x AS LONG)
                  Somthing = x
              END PROPERTY
      '       ... addtional PROPERTY blocks
      'add any EVENT SOURCE declaration if you have also coded any corresponding EVENT class(es)
      'see the EVENT statement page in the PB Help File for an example of an Event class and the
      'EVENT SOURCE statement in a basic class such as this.  That help also illustrates how to
      'instatiate the events and terminate them with EVENTS FROM and EVENTS END statements.
      '  EVENT SOURCE interface-name
          END INTERFACE
      '
      '   ... Additional INTERFACE blocks, if any
      END CLASS
      '
      '   ... Additional CLASS blocks if any.
      Rick Angell

      Comment

      Working...
      X