Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: COM_GetObject()

  1. #1

    COM_GetObject()

    Hi,

    I was looking at the script examples for COM. Amazing! I saw the Excel example. I discovered that when I already have an Excel process running a second Excel process is added.

    Is it possible to add a new command to thinBasic named as COM_GetObject() that looks for an already running identical process and returns the pointer to that running process? That would be a great benefit above the OLE_CreateObject() that every times creates a new object.

    Kind Regards,

    Marcel
    Kind regards<br /><br />Marcel

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: COM_GetObject()

    Ciao Marcel,

    unfortunately COM module is not much developed so far. Usually we add new features when needed or asked.
    I'm sure Roberto will have a look and see if he can dev what you asked. He is in charge of COM module.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3

    Re: COM_GetObject()

    Hello Eros,

    Thanks for the answer. I'll keep looking when the command pops up in the ADD list when a new version is published.

    Regards

    Marcel
    Kind regards<br /><br />Marcel

  4. #4

    Re: COM_GetObject()

    Hi Marcel,

    I'm try to add the COM_GetActiveObject() functions but until now it works only if the previous instance is started by same process that run COM_GetActiveObject().
    So not very useful but always a step forward.

    Regards,
    Roberto
    http://www.thinbasic.com

  5. #5

    Re: COM_GetObject()

    Hello Roberto,

    Thanks for acting so quickly.

    It is indeed a step in the right direction. I knew this command from another language. GetObject(ObjectName as string,ProgramId as string) When the OLE object doesn't exist it is created otherwise the object pointer is returned.

    oWord = GetObject("","word.application")
    oWord.Documents.Add
    
    I use it very often for working with Word, Excel or Outlook.

    Regards,

    Marcel

    Kind regards<br /><br />Marcel

  6. #6

    Re: COM_GetObject()

    Marcel,

    thanks for the info.
    The COM_GetObject() is already developed but it doesn't work as you pointed out, I'll check ASAP.

    Bye,
    Roberto
    http://www.thinbasic.com

  7. #7

    Re: COM_GetObject()

    Roberto,

    VB's GetObject calls the API function GetActiveObject, and VB's CreateObject calls the API function CoCreateInstance.

    You can use GetActiveObject to retrieve a pointer to a running object that has been registered with OLE calling the RegisterActiveObject API function, as Office applications do, but not to an unregistered running object. If multiple instances of, for example Excel, are running, GetActiveObject attaches to the instance that is launched first. If you then close the first instance, another call to GetActiveObject attaches to the second instance that was launched, and so forth.

  8. #8

    Re: COM_GetObject()

    Hi José,

    thanks for your clarifications.
    Into the thinbasic_com.dll module (that is written in C) I've implemented the COM_GetActiveObject() as a call to the GetActiveObject() API that seems work successfully, I mean it detect a if Excel is already started and if true it returns an handle to it. Probem begins (invalid handle into GetIDsOfNames) if I use the handle to set some object's properties as showed in the following script example:

    [code=thinbasic]uses "COM"

    %NUMBER_OF_PARAMETERS = 3

    dim pXlApp, pXlBooks, pXlSheet AS DWORD
    dim vParam(%NUMBER_OF_PARAMETERS), vRetVal AS VARIANT
    dim hr, i, n AS LONG

    pXlApp = COM_GetActiveObject("Excel.Application")

    IF pXlApp = 0 THEN
    pXlApp = COM_CreateObject("Excel.Application")
    msgbox 0, "COM_CreateObject() returned " + pXlApp
    else
    msgbox 0, "COM_GetActiveObject() returned " + pXlApp
    end if

    IF COM_Succeeded(hr) THEN
    for i = 1 to %NUMBER_OF_PARAMETERS
    COM_VariantInit(vParam(i))
    next
    vParam(1) = 1
    if COM_Succeeded(COM_Execute(pXlApp, "Visible", %TB_DISPATCH_PROPERTYPUT, 1, vParam(1), 0)) then
    msgbox 0, "Now Excel should be visible!"
    COM_VariantInit(vRetVal)
    if COM_Succeeded(COM_Execute(pXlApp, "Workbooks", %TB_DISPATCH_PROPERTYGET, 0, 0, vRetVal)) then
    pXlBooks = vRetVal
    if COM_Succeeded(COM_Execute(pXlBooks, "Add", %TB_DISPATCH_METHOD, 0, 0, 0)) then
    msgbox 0, "A new workbook should be added!"
    else
    msgbox 0, "Add workbook fails"
    end if
    if COM_Succeeded(COM_Execute(pXlApp, "ActiveSheet", %TB_DISPATCH_PROPERTYGET, 0, 0, vRetVal)) then
    pXlSheet = vRetVal
    vParam(1) = "ThinBASIC"
    if COM_Succeeded(COM_Execute(pXlSheet, "Name", %TB_DISPATCH_PROPERTYPUT, 1, vParam(1), 0)) then
    msgbox 0, "ActiveSheet name should be changed!"
    for i = 1 to 20
    for n = 1 to 5
    vParam(3) = i
    vParam(2) = n
    vParam(1) = "Hello"
    COM_Execute(pXlSheet, "Cells", %TB_DISPATCH_PROPERTYPUT, %NUMBER_OF_PARAMETERS, vParam(1), 0)
    next
    next
    if COM_Succeeded(COM_Execute(pXlApp, "Quit", %TB_DISPATCH_METHOD, 0, 0, 0)) then
    msgbox 0, "Great!"
    end if
    end if
    end if
    end if
    else
    msgbox 0, "Visible fails"
    end if
    END IF

    if isfalse(COM_Succeeded(COM_Release(pXlSheet))) then
    msgbox 0, "ActiveSheet release fails"
    end if
    if isfalse(COM_Succeeded(COM_Release(pXlBooks))) then
    msgbox 0, "Workbooks release fails"
    end if
    if isfalse(COM_Succeeded(COM_Release(pXlApp))) then
    msgbox 0, "Excel application release fails"
    end if[/code]

    Regards,
    Roberto
    Attached Files Attached Files
    http://www.thinbasic.com

  9. #9
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: COM_GetObject()

    This is the error message I get.
    Attached Images Attached Images
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  10. #10

    Re: COM_GetObject()

    Yes, this is the error.
    I changed the script in order to test if this is due to different process but I got same error.

    [code=thinbasic]uses "COM"

    %NUMBER_OF_PARAMETERS = 3

    dim pXlApp, pXlApp1, pXlBooks, pXlSheet AS DWORD
    dim vParam(%NUMBER_OF_PARAMETERS), vRetVal AS VARIANT
    dim hr, i, n AS LONG

    pXlApp1 = COM_CreateObject("Excel.Application")

    pXlApp = COM_GetActiveObject("Excel.Application")

    if pXlApp = pXlApp1 then
    msgbox 0, "Same handle" + $tab + pXlApp + $tab + pXlApp1
    else
    msgbox 0, "Different handle" + $tab + pXlApp + $tab + pXlApp1
    end if

    IF COM_Succeeded(hr) THEN
    for i = 1 to %NUMBER_OF_PARAMETERS
    COM_VariantInit(vParam(i))
    next
    vParam(1) = 1
    if COM_Succeeded(COM_Execute(pXlApp, "Visible", %TB_DISPATCH_PROPERTYPUT, 1, vParam(1), 0)) then
    msgbox 0, "Now Excel should be visible!"
    COM_VariantInit(vRetVal)
    if COM_Succeeded(COM_Execute(pXlApp, "Workbooks", %TB_DISPATCH_PROPERTYGET, 0, 0, vRetVal)) then
    pXlBooks = vRetVal
    if COM_Succeeded(COM_Execute(pXlBooks, "Add", %TB_DISPATCH_METHOD, 0, 0, 0)) then
    msgbox 0, "A new workbook should be added!"
    else
    msgbox 0, "Add workbook fails"
    end if
    if COM_Succeeded(COM_Execute(pXlApp, "ActiveSheet", %TB_DISPATCH_PROPERTYGET, 0, 0, vRetVal)) then
    pXlSheet = vRetVal
    vParam(1) = "ThinBASIC"
    if COM_Succeeded(COM_Execute(pXlSheet, "Name", %TB_DISPATCH_PROPERTYPUT, 1, vParam(1), 0)) then
    msgbox 0, "ActiveSheet name should be changed!"
    for i = 1 to 20
    for n = 1 to 5
    vParam(3) = i
    vParam(2) = n
    vParam(1) = "Hello"
    COM_Execute(pXlSheet, "Cells", %TB_DISPATCH_PROPERTYPUT, %NUMBER_OF_PARAMETERS, vParam(1), 0)
    next
    next
    if COM_Succeeded(COM_Execute(pXlApp, "Quit", %TB_DISPATCH_METHOD, 0, 0, 0)) then
    msgbox 0, "Great!"
    end if
    end if
    end if
    end if
    else
    msgbox 0, "Visible fails"
    end if
    END IF

    if isfalse(COM_Succeeded(COM_Release(pXlSheet))) then
    msgbox 0, "ActiveSheet release fails"
    end if
    if isfalse(COM_Succeeded(COM_Release(pXlBooks))) then
    msgbox 0, "Workbooks release fails"
    end if
    if isfalse(COM_Succeeded(COM_Release(pXlApp))) then
    msgbox 0, "Excel application release fails"
    end if[/code]
    http://www.thinbasic.com

Page 1 of 3 123 LastLast

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •