Scripts/DDEClientClass

From J Wiki
Jump to navigation Jump to search

Download this script: File:Dmdde.ijs

NB. DDE Client interface class
NB.
NB. Use Windows API DDEML for DDE Client access
NB.
NB.   r=. ddereq__ob data
NB.   r=. ddeex__ob  data
NB.	
NB. ddereq returns the text returned by the successful DDE call.
NB. ddeex returns the return code from the ddeex call.
NB.
NB.   ddecleanup__ob''
NB.
NB. terminates and cleans up any DDE resources.
NB.
NB. Example:
NB.
NB. test=: 3 : 0
NB. dd=.conew 'dmdde'
NB. 'iexplore' create__dd 'WWW_OpenURL'
NB. ddeex__dd '"http://www.jsoftware.com"'
NB. destroy__dd ''
NB. )

require 'dll convert winapi strings'

coclass'dmdde'

DdeInitialize=: 2&{.@:('user32 DdeInitializeA i *i i i i'&cd)
DdeCreateStringHandle=: 0&{@:('user32 DdeCreateStringHandleA i i *c i'&cd)
DdeFreeStringHandle=: ('user32 DdeFreeStringHandle i i i'&cd)
DdeQueryString=: ('user32 DdeQueryStringA i i i *c i i'&cd)
DdeKeepStringHandle=: ('user32 DdeKeepStringHandle i i i'&cd)
DdeConnect=: 0&{@:('user32 DdeConnect i i i i i'&cd)
DdeDisconnect=: ('user32 DdeDisconnect i i'&cd)
DdeGetLastError=: 0&{@:('user32 DdeGetLastError i i'&cd)
DdeUninitialize=: ('user32 DdeUninitialize i i'&cd)
DdeClientTransaction=: 0&{@:('user32 DdeClientTransaction i i i i i i i i *i'&cd)
DdeClientTransactiona=: 0&{@:('user32 DdeClientTransaction i *c i i i i i i *i'&cd)
DdeAccessData=: ('user32 DdeAccessData i i *i'&cd)
DdeUnaccessData=: ('user32 DdeUnaccessData i i'&cd)

CP_WINANSI=:1004    NB. /* default codepage for windows & old DDE convs. */
XCLASS_DATA=: (dfh'2000')
XCLASS_FLAGS=: (dfh'4000')
XTYP_REQUEST=: ((dfh'00B0')+XCLASS_DATA)
XTYP_EXECUTE=: ((dfh'0050')+XCLASS_FLAGS)
CF_TEXT=: 1
DMLERR_NO_ERROR=: 0
TIMEOUT_ASYNC=: _1

DDECB=: cdcb 8 NB. address of C routine with 8 integer arguments

DDEINST_dmdde_=:0

NB. =========================================================
NB.*ddesetup v setup dde
NB. form: ddesetup ''
NB. returns handle
ddesetup=: 3 : 0
'r inst'=.DdeInitialize (,0);DDECB;0;0
if. r~:DMLERR_NO_ERROR do. throw. r end.
DDEINST_dmdde_=:0{inst
)

NB. =========================================================
NB.*create v setup conversation
NB. form: service create topic
NB. returns 0
create=:4 : 0
if. 0=DDEINST do. ddesetup'' end.
DDECONV=:0
r=.>DdeConnect DDEINST;(DDEINST ddestringh x.);(DDEINST ddestringh y.);0
if. r=0 do. throw. DdeGetLastError DDEINST end.
DDECONV=:0{r
DDESERVICE=:x.
DDETOPIC=:y.
0
)

NB. =========================================================
NB.*destroy v cleanup dde, releasing the converation
NB. form: destroy ''
NB. returns ''
destroy=: 3 : 0
DdeDisconnect DDECONV
codestroy''
)

NB. =========================================================
NB.*ddecleanup v cleanup dde, releasing any converation(s) and data
NB. form: ddecleanup ''
NB. returns ''
ddecleanup=:3 : 0
destroy''
DdeUninitialize DDEINST
DDEINST_dmdde_=:0
)

NB. =========================================================
NB.*ddereq v initiate dde request with data
NB. form: ddereq data
NB. returns text vector response
ddereq=: 3 : 0
r=.>DdeClientTransaction 0;0;DDECONV;(DDEINST ddestringh y.);CF_TEXT;XTYP_REQUEST;1000;<<0
if. r=0 do. throw. DdeGetLastError DDEINST end.
hret=.0{r
'ra r rs'=.DdeAccessData hret;,0
if. r=0 do. throw. DdeGetLastError DDEINST end.
rv=.}:memr ra,0,rs
DdeUnaccessData hret
rv
)

NB. =========================================================
NB.*ddeex v initiate dde execute with data
NB. form: ddeex data
NB. returns response from DdeClientTransaction
ddeex=: 3 : 0
r=.>DdeClientTransactiona (y.,0{a.);(>:#y.);DDECONV;0;0;XTYP_EXECUTE;((DDESERVICE-:'iexplore'){1000,TIMEOUT_ASYNC);<<0
if. r=0 do. throw. DdeGetLastError DDEINST end.
r
)

NB. =========================================================
NB.*ddestringh v create ddeml string handle
NB. form: inst ddestringh data
NB. returns handle
ddestringh=: 4 : 0
r=.>DdeCreateStringHandle x.;(y.,0{a.);CP_WINANSI
if. r=0 do. throw. DdeGetLastError x. end.
r
)

NB. =========================================================
NB.*cdcallback v dll callback for ddeml
NB. form: cdcallback v
NB. returns 0
cdcallback=: 3 : 0
0
)