DECLARE DLL allows OBJECT type

Before VFP7, it was very difficult to get an object back from a DECLARE DLL call. I added the “OBJECT” type for VFP7 which allows you to call a DLL which returns a COM object reference. Luckily, most COM objects are created from other COM objects, and very few DLL APIs return COM objects. The VFP CreateObject command (and its brethren CreateObjectEx and NewObject) can create an initial COM object, from which others can be created (e.g. an Excel spreadsheet object can return an Excel cell object).

This code calls. AccessibleObjectFromWindow, which returns a COM object, in 2 different but equivalent ways: one using the OBJECT syntax, and the other using an integer and SYS(3096) to convert the integer into a COM object. Alas, SYS(3096) was also added in VFP7 making it hard to do this in prior versions.

#define IAccGuid "{618736E0-3C3D-11CF-810C-00AA00389B71}"

#define OBJID_CLIENT 0xFFFFFFFC

DECLARE integer CLSIDFromString IN ole32 string , string @

iAccessible=SPACE(16)

?TRANSFORM(CLSIDFromString(STRCONV(IAccGuid,5)+CHR(0),@IAccessible),"@0x")

DECLARE INTEGER AccessibleObjectFromWindow IN oleacc.dll INTEGER hWnd, INTEGER dwObjectID, STRING RIID , OBJECT @ pAcc

oAcc=0

?AccessibleObjectFromWindow(_screen.HWnd,OBJID_CLIENT, IAccessible,@oAcc)

?oAcc.accName

CLEAR DLLS

DECLARE INTEGER AccessibleObjectFromWindow IN oleacc.dll INTEGER hWnd, INTEGER dwObjectID, STRING RIID , integer @ pAcc

pAcc=0

?AccessibleObjectFromWindow(_screen.HWnd,OBJID_CLIENT, IAccessible,@pAcc)

oAcc=SYS(3096,INT(pAcc))

?oAcc.accName

For another example of IAccessible use, see Excel's new gradient Data Bar feature is cool: you can do it too!

You don’t even need to use CreateObject to create an initial COM object: This sample creates an instance of Excel using CoCreateInstance, CLSIDFromProgID, and CLSIDFromString

#define CLSCTX_INPROC_SERVER 0x1

#define CLSCTX_LOCAL_SERVER 0x4

#define CLSCTX_REMOTE_SERVER 0x10

DECLARE integer CoCreateInstance IN ole32 string, integer, integer, string, object @

DECLARE integer CLSIDFromProgID IN ole32 string, string @

DECLARE integer CLSIDFromString IN ole32 string , string @

cClsId=REPLICATE(CHR(0),16)

CLSIDFromProgID(STRCONV("excel.application"+CHR(0),5),@cClsId)

PUBLIC ox

ox=0

iidIDispatch=REPLICATE(CHR(0),16)

CLSIDFromString(STRCONV("{00020400-0000-0000-C000-000000000046}"+CHR(0),5),@iidIDispatch)

?"CoCreate=",CoCreateInstance(cClsId, 0, CLSCTX_LOCAL_SERVER+CLSCTX_INPROC_SERVER, iidIDispatch, @ox)

?ox

ox.Visible=1

ox.Workbooks.Add

ox.Cells(1,1).Value="Hi From VFP"