IT-Consultant: José Roca (PBWIN 10+/PBCC 6+) (Archive only) > Discussion

IRunningObjectTable Interface

<< < (2/2)

José Roca:
I have modified the above posted functions to return an IDispatch pointer.


--- Code: ---' ========================================================================================
' If the requested object is in an EXE (out-of-process server), such Office applications,
' and it is running and registered in the Running Object Table (ROT), AfxGetCom will
' return a pointer to its interface. AfxAnyCom will first try to use an existing, running
' application if available, or it will create a new instance if not.
' Be aware that AfxGetCom can fail under if Office is running but not registered in the ROT.
' When an Office application starts, it does not immediately register its running objects.
' This optimizes the application's startup process. Instead of registering at startup, an
' Office application registers its running objects in the ROT once it loses focus. Therefore,
' if you attempt to use GetObject or GetActiveObject to attach to a running instance of an
' Office application before the application has lost focus, you might receive an error.
' See: https://support.microsoft.com/en-us/help/238610/getobject-or-getactiveobject-cannot-find-a-running-office-application
' ========================================================================================
PRIVATE FUNCTION AfxGetCom OVERLOAD (BYREF wszProgID AS CONST WSTRING) AS IDispatch PTR
   DIM classID AS CLSID, pUnk AS IUnknown PTR, pDisp AS IDispatch PTR
   CLSIDFromProgID(wszProgID, @classID)
   IF IsEqualGuid(@classID, @IID_NULL) THEN RETURN NULL
   GetActiveObject(@classID, NULL, @pUnk)
   IF pUnk THEN
      pUnk->lpVtbl->QueryInterface(pUnk, @IID_IDispatch, @pDisp)
      pUnk->lpVtbl->Release(pUnk)
   END IF
   RETURN pDisp
END FUNCTION
' ========================================================================================
' ========================================================================================
PRIVATE FUNCTION AfxGetCom OVERLOAD (BYREF classID AS CONST CLSID) AS IDispatch PTR
   DIM pUnk AS IUnknown PTR, pDisp AS IDispatch PTR
   GetActiveObject(@classID, NULL, @pUnk)
   IF pUnk THEN
      pUnk->lpVtbl->QueryInterface(pUnk, @IID_IDispatch, @pDisp)
      pUnk->lpVtbl->Release(pUnk)
   END IF
   RETURN pDisp
END FUNCTION
' ========================================================================================
' ========================================================================================
PRIVATE FUNCTION AfxAnyCom OVERLOAD (BYREF wszProgID AS CONST WSTRING) AS IDispatch PTR
   DIM classID AS CLSID, pUnk AS IUnknown PTR, pDisp AS IDispatch PTR
   CLSIDFromProgID(wszProgID, @classID)
   IF IsEqualGuid(@classID, @IID_NULL) THEN RETURN NULL
   ' // Check if there is an instance already running
   IF GetActiveObject(@classID, NULL, @pUnk) = S_OK THEN
      pUnk->lpVtbl->QueryInterface(pUnk, @IID_IDispatch, @pDisp)
      pUnk->lpVtbl->Release(pUnk)
      RETURN pDisp
   END IF
   ' // Otherwise, create a new instance
   CoCreateInstance(@classID, NULL, CLSCTX_INPROC_SERVER, @IID_IUnknown, @pUnk)
   IF pUnk THEN
      pUnk->lpVtbl->QueryInterface(pUnk, @IID_IDispatch, @pDisp)
      pUnk->lpVtbl->Release(pUnk)
   END IF
   RETURN pDisp
END FUNCTION
' ========================================================================================
' ========================================================================================
PRIVATE FUNCTION AfxAnyCom OVERLOAD (BYREF classID AS CONST CLSID) AS IDispatch PTR
   DIM pUnk AS IUnknown PTR, pDisp AS IDispatch PTR
   ' // Check if there is an instance already running
   IF GetActiveObject(@classID, NULL, @pUnk) = S_OK THEN
      pUnk->lpVtbl->QueryInterface(pUnk, @IID_IDispatch, @pDisp)
      pUnk->lpVtbl->Release(pUnk)
      RETURN pDisp
   END IF
   ' // Otherwise, create a new instance
   CoCreateInstance(@classID, NULL, CLSCTX_INPROC_SERVER, @IID_IUnknown, @pUnk)
   IF pUnk THEN
      pUnk->lpVtbl->QueryInterface(pUnk, @IID_IDispatch, @pDisp)
      pUnk->lpVtbl->Release(pUnk)
   END IF
   RETURN pDisp
END FUNCTION
' ========================================================================================

--- End code ---

Frederick J. Harris:
About FreeBASIC...

If I install Pauls WinFBE I don't need to install the compilers first?  From the GitHub description of WinFBE....

The WinFBE Suite package (attached to each release as a compressed RAR file), is an all-in-one package including the latest WinFBE editor, FreeBASIC compiler (32 and 64 bit compilers), Jose Roca's WinFBX library, several Help files and all necessary Include files.

But furthyer on down in Requirements it states...

Requirements:

·   Windows Vista or later operating system. Windows XP is not supported because many newer API's are used in the source code.
·   FreeBASIC Compiler http://www.freebasic.net/

I downloaded FreeBASIC-1.06.0-win32.exe and that is about 11 megabytes, and Paul's WinFBE-master.zip and that's about 18 megabytes.  So the latter includes the compilers?

I'm using an ancient version of Office and Excel myself - Office 2000.  It still works fine.  Where I worked before I retired they had front row seats on the Microsoft Upgrade Bandwagon, so I had access to the latest of everything.  But on my personal computers which I used exclusively for development, I just used the old version of Office.  There weren't very many issues with that - testament to the power of COM.  So what I'm saying is that its possible - though in my mind unlikely, that the IUnknown** returned by GetActiveObject() might work in IDispatch calls.  It absolutely didn't though in the version I have, like I said.

Just as soon as I get a better internet connection I'm hoping to get the latest versions of Excel and Word.  It isn't that I need them for myself, but I still hope to support all the work I did over the years for the Pennsylvania Bureau of Forestry.  All my apps interacted heavily with Word and Excel.

Right now I only have a cellular internet connection and am limited to 15 gigs per month.  And I'm happy to have it, as for the first couple months I lived up here I didn't have any internet at all.  I built a log cabin way up in the mountains in a remote area and its amazing I have a cellular connection at all.  Most remote areas in the US don't have cellular access.  But I just about have to use an old Windows 7 machine because as soon as I connect a Windows 10 machine to the internet it blows through the monthly allotment like almost immediately what with all the connections it makes to the internet as it scours my personal information for anything saleable and sends it all over the world.
 
Its a winter wonderland up here now (I'm at 7500 feet elevation) and the ground is frozen solid as a rock.  As soon as it thaws I need to dig a 300 foot trench up to the dirt road above me and string cable down to my cabin.  I need to put an antennae up there where I'll have line of sight to a tower from which I can get a better internet connection.  That's why I'm saying I have a hard time finding time or coding right now - too much to do just staying alive!  But its good for my soul!

José Roca:
If you install the WinFBE suite, you don't need to install anything else. It comes with the latest compilers and tools. Yesterday, version 1.06 of the compilers was released, so I guess that Paul will prepare a new version of the WinFBE suite in a few days, after the needed checks.

> Its a winter wonderland up here now (I'm at 7500 feet elevation) and the ground is frozen solid as a rock.

Just the opposite that me. I live at sea level and Spring will come next Friday (one month before that it used to be, courtesy of climate change), with temperatures of 19º C (66.2º F).

> But its good for my soul!

This is the more important. I still do some programming because I like it and it is good for my brain activity, but mainly as a hobbie.

Paul Squires:

--- Quote from: José Roca on February 19, 2019, 04:52:32 PM ---This is the more important. I still do some programming because I like it and it is good for my brain activity, but mainly as a hobbie.

--- End quote ---

That is exactly my situation as well. I would hate to never program again because it keeps my mind so very active. It is nice building things and feeling that sense of satisfaction from completing a project that others use.

Navigation

[0] Message Index

[*] Previous page

Go to full version