Addons/general/pcall/test pcall

From J Wiki
Jump to navigation Jump to search
   require 'dll convert general/pcall'

   VAR1=: 'VtR1R2R3Valu'
   b=: #~ [: $&1 0 #
   h=: ([: ;:^:_1"1 [: <"1 hfd)@(([: , _4 (_2&(3!:4))@|.\ ])^:(2=3!:0))
   mi=: [: {.@memr ,&(0 1,JINT)
   mc=: ,&0@] memr@, ,&JCHAR@[

   'CLSCTX_INPROC_SERVER CLSCTX_LOCAL_SERVER'=: 16b0001 16b0004
   CTX=: CLSCTX_INPROC_SERVER+CLSCTX_LOCAL_SERVER

   IUknown=: ;:'QueryInterface AddRef Release'
   IDispatch=: IUknown,;:'GetTypeInfoCount GetTypeInfo GetIDsOfNames Invoke'
   IJ=: IDispatch,;:'Do Show Log IsBusy Break Quit Get Set GetM SetM ErrorText ErrorTextM Clear Transpose ErrorTextB GetB SetB DoR'

   h clsid=. 2{::'ole32 CLSIDFromProgID i *w *c'cd 'JDLLServer';'WWWWXXYYZZZZZZZZ'
EA05EB21 B31ACF11 A2AC8FF7 0874C460
   h iid=. 2{::'ole32 CLSIDFromString i *w *c'cd '{21EB05EC-1AB3-11CF-A2AC-8FF70874C460}';'WWWWXXYYZZZZZZZZ'
EC05EB21 B31ACF11 A2AC8FF7 0874C460

   h ip=. {._1{::'ole32 CoCreateInstance i *c i i *c *i'cd clsid;0;CTX;iid;,_2
10B4018
   h vt=. mi ip                        NB. vtable of J interface pointer
1012D380

   h pDoR=. mi vt+4*IJ i.<'DoR'        NB. funcion pointer from vtable
100512D0
     res=. i.4%~#VAR1                  NB. alloc VARIANT for BSTR
     'cp4 i i i *w *i *i' pcall pDoR;ip;'i.3 4';res;,_2
+-+---------+--------+-----+----------+-+
|0|268767952|17514520|i.3 4|8 1 315268|0|
+-+---------+--------+-----+----------+-+
   ] len=. mi _4+{:res                 NB. BSTR length
66
   b val=. len mc {:res                NB. BSTR string value
0 1  2  3
4 5  6  7
8 9 10 11


   'oleaut32 VariantClear > i *i' cd <res  NB. free BSTR, 'cause we own it
0

   h pRelease=. mi vt+4*IJ i.<'Release'  NB. Release from IUnknown interface
10051060
   'cp1 > i i i' pcall pRelease;ip    NB. destroys J object; RefCount=0
0