this code was originally in the thread "dde client - how to simply send a message to
another app" at http://www.powerbasic.com/support/pb...ead.php?t=3762
------------------
another app" at http://www.powerbasic.com/support/pb...ead.php?t=3762
Code:
' a very basic example connecting to pegasus mail with dde and sending a test message type security_quality_of_service length as dword impersonationlevel as long contexttrackingmode as byte effectiveonly as byte end type type convcontext cb as dword wflags as dword ' yes, this is supposed to be a dword wcountryid as dword ' yes, this is supposed to be a dword icodepage as long dwlangid as dword dwsecurity as dword qos as security_quality_of_service end type type convinfo cb as dword huser as dword hconvpartner as long hszsvcpartner as long hszservicereq as long hsztopic as dword hszitem as dword wfmt as dword wtype as dword wstatus as dword wconvst as dword wlasterror as dword hconvlist as dword convctxt as convcontext hwnd as dword hwndpartner as long end type %cf_text = 1 %cbf_fail_selfconnections = &h1000 %cbf_fail_connections = &h2000 %cbf_fail_advises = &h4000 %cbf_fail_executes = &h8000 %cbf_fail_pokes = &h10000 %cbf_fail_requests = &h20000 %cbf_fail_allsvrxactions = &h3f000 %cbf_skip_connect_confirms = &h40000 %cbf_skip_registrations = &h80000 %cbf_skip_unregistrations = &h100000 %cbf_skip_disconnects = &h200000 %cbf_skip_allnotifications = &h3c0000 ' application command flags %appcmd_clientonly = &h10& %appcmd_filterinits = &h20& %appcmd_mask = &hff0& ' application classification flags %appclass_standard = &h0& %appclass_mask = &hf& %dmlerr_no_error = 0 ' must be 0 %dmlerr_first = &h4000 %dmlerr_advacktimeout = &h4000 %dmlerr_busy = &h4001 %dmlerr_dataacktimeout = &h4002 %dmlerr_dll_not_initialized = &h4003 %dmlerr_dll_usage = &h4004 %dmlerr_execacktimeout = &h4005 %dmlerr_invalidparameter = &h4006 %dmlerr_low_memory = &h4007 %dmlerr_memory_error = &h4008 %dmlerr_notprocessed = &h4009 %dmlerr_no_conv_established = &h400a %dmlerr_pokeacktimeout = &h400b %dmlerr_postmsg_failed = &h400c %dmlerr_reentrancy = &h400d %dmlerr_server_died = &h400e %dmlerr_sys_error = &h400f %dmlerr_unadvacktimeout = &h4010 %dmlerr_unfound_queue_id = &h4011 %dmlerr_last = &h4011 %dde_fack = &h8000 %dde_fbusy = &h4000 %dde_fdeferupd = &h4000 %dde_fackreq = &h8000 %dde_frelease = &h2000 %dde_frequested = &h1000 %dde_fappstatus = &hff %dde_fnotprocessed= &h0 %dde_fackreserved = not (%dde_fack or %dde_fbusy or %dde_fappstatus) %dde_fadvreserved = not (%dde_fackreq or %dde_fdeferupd) %dde_fdatreserved = not (%dde_fackreq or %dde_frelease or %dde_frequested) %dde_fpokreserved = not (%dde_frelease) %mh_create = 1 %mh_keep = 2 %mh_delete = 3 %mh_cleanup = 4 %max_monitors = 4 ' transaction types %xtypf_noblock = &h2??? ' cbr_block will not work %xtypf_nodata = &h4??? ' dde_fdeferupd %xtypf_ackreq = &h8??? ' dde_fackreq %xclass_mask = &hfc00??? %xclass_bool = &h1000??? %xclass_data = &h2000??? %xclass_flags = &h4000??? %xclass_notification = &h8000??? %xtyp_error = &h0 or %xclass_notification or %xtypf_noblock %xtyp_advdata = &h10 or %xclass_flags %xtyp_advreq = &h20 or %xclass_data or %xtypf_noblock %xtyp_advstart = &h30 or %xclass_bool %xtyp_advstop = &h40 or %xclass_notification %xtyp_execute = &h50 or %xclass_flags %xtyp_connect = &h60 or %xclass_bool or %xtypf_noblock %xtyp_connect_confirm = &h70 or %xclass_notification or %xtypf_noblock %xtyp_xact_complete = &h80 or %xclass_notification %xtyp_poke = &h90 or %xclass_flags %xtyp_register = &ha0 or %xclass_notification or %xtypf_noblock %xtyp_request = &hb0 or %xclass_data %xtyp_disconnect = &hc0 or %xclass_notification or %xtypf_noblock %xtyp_unregister = &hd0 or %xclass_notification or %xtypf_noblock %xtyp_wildconnect = &he0 or %xclass_data or %xtypf_noblock %xtyp_mask = &hf0 %xtyp_shift = 4 ' shift to turn xtyp_ into an index %appclass_monitor = &h1& %xtyp_monitor = &hf0 or %xclass_notification or %xtypf_noblock ' callback filter flags for use with monitor apps - 0 implies no monitor callbacks %mf_hsz_info = &h1000000 %mf_sendmsgs = &h2000000 %mf_postmsgs = &h4000000 %mf_callbacks = &h8000000 %mf_errors = &h10000000 %mf_links = &h20000000 %mf_conv = &h40000000 %mf_mask = &hff000000 %cp_winansi = 1004 ' default codepage for windows old dde convs. %cp_winunicode = 1200 %true = 1 %false = 0 declare function ddeabandontransaction lib "user32.dll" alias "ddeabandontransaction" (byval idinst as long, byval hconv as long, byval idtransaction as long) as long declare function ddeaccessdata lib "user32.dll" alias "ddeaccessdata" (byval hdata as long, pcbdatasize as long) as long declare function ddeadddata lib "user32.dll" alias "ddeadddata" (byval hdata as long, psrc as byte, byval cb as long, byval cboff as long) as long declare function ddeclienttransaction lib "user32.dll" alias "ddeclienttransaction" ( byval pdata as byte ptr, byval cbdata as long, byval hconv as long, byval hszitem as long, byval wfmt as long, byval wtype as long, byval dwtimeout as long, _ pdwresult as long) as long declare function ddecmpstringhandles lib "user32.dll" alias "ddecmpstringhandles" (byval hsz1 as long, byval hsz2 as long) as long declare function ddeconnect lib "user32.dll" alias "ddeconnect" (byval idinst as long, byval hszservice as long, byval hsztopic as long, pcc as convcontext) as long declare function ddeconnectlist lib "user32.dll" alias "ddeconnectlist" (byval idinst as long, byval hszservice as long, byval hsztopic as long, byval hconvlist as long, pcc as convcontext) as long declare function ddecreatedatahandle lib "user32.dll" alias "ddecreatedatahandle" (byval idinst as long, psrc as byte, byval cb as long, byval cboff as long, byval hszitem as long, byval wfmt as long, byval afcmd as long) as long declare function ddecreatestringhandle lib "user32.dll" alias "ddecreatestringhandlea" (byval idinst as long, psz as asciiz, byval icodepage as long) as long declare function ddedisconnect lib "user32.dll" alias "ddedisconnect" (byval hconv as long) as long declare function ddedisconnectlist lib "user32.dll" alias "ddedisconnectlist" (byval hconvlist as long) as long declare function ddeenablecallback lib "user32.dll" alias "ddeenablecallback" (byval idinst as long, byval hconv as long, byval wcmd as long) as long declare function ddefreedatahandle lib "user32.dll" alias "ddefreedatahandle" (byval hdata as long) as long declare function ddefreestringhandle lib "user32.dll" alias "ddefreestringhandle" (byval idinst as long, byval hsz as long) as long declare function ddegetdata lib "user32.dll" alias "ddegetdata" (byval hdata as long, pdst as byte, byval cbmax as long, byval cboff as long) as long declare function ddegetlasterror lib "user32.dll" alias "ddegetlasterror" (byval idinst as long) as long declare function ddeimpersonateclient lib "user32.dll" alias "ddeimpersonateclient" (byval hconv as long) as long declare function ddeinitialize lib "user32.dll" alias "ddeinitializea" (pidinst as long, byval pfncallback as long, byval afcmd as long, byval ulres as long) as long declare function ddekeepstringhandle lib "user32.dll" alias "ddekeepstringhandle" (byval idinst as long, byval hsz as long) as long declare function ddenameservice lib "user32.dll" alias "ddenameservice" (byval idinst as long, byval hsz1 as long, byval hsz2 as long, byval afcmd as long) as long declare function ddepostadvise lib "user32.dll" alias "ddepostadvise" (byval idinst as long, byval hsztopic as long, byval hszitem as long) as long declare function ddequeryconvinfo lib "user32.dll" alias "ddequeryconvinfo" (byval hconv as long, byval idtransaction as long, pconvinfo as convinfo) as long declare function ddequerynextserver lib "user32.dll" alias "ddequerynextserver" (byval hconvlist as long, byval hconvprev as long) as long declare function ddequerystring lib "user32.dll" alias "ddequerystring" (byval idinst as long, byval hsz as long, psz as asciiz, byval cchmax as long, byval icodepage as long) as long declare function ddereconnect lib "user32.dll" alias "ddereconnect" (byval hconv as long) as long declare function ddesetqualityofservice lib "user32.dll" alias "ddesetqualityofservice" (byval hwndclient as long, pqosnew as security_quality_of_service, pqosprev as security_quality_of_service) as long declare function ddesetuserhandle lib "user32.dll" alias "ddesetuserhandle" (byval hconv as long, byval id as long, byval huser as long) as long declare function ddeunaccessdata lib "user32.dll" alias "ddeunaccessdata" (byval hdata as long) as long declare function ddeuninitialize lib "user32.dll" alias "ddeuninitialize" (byval idinst as long) as long function dde_callbackfunc(byval utype as dword, byval ufmt as long, byval hconv as long, byval hsztopic as long, byval hszitem as long, byval hdata as long, byval dwdata1 as dword, byval dwdata2 as dword) as long 'for monitoring and server apps end function function pbmain() as long local i as long local lid as long local hconvhandle as long local hservice as long local htopicmessage as long local htopictcp as long local hitemmessage as long local hitemtcp as long local lresult as long local tddeconv as convcontext dim scmd(0:10) as string 'since we are client only we decline to receive callbacks if ddeinitialize( lid, codeptr(dde_callbackfunc), %appcmd_clientonly, 0 ) <> 0 then stdout "failed to initialize dde - exiting" exit function end if 'create string handle for the service hservice = ddecreatestringhandle( lid, "winpmail", %cp_winansi ) if hservice = 0 then goto clean_up end if 'create string handle for the topic htopicmessage = ddecreatestringhandle( lid, "message", %cp_winansi ) if htopicmessage = 0 then goto clean_up end if 'connect (start conversation) tddeconv.cb = sizeof(tddeconv) tddeconv.icodepage = %cp_winansi hconvhandle = ddeconnect( lid, hservice, htopicmessage, tddeconv ) if hconvhandle = 0 then goto clean_up end if 'create item string handle for poke request hitemmessage = ddecreatestringhandle( lid, "message", %cp_winansi ) if hitemmessage = 0 then goto clean_up end if scmd(0) = "new: message" + chr$(0) scmd(1) = "defaults: y" + chr$(0) scmd(2) = "to: florent.heywo[email protected]" + chr$(0) scmd(3) = "subject: a test message" + chr$(0) scmd(4) = "data: here it is" + chr$(0) scmd(5) = "data: one line per %xtyp_poke" + chr$(0) scmd(6) = "data: is the way that pegasus mail" + chr$(0) scmd(7) = "data: processes the message body" + chr$(0) scmd(8) = "data: this seems to work ok" + chr$(0) scmd(9) = "data: as it is now" + chr$(0) scmd(10) = "send" + chr$(0) do lresult = ddeclienttransaction( strptr(scmd(i)), len(scmd(i)), hconvhandle, hitemmessage, %cf_text, %xtyp_poke, 3000, byval 0 ) if lresult then call ddefreedatahandle( lresult ) end if incr i loop while i < ubound(scmd(),1)+1 call ddedisconnect( hconvhandle ): hconvhandle = 0 'create string handle for the topic htopictcp = ddecreatestringhandle( lid, "tcp", %cp_winansi ) if htopictcp = 0 then goto clean_up end if 'create item string handle for poke request hitemtcp = ddecreatestringhandle( lid, "tcp", %cp_winansi ) if hitemmessage = 0 then goto clean_up end if hconvhandle = ddeconnect( lid, hservice, htopictcp, tddeconv ) if hconvhandle = 0 then goto clean_up end if lresult = ddeclienttransaction( strptr(scmd(10)), len(scmd(10)), hconvhandle, hitemtcp, %cf_text, %xtyp_poke, 3000, byval 0 ) if lresult then call ddefreedatahandle( lresult ) end if clean_up: if hconvhandle then call ddedisconnect( hconvhandle ) end if if hitemmessage then call ddefreestringhandle( lid, hitemmessage ) end if if hitemtcp then call ddefreestringhandle( lid, hitemtcp ) end if if htopicmessage then call ddefreestringhandle( lid, htopicmessage ) end if if htopictcp then call ddefreestringhandle( lid, htopictcp ) end if if hservice then call ddefreestringhandle( lid, hservice ) end if call ddeuninitialize( lid ) print "finished" waitkey$ end function
Comment