1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DDE classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dde.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/module.h"
43 #include "wx/msw/private.h"
49 #if defined(__TWIN32__) || defined(__GNUWIN32_OLD__)
50 #include "wx/msw/gnuwin32/extra.h"
53 // some compilers headers don't define this one (mingw32)
54 #ifndef DMLERR_NO_ERROR
55 #define DMLERR_NO_ERROR (0)
57 // this one is also missing from some mingw32 headers, but there is no way
58 // to test for it (I know of) - the test for DMLERR_NO_ERROR works for me,
59 // but is surely not the right thing to do
61 HDDEDATA STDCALL
DdeClientTransaction(LPBYTE pData
,
69 #endif // no DMLERR_NO_ERROR
71 // ----------------------------------------------------------------------------
72 // macros and constants
73 // ----------------------------------------------------------------------------
78 #define _EXPORT _export
82 #define DDE_CP CP_WINUNICODE
84 #define DDE_CP CP_WINANSI
87 #define GetHConv() ((HCONV)m_hConv)
89 // default timeout for DDE operations (5sec)
90 #define DDE_TIMEOUT 5000
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 static wxDDEConnection
*DDEFindConnection(HCONV hConv
);
97 static void DDEDeleteConnection(HCONV hConv
);
98 static wxDDEServer
*DDEFindServer(const wxString
& s
);
100 extern "C" HDDEDATA EXPENTRY _EXPORT
_DDECallback(WORD wType
,
109 // Add topic name to atom table before using in conversations
110 static HSZ
DDEAddAtom(const wxString
& string
);
111 static HSZ
DDEGetAtom(const wxString
& string
);
114 static HSZ
DDEAtomFromString(const wxString
& s
);
115 static wxString
DDEStringFromAtom(HSZ hsz
);
118 static wxString
DDEGetErrorMsg(UINT error
);
119 static void DDELogError(const wxString
& s
, UINT error
= DMLERR_NO_ERROR
);
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 static DWORD DDEIdInst
= 0L;
126 static wxDDEConnection
*DDECurrentlyConnecting
= NULL
;
128 static wxList
wxAtomTable(wxKEY_STRING
);
129 static wxList wxDDEClientObjects
;
130 static wxList wxDDEServerObjects
;
132 char *DDEDefaultIPCBuffer
= NULL
;
133 int DDEDefaultIPCBufferSize
= 0;
134 static bool DDEInitialized
= FALSE
;
136 // ----------------------------------------------------------------------------
138 // ----------------------------------------------------------------------------
140 // A module to allow DDE cleanup without calling these functions
141 // from app.cpp or from the user's application.
143 class wxDDEModule
: public wxModule
147 bool OnInit() { return TRUE
; }
148 void OnExit() { wxDDECleanUp(); }
151 DECLARE_DYNAMIC_CLASS(wxDDEModule
)
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 IMPLEMENT_DYNAMIC_CLASS(wxDDEServer
, wxServerBase
)
159 IMPLEMENT_DYNAMIC_CLASS(wxDDEClient
, wxClientBase
)
160 IMPLEMENT_CLASS(wxDDEConnection
, wxConnectionBase
)
161 IMPLEMENT_DYNAMIC_CLASS(wxDDEModule
, wxModule
)
163 // ============================================================================
165 // ============================================================================
167 // ----------------------------------------------------------------------------
168 // initialization and cleanup
169 // ----------------------------------------------------------------------------
171 extern void wxDDEInitialize()
173 if ( !DDEInitialized
)
175 // Should insert filter flags
176 PFNCALLBACK callback
= (PFNCALLBACK
)
177 MakeProcInstance((FARPROC
)_DDECallback
, wxGetInstance());
178 UINT rc
= DdeInitialize(&DDEIdInst
, callback
, APPCLASS_STANDARD
, 0L);
179 if ( rc
!= DMLERR_NO_ERROR
)
181 DDELogError(_T("Failed to initialize DDE"), rc
);
185 DDEInitialized
= TRUE
;
192 if ( DDEIdInst
!= 0 )
194 DdeUninitialize(DDEIdInst
);
198 delete [] DDEDefaultIPCBuffer
;
201 // ----------------------------------------------------------------------------
202 // functions working with the global connection list(s)
203 // ----------------------------------------------------------------------------
205 // Global find connection
206 static wxDDEConnection
*DDEFindConnection(HCONV hConv
)
208 wxNode
*node
= wxDDEServerObjects
.First();
209 wxDDEConnection
*found
= NULL
;
210 while (node
&& !found
)
212 wxDDEServer
*object
= (wxDDEServer
*)node
->Data();
213 found
= object
->FindConnection((WXHCONV
) hConv
);
219 node
= wxDDEClientObjects
.First();
220 while (node
&& !found
)
222 wxDDEClient
*object
= (wxDDEClient
*)node
->Data();
223 found
= object
->FindConnection((WXHCONV
) hConv
);
229 // Global delete connection
230 static void DDEDeleteConnection(HCONV hConv
)
232 wxNode
*node
= wxDDEServerObjects
.First();
234 while (node
&& !found
)
236 wxDDEServer
*object
= (wxDDEServer
*)node
->Data();
237 found
= object
->DeleteConnection((WXHCONV
) hConv
);
243 node
= wxDDEClientObjects
.First();
244 while (node
&& !found
)
246 wxDDEClient
*object
= (wxDDEClient
*)node
->Data();
247 found
= object
->DeleteConnection((WXHCONV
) hConv
);
252 // Find a server from a service name
253 static wxDDEServer
*DDEFindServer(const wxString
& s
)
255 wxNode
*node
= wxDDEServerObjects
.First();
256 wxDDEServer
*found
= NULL
;
257 while (node
&& !found
)
259 wxDDEServer
*object
= (wxDDEServer
*)node
->Data();
261 if (object
->GetServiceName() == s
)
263 else node
= node
->Next();
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 wxDDEServer::wxDDEServer()
276 wxDDEServerObjects
.Append(this);
279 bool wxDDEServer::Create(const wxString
& server
)
281 m_serviceName
= server
;
283 if ( !DdeNameService(DDEIdInst
, DDEAtomFromString(server
), (HSZ
)NULL
, DNS_REGISTER
) )
285 DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
294 wxDDEServer::~wxDDEServer()
296 if ( !!m_serviceName
)
298 if ( !DdeNameService(DDEIdInst
, DDEAtomFromString(m_serviceName
),
299 (HSZ
)NULL
, DNS_UNREGISTER
) )
301 DDELogError(wxString::Format(_("Failed to unregister DDE server '%s'"),
302 m_serviceName
.c_str()));
306 wxDDEServerObjects
.DeleteObject(this);
308 wxNode
*node
= m_connections
.First();
311 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
312 wxNode
*next
= node
->Next();
313 connection
->OnDisconnect(); // May delete the node implicitly
317 // If any left after this, delete them
318 node
= m_connections
.First();
321 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
322 wxNode
*next
= node
->Next();
328 wxConnectionBase
*wxDDEServer::OnAcceptConnection(const wxString
& /* topic */)
330 return new wxDDEConnection
;
333 wxDDEConnection
*wxDDEServer::FindConnection(WXHCONV conv
)
335 wxNode
*node
= m_connections
.First();
336 wxDDEConnection
*found
= NULL
;
337 while (node
&& !found
)
339 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
340 if (connection
->m_hConv
== conv
)
342 else node
= node
->Next();
347 // Only delete the entry in the map, not the actual connection
348 bool wxDDEServer::DeleteConnection(WXHCONV conv
)
350 wxNode
*node
= m_connections
.First();
352 while (node
&& !found
)
354 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
355 if (connection
->m_hConv
== conv
)
360 else node
= node
->Next();
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 wxDDEClient::wxDDEClient()
373 wxDDEClientObjects
.Append(this);
376 wxDDEClient::~wxDDEClient()
378 wxDDEClientObjects
.DeleteObject(this);
379 wxNode
*node
= m_connections
.First();
382 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
383 delete connection
; // Deletes the node implicitly (see ~wxDDEConnection)
384 node
= m_connections
.First();
388 bool wxDDEClient::ValidHost(const wxString
& /* host */)
393 wxConnectionBase
*wxDDEClient::MakeConnection(const wxString
& WXUNUSED(host
),
394 const wxString
& server
,
395 const wxString
& topic
)
397 HCONV hConv
= DdeConnect(DDEIdInst
, DDEAtomFromString(server
), DDEAtomFromString(topic
),
401 DDELogError(wxString::Format(_("Failed to create connection to server '%s' on topic '%s'"),
402 server
.c_str(), topic
.c_str()));
406 wxDDEConnection
*connection
= (wxDDEConnection
*) OnMakeConnection();
409 connection
->m_hConv
= (WXHCONV
) hConv
;
410 connection
->m_topicName
= topic
;
411 connection
->m_client
= this;
412 m_connections
.Append(connection
);
417 return (wxConnectionBase
*) NULL
;
420 wxConnectionBase
*wxDDEClient::OnMakeConnection()
422 return new wxDDEConnection
;
425 wxDDEConnection
*wxDDEClient::FindConnection(WXHCONV conv
)
427 wxNode
*node
= m_connections
.First();
428 wxDDEConnection
*found
= NULL
;
429 while (node
&& !found
)
431 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
432 if (connection
->m_hConv
== conv
)
434 else node
= node
->Next();
439 // Only delete the entry in the map, not the actual connection
440 bool wxDDEClient::DeleteConnection(WXHCONV conv
)
442 wxNode
*node
= m_connections
.First();
444 while (node
&& !found
)
446 wxDDEConnection
*connection
= (wxDDEConnection
*)node
->Data();
447 if (connection
->m_hConv
== conv
)
452 else node
= node
->Next();
457 // ----------------------------------------------------------------------------
459 // ----------------------------------------------------------------------------
461 wxDDEConnection::wxDDEConnection(char *buffer
, int size
)
465 if (DDEDefaultIPCBuffer
== NULL
)
466 DDEDefaultIPCBuffer
= new char[DDEDefaultIPCBufferSize
];
467 m_bufPtr
= DDEDefaultIPCBuffer
;
468 m_bufSize
= DDEDefaultIPCBufferSize
;
480 m_sendingData
= NULL
;
483 wxDDEConnection::wxDDEConnection()
486 m_sendingData
= NULL
;
489 if (DDEDefaultIPCBuffer
== NULL
)
490 DDEDefaultIPCBuffer
= new char[DDEDefaultIPCBufferSize
];
492 m_bufPtr
= DDEDefaultIPCBuffer
;
493 m_bufSize
= DDEDefaultIPCBufferSize
;
496 wxDDEConnection::~wxDDEConnection()
499 m_server
->GetConnections().DeleteObject(this);
501 m_client
->GetConnections().DeleteObject(this);
504 // Calls that CLIENT can make
505 bool wxDDEConnection::Disconnect()
507 DDEDeleteConnection(GetHConv());
509 bool ok
= DdeDisconnect(GetHConv()) != 0;
512 DDELogError(_T("Failed to disconnect from DDE server gracefully"));
518 bool wxDDEConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
523 size
= wxStrlen(data
) + 1;
526 bool ok
= DdeClientTransaction((LPBYTE
)data
, size
,
535 DDELogError(_T("DDE execute request failed"));
541 char *wxDDEConnection::Request(const wxString
& item
, int *size
, wxIPCFormat format
)
544 HSZ atom
= DDEGetAtom(item
);
546 HDDEDATA returned_data
= DdeClientTransaction(NULL
, 0,
552 if ( !returned_data
)
554 DDELogError(_T("DDE data request failed"));
559 DWORD len
= DdeGetData(returned_data
, (LPBYTE
)m_bufPtr
, m_bufSize
, 0);
561 DdeFreeDataHandle(returned_data
);
569 bool wxDDEConnection::Poke(const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
574 size
= wxStrlen(data
) + 1;
577 HSZ item_atom
= DDEGetAtom(item
);
578 bool ok
= DdeClientTransaction((LPBYTE
)data
, size
,
586 DDELogError(_("DDE poke request failed"));
592 bool wxDDEConnection::StartAdvise(const wxString
& item
)
595 HSZ atom
= DDEGetAtom(item
);
597 bool ok
= DdeClientTransaction(NULL
, 0,
605 DDELogError(_("Failed to establish an advise loop with DDE server"));
611 bool wxDDEConnection::StopAdvise(const wxString
& item
)
614 HSZ atom
= DDEGetAtom(item
);
616 bool ok
= DdeClientTransaction(NULL
, 0,
624 DDELogError(_("Failed to terminate the advise loop with DDE server"));
630 // Calls that SERVER can make
631 bool wxDDEConnection::Advise(const wxString
& item
,
638 size
= wxStrlen(data
) + 1;
641 HSZ item_atom
= DDEGetAtom(item
);
642 HSZ topic_atom
= DDEGetAtom(m_topicName
);
643 m_sendingData
= data
;
647 bool ok
= DdePostAdvise(DDEIdInst
, topic_atom
, item_atom
) != 0;
650 DDELogError(_("Failed to send DDE advise notification"));
656 bool wxDDEConnection::OnDisconnect()
662 // ----------------------------------------------------------------------------
664 // ----------------------------------------------------------------------------
666 #define DDERETURN HDDEDATA
668 HDDEDATA EXPENTRY _EXPORT
669 _DDECallback(WORD wType
,
675 DWORD
WXUNUSED(lData1
),
676 DWORD
WXUNUSED(lData2
))
682 wxString topic
= DDEStringFromAtom(hsz1
),
683 srv
= DDEStringFromAtom(hsz2
);
684 wxDDEServer
*server
= DDEFindServer(srv
);
687 wxDDEConnection
*connection
=
688 (wxDDEConnection
*) server
->OnAcceptConnection(topic
);
691 connection
->m_server
= server
;
692 server
->GetConnections().Append(connection
);
693 connection
->m_hConv
= 0;
694 connection
->m_topicName
= topic
;
695 DDECurrentlyConnecting
= connection
;
696 return (DDERETURN
)(DWORD
)TRUE
;
702 case XTYP_CONNECT_CONFIRM
:
704 if (DDECurrentlyConnecting
)
706 DDECurrentlyConnecting
->m_hConv
= (WXHCONV
) hConv
;
707 DDECurrentlyConnecting
= NULL
;
708 return (DDERETURN
)(DWORD
)TRUE
;
713 case XTYP_DISCONNECT
:
715 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
716 if (connection
&& connection
->OnDisconnect())
718 DDEDeleteConnection(hConv
); // Delete mapping: hConv => connection
719 return (DDERETURN
)(DWORD
)TRUE
;
726 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
730 DWORD len
= DdeGetData(hData
,
731 (LPBYTE
)connection
->m_bufPtr
,
732 connection
->m_bufSize
,
734 DdeFreeDataHandle(hData
);
735 if ( connection
->OnExecute(connection
->m_topicName
,
736 connection
->m_bufPtr
,
738 (wxIPCFormat
) wFmt
) )
740 return (DDERETURN
)(DWORD
)DDE_FACK
;
744 return (DDERETURN
)DDE_FNOTPROCESSED
;
749 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
753 wxString item_name
= DDEStringFromAtom(hsz2
);
756 char *data
= connection
->OnRequest(connection
->m_topicName
,
763 user_size
= wxStrlen((wxChar
*)data
) + 1;
765 HDDEDATA handle
= DdeCreateDataHandle(DDEIdInst
,
772 return (DDERETURN
)handle
;
780 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
784 wxString item_name
= DDEStringFromAtom(hsz2
);
786 DWORD len
= DdeGetData(hData
,
787 (LPBYTE
)connection
->m_bufPtr
,
788 connection
->m_bufSize
,
790 DdeFreeDataHandle(hData
);
792 connection
->OnPoke(connection
->m_topicName
,
794 (wxChar
*)connection
->m_bufPtr
,
798 return (DDERETURN
)DDE_FACK
;
802 return (DDERETURN
)DDE_FNOTPROCESSED
;
808 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
812 wxString item_name
= DDEStringFromAtom(hsz2
);
814 return (DDERETURN
)connection
->
815 OnStartAdvise(connection
->m_topicName
, item_name
);
823 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
827 wxString item_name
= DDEStringFromAtom(hsz2
);
829 return (DDERETURN
)connection
->
830 OnStopAdvise(connection
->m_topicName
, item_name
);
838 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
840 if (connection
&& connection
->m_sendingData
)
842 HDDEDATA data
= DdeCreateDataHandle
845 (LPBYTE
)connection
->m_sendingData
,
846 connection
->m_dataSize
,
849 connection
->m_dataType
,
853 connection
->m_sendingData
= NULL
;
855 return (DDERETURN
)data
;
863 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
867 wxString item_name
= DDEStringFromAtom(hsz2
);
869 DWORD len
= DdeGetData(hData
,
870 (LPBYTE
)connection
->m_bufPtr
,
871 connection
->m_bufSize
,
873 DdeFreeDataHandle(hData
);
874 if ( connection
->OnAdvise(connection
->m_topicName
,
876 connection
->m_bufPtr
,
878 (wxIPCFormat
) wFmt
) )
880 return (DDERETURN
)(DWORD
)DDE_FACK
;
884 return (DDERETURN
)DDE_FNOTPROCESSED
;
891 // ----------------------------------------------------------------------------
892 // DDE strings and atoms
893 // ----------------------------------------------------------------------------
896 static HSZ
DDEAddAtom(const wxString
& string
)
898 HSZ atom
= DDEAtomFromString(string
);
899 wxAtomTable
.Append(string
, (wxObject
*)atom
);
903 static HSZ
DDEGetAtom(const wxString
& string
)
905 wxNode
*node
= wxAtomTable
.Find(string
);
907 return (HSZ
)node
->Data();
911 return (HSZ
)(wxAtomTable
.Find(string
)->Data());
916 static HSZ
DDEAtomFromString(const wxString
& s
)
918 wxASSERT_MSG( DDEIdInst
, _T("DDE not initialized") );
920 HSZ hsz
= DdeCreateStringHandle(DDEIdInst
, (wxChar
*) s
.c_str(), DDE_CP
);
923 DDELogError(_("Failed to create DDE string"));
929 static wxString
DDEStringFromAtom(HSZ hsz
)
931 // all DDE strings are normally limited to 255 bytes
932 static const size_t len
= 256;
935 (void)DdeQueryString(DDEIdInst
, hsz
, s
.GetWriteBuf(len
), len
, DDE_CP
);
941 // ----------------------------------------------------------------------------
943 // ----------------------------------------------------------------------------
945 static void DDELogError(const wxString
& s
, UINT error
)
949 error
= DdeGetLastError(DDEIdInst
);
952 wxLogError(s
+ _T(": ") + DDEGetErrorMsg(error
));
955 static wxString
DDEGetErrorMsg(UINT error
)
960 case DMLERR_NO_ERROR
:
961 err
= _("no DDE error.");
964 case DMLERR_ADVACKTIMEOUT
:
965 err
= _("a request for a synchronous advise transaction has timed out.");
968 err
= _("the response to the transaction caused the DDE_FBUSY bit to be set.");
970 case DMLERR_DATAACKTIMEOUT
:
971 err
= _("a request for a synchronous data transaction has timed out.");
973 case DMLERR_DLL_NOT_INITIALIZED
:
974 err
= _("a DDEML function was called without first calling the DdeInitialize function,\nor an invalid instance identifier\nwas passed to a DDEML function.");
976 case DMLERR_DLL_USAGE
:
977 err
= _("an application initialized as APPCLASS_MONITOR has\nattempted to perform a DDE transaction,\nor an application initialized as APPCMD_CLIENTONLY has \nattempted to perform server transactions.");
979 case DMLERR_EXECACKTIMEOUT
:
980 err
= _("a request for a synchronous execute transaction has timed out.");
982 case DMLERR_INVALIDPARAMETER
:
983 err
= _("a parameter failed to be validated by the DDEML.");
985 case DMLERR_LOW_MEMORY
:
986 err
= _("a DDEML application has created a prolonged race condition.");
988 case DMLERR_MEMORY_ERROR
:
989 err
= _("a memory allocation failed.");
991 case DMLERR_NO_CONV_ESTABLISHED
:
992 err
= _("a client's attempt to establish a conversation has failed.");
994 case DMLERR_NOTPROCESSED
:
995 err
= _("a transaction failed.");
997 case DMLERR_POKEACKTIMEOUT
:
998 err
= _("a request for a synchronous poke transaction has timed out.");
1000 case DMLERR_POSTMSG_FAILED
:
1001 err
= _("an internal call to the PostMessage function has failed. ");
1003 case DMLERR_REENTRANCY
:
1004 err
= _("reentrancy problem.");
1006 case DMLERR_SERVER_DIED
:
1007 err
= _("a server-side transaction was attempted on a conversation\nthat was terminated by the client, or the server\nterminated before completing a transaction.");
1009 case DMLERR_SYS_ERROR
:
1010 err
= _("an internal error has occurred in the DDEML.");
1012 case DMLERR_UNADVACKTIMEOUT
:
1013 err
= _("a request to end an advise transaction has timed out.");
1015 case DMLERR_UNFOUND_QUEUE_ID
:
1016 err
= _("an invalid transaction identifier was passed to a DDEML function.\nOnce the application has returned from an XTYP_XACT_COMPLETE callback,\nthe transaction identifier for that callback is no longer valid.");
1019 err
.Printf(_("Unknown DDE error %08x"), error
);