1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DDE classes
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dde.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/module.h"
41 #include "wx/hashmap.h"
43 #include "wx/msw/private.h"
48 #ifdef __GNUWIN32_OLD__
49 #include "wx/msw/gnuwin32/extra.h"
52 // some compilers headers don't define this one (mingw32)
53 #ifndef DMLERR_NO_ERROR
54 #define DMLERR_NO_ERROR (0)
56 // this one is also missing from some mingw32 headers, but there is no way
57 // to test for it (I know of) - the test for DMLERR_NO_ERROR works for me,
58 // but is surely not the right thing to do
60 HDDEDATA STDCALL
DdeClientTransaction(LPBYTE pData
,
68 #endif // no DMLERR_NO_ERROR
70 // ----------------------------------------------------------------------------
71 // macros and constants
72 // ----------------------------------------------------------------------------
77 #define _EXPORT _export
81 #define DDE_CP CP_WINUNICODE
83 #define DDE_CP CP_WINANSI
86 #define GetHConv() ((HCONV)m_hConv)
88 // default timeout for DDE operations (5sec)
89 #define DDE_TIMEOUT 5000
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 static wxDDEConnection
*DDEFindConnection(HCONV hConv
);
96 static void DDEDeleteConnection(HCONV hConv
);
97 static wxDDEServer
*DDEFindServer(const wxString
& s
);
99 extern "C" HDDEDATA EXPENTRY _EXPORT
_DDECallback(WORD wType
,
108 // Add topic name to atom table before using in conversations
109 static HSZ
DDEAddAtom(const wxString
& string
);
110 static HSZ
DDEGetAtom(const wxString
& string
);
113 static HSZ
DDEAtomFromString(const wxString
& s
);
114 static wxString
DDEStringFromAtom(HSZ hsz
);
117 static wxString
DDEGetErrorMsg(UINT error
);
118 static void DDELogError(const wxString
& s
, UINT error
= DMLERR_NO_ERROR
);
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 WX_DECLARE_STRING_HASH_MAP( HSZ
, wxAtomMap
);
126 static DWORD DDEIdInst
= 0L;
127 static wxDDEConnection
*DDECurrentlyConnecting
= NULL
;
128 static wxAtomMap wxAtomTable
;
130 #include "wx/listimpl.cpp"
132 WX_DEFINE_LIST(wxDDEClientList
);
133 WX_DEFINE_LIST(wxDDEServerList
);
134 WX_DEFINE_LIST(wxDDEConnectionList
);
136 static wxDDEClientList wxDDEClientObjects
;
137 static wxDDEServerList wxDDEServerObjects
;
139 static bool DDEInitialized
= false;
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 // A module to allow DDE cleanup without calling these functions
146 // from app.cpp or from the user's application.
148 class wxDDEModule
: public wxModule
152 bool OnInit() { return true; }
153 void OnExit() { wxDDECleanUp(); }
156 DECLARE_DYNAMIC_CLASS(wxDDEModule
)
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 IMPLEMENT_DYNAMIC_CLASS(wxDDEServer
, wxServerBase
)
164 IMPLEMENT_DYNAMIC_CLASS(wxDDEClient
, wxClientBase
)
165 IMPLEMENT_CLASS(wxDDEConnection
, wxConnectionBase
)
166 IMPLEMENT_DYNAMIC_CLASS(wxDDEModule
, wxModule
)
168 // ============================================================================
170 // ============================================================================
172 // ----------------------------------------------------------------------------
173 // initialization and cleanup
174 // ----------------------------------------------------------------------------
176 extern void wxDDEInitialize()
178 if ( !DDEInitialized
)
180 // Should insert filter flags
181 PFNCALLBACK callback
= (PFNCALLBACK
)
182 MakeProcInstance((FARPROC
)_DDECallback
, wxGetInstance());
183 UINT rc
= DdeInitialize(&DDEIdInst
, callback
, APPCLASS_STANDARD
, 0L);
184 if ( rc
!= DMLERR_NO_ERROR
)
186 DDELogError(_T("Failed to initialize DDE"), rc
);
190 DDEInitialized
= true;
197 // deleting them later won't work as DDE won't be initialized any more
198 wxASSERT_MSG( wxDDEServerObjects
.empty() &&
199 wxDDEClientObjects
.empty(),
200 _T("all DDE objects should be deleted by now") );
204 if ( DDEIdInst
!= 0 )
206 DdeUninitialize(DDEIdInst
);
211 // ----------------------------------------------------------------------------
212 // functions working with the global connection list(s)
213 // ----------------------------------------------------------------------------
215 // Global find connection
216 static wxDDEConnection
*DDEFindConnection(HCONV hConv
)
218 wxDDEServerList::compatibility_iterator serverNode
= wxDDEServerObjects
.GetFirst();
219 wxDDEConnection
*found
= NULL
;
220 while (serverNode
&& !found
)
222 wxDDEServer
*object
= serverNode
->GetData();
223 found
= object
->FindConnection((WXHCONV
) hConv
);
224 serverNode
= serverNode
->GetNext();
232 wxDDEClientList::compatibility_iterator clientNode
= wxDDEClientObjects
.GetFirst();
233 while (clientNode
&& !found
)
235 wxDDEClient
*object
= clientNode
->GetData();
236 found
= object
->FindConnection((WXHCONV
) hConv
);
237 clientNode
= clientNode
->GetNext();
242 // Global delete connection
243 static void DDEDeleteConnection(HCONV hConv
)
245 wxDDEServerList::compatibility_iterator serverNode
= wxDDEServerObjects
.GetFirst();
247 while (serverNode
&& !found
)
249 wxDDEServer
*object
= serverNode
->GetData();
250 found
= object
->DeleteConnection((WXHCONV
) hConv
);
251 serverNode
= serverNode
->GetNext();
258 wxDDEClientList::compatibility_iterator clientNode
= wxDDEClientObjects
.GetFirst();
259 while (clientNode
&& !found
)
261 wxDDEClient
*object
= clientNode
->GetData();
262 found
= object
->DeleteConnection((WXHCONV
) hConv
);
263 clientNode
= clientNode
->GetNext();
267 // Find a server from a service name
268 static wxDDEServer
*DDEFindServer(const wxString
& s
)
270 wxDDEServerList::compatibility_iterator node
= wxDDEServerObjects
.GetFirst();
271 wxDDEServer
*found
= NULL
;
272 while (node
&& !found
)
274 wxDDEServer
*object
= node
->GetData();
276 if (object
->GetServiceName() == s
)
282 node
= node
->GetNext();
289 // ----------------------------------------------------------------------------
291 // ----------------------------------------------------------------------------
293 wxDDEServer::wxDDEServer()
297 wxDDEServerObjects
.Append(this);
300 bool wxDDEServer::Create(const wxString
& server
)
302 m_serviceName
= server
;
304 if ( !DdeNameService(DDEIdInst
, DDEAtomFromString(server
), (HSZ
)NULL
, DNS_REGISTER
) )
306 DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
315 wxDDEServer::~wxDDEServer()
317 if ( !!m_serviceName
)
319 if ( !DdeNameService(DDEIdInst
, DDEAtomFromString(m_serviceName
),
320 (HSZ
)NULL
, DNS_UNREGISTER
) )
322 DDELogError(wxString::Format(_("Failed to unregister DDE server '%s'"),
323 m_serviceName
.c_str()));
327 wxDDEServerObjects
.DeleteObject(this);
329 wxDDEConnectionList::compatibility_iterator node
= m_connections
.GetFirst();
332 wxDDEConnection
*connection
= node
->GetData();
333 wxDDEConnectionList::compatibility_iterator next
= node
->GetNext();
334 connection
->SetConnected(false);
335 connection
->OnDisconnect(); // May delete the node implicitly
339 // If any left after this, delete them
340 node
= m_connections
.GetFirst();
343 wxDDEConnection
*connection
= node
->GetData();
344 wxDDEConnectionList::compatibility_iterator next
= node
->GetNext();
350 wxConnectionBase
*wxDDEServer::OnAcceptConnection(const wxString
& /* topic */)
352 return new wxDDEConnection
;
355 wxDDEConnection
*wxDDEServer::FindConnection(WXHCONV conv
)
357 wxDDEConnectionList::compatibility_iterator node
= m_connections
.GetFirst();
358 wxDDEConnection
*found
= NULL
;
359 while (node
&& !found
)
361 wxDDEConnection
*connection
= node
->GetData();
362 if (connection
->m_hConv
== conv
)
364 else node
= node
->GetNext();
369 // Only delete the entry in the map, not the actual connection
370 bool wxDDEServer::DeleteConnection(WXHCONV conv
)
372 wxDDEConnectionList::compatibility_iterator node
= m_connections
.GetFirst();
375 wxDDEConnection
*connection
= node
->GetData();
376 if (connection
->m_hConv
== conv
)
378 m_connections
.Erase(node
);
383 node
= node
->GetNext();
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 wxDDEClient::wxDDEClient()
397 wxDDEClientObjects
.Append(this);
400 wxDDEClient::~wxDDEClient()
402 wxDDEClientObjects
.DeleteObject(this);
403 wxDDEConnectionList::compatibility_iterator node
= m_connections
.GetFirst();
406 wxDDEConnection
*connection
= node
->GetData();
407 delete connection
; // Deletes the node implicitly (see ~wxDDEConnection)
408 node
= m_connections
.GetFirst();
412 bool wxDDEClient::ValidHost(const wxString
& /* host */)
417 wxConnectionBase
*wxDDEClient::MakeConnection(const wxString
& WXUNUSED(host
),
418 const wxString
& server
,
419 const wxString
& topic
)
421 HCONV hConv
= DdeConnect(DDEIdInst
, DDEAtomFromString(server
), DDEAtomFromString(topic
),
425 DDELogError(wxString::Format(_("Failed to create connection to server '%s' on topic '%s'"),
426 server
.c_str(), topic
.c_str()));
430 wxDDEConnection
*connection
= (wxDDEConnection
*) OnMakeConnection();
433 connection
->m_hConv
= (WXHCONV
) hConv
;
434 connection
->m_topicName
= topic
;
435 connection
->m_client
= this;
436 m_connections
.Append(connection
);
441 return (wxConnectionBase
*) NULL
;
444 wxConnectionBase
*wxDDEClient::OnMakeConnection()
446 return new wxDDEConnection
;
449 wxDDEConnection
*wxDDEClient::FindConnection(WXHCONV conv
)
451 wxDDEConnectionList::compatibility_iterator node
= m_connections
.GetFirst();
452 wxDDEConnection
*found
= NULL
;
453 while (node
&& !found
)
455 wxDDEConnection
*connection
= node
->GetData();
456 if (connection
->m_hConv
== conv
)
458 else node
= node
->GetNext();
463 // Only delete the entry in the map, not the actual connection
464 bool wxDDEClient::DeleteConnection(WXHCONV conv
)
466 wxDDEConnectionList::compatibility_iterator node
= m_connections
.GetFirst();
469 wxDDEConnection
*connection
= node
->GetData();
470 if (connection
->m_hConv
== conv
)
472 m_connections
.Erase(node
);
475 else node
= node
->GetNext();
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 wxDDEConnection::wxDDEConnection(wxChar
*buffer
, int size
)
485 : wxConnectionBase(buffer
, size
)
491 m_sendingData
= NULL
;
494 wxDDEConnection::wxDDEConnection()
498 m_sendingData
= NULL
;
503 wxDDEConnection::~wxDDEConnection()
507 m_server
->GetConnections().DeleteObject(this);
509 m_client
->GetConnections().DeleteObject(this);
512 // Calls that CLIENT can make
513 bool wxDDEConnection::Disconnect()
515 if ( !GetConnected() )
518 DDEDeleteConnection(GetHConv());
520 bool ok
= DdeDisconnect(GetHConv()) != 0;
523 DDELogError(_T("Failed to disconnect from DDE server gracefully"));
526 SetConnected( false ); // so we don't try and disconnect again
531 bool wxDDEConnection::Execute(const wxChar
*data
, int size
, wxIPCFormat format
)
536 size
= wxStrlen(data
) + 1;
539 bool ok
= DdeClientTransaction((LPBYTE
)data
, size
,
549 DDELogError(_T("DDE execute request failed"));
555 wxChar
*wxDDEConnection::Request(const wxString
& item
, int *size
, wxIPCFormat format
)
559 HSZ atom
= DDEGetAtom(item
);
561 HDDEDATA returned_data
= DdeClientTransaction(NULL
, 0,
567 if ( !returned_data
)
569 DDELogError(_T("DDE data request failed"));
574 DWORD len
= DdeGetData(returned_data
, NULL
, 0, 0);
576 wxChar
*data
= GetBufferAtLeast( len
);
577 wxASSERT_MSG(data
!= NULL
,
578 _T("Buffer too small in wxDDEConnection::Request") );
579 DdeGetData(returned_data
, (LPBYTE
)data
, len
, 0);
581 DdeFreeDataHandle(returned_data
);
589 bool wxDDEConnection::Poke(const wxString
& item
, wxChar
*data
, int size
, wxIPCFormat format
)
594 size
= wxStrlen(data
) + 1;
597 HSZ item_atom
= DDEGetAtom(item
);
598 bool ok
= DdeClientTransaction((LPBYTE
)data
, size
,
606 DDELogError(_("DDE poke request failed"));
612 bool wxDDEConnection::StartAdvise(const wxString
& item
)
615 HSZ atom
= DDEGetAtom(item
);
617 bool ok
= DdeClientTransaction(NULL
, 0,
625 DDELogError(_("Failed to establish an advise loop with DDE server"));
631 bool wxDDEConnection::StopAdvise(const wxString
& item
)
634 HSZ atom
= DDEGetAtom(item
);
636 bool ok
= DdeClientTransaction(NULL
, 0,
644 DDELogError(_("Failed to terminate the advise loop with DDE server"));
650 // Calls that SERVER can make
651 bool wxDDEConnection::Advise(const wxString
& item
,
658 size
= wxStrlen(data
) + 1;
661 HSZ item_atom
= DDEGetAtom(item
);
662 HSZ topic_atom
= DDEGetAtom(m_topicName
);
663 m_sendingData
= data
; // mrf: potential for scope problems here?
667 bool ok
= DdePostAdvise(DDEIdInst
, topic_atom
, item_atom
) != 0;
670 DDELogError(_("Failed to send DDE advise notification"));
676 bool wxDDEConnection::OnDisconnect()
682 // ----------------------------------------------------------------------------
684 // ----------------------------------------------------------------------------
686 #define DDERETURN HDDEDATA
688 HDDEDATA EXPENTRY _EXPORT
689 _DDECallback(WORD wType
,
695 DWORD
WXUNUSED(lData1
),
696 DWORD
WXUNUSED(lData2
))
702 wxString topic
= DDEStringFromAtom(hsz1
),
703 srv
= DDEStringFromAtom(hsz2
);
704 wxDDEServer
*server
= DDEFindServer(srv
);
707 wxDDEConnection
*connection
=
708 (wxDDEConnection
*) server
->OnAcceptConnection(topic
);
711 connection
->m_server
= server
;
712 server
->GetConnections().Append(connection
);
713 connection
->m_hConv
= 0;
714 connection
->m_topicName
= topic
;
715 DDECurrentlyConnecting
= connection
;
716 return (DDERETURN
)(DWORD
)true;
722 case XTYP_CONNECT_CONFIRM
:
724 if (DDECurrentlyConnecting
)
726 DDECurrentlyConnecting
->m_hConv
= (WXHCONV
) hConv
;
727 DDECurrentlyConnecting
= NULL
;
728 return (DDERETURN
)(DWORD
)true;
733 case XTYP_DISCONNECT
:
735 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
738 connection
->SetConnected( false );
739 if (connection
->OnDisconnect())
741 DDEDeleteConnection(hConv
); // Delete mapping: hConv => connection
742 return (DDERETURN
)(DWORD
)true;
750 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
754 DWORD len
= DdeGetData(hData
, NULL
, 0, 0);
756 wxChar
*data
= connection
->GetBufferAtLeast( len
);
757 wxASSERT_MSG(data
!= NULL
,
758 _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
760 DdeGetData(hData
, (LPBYTE
)data
, len
, 0);
762 DdeFreeDataHandle(hData
);
764 if ( connection
->OnExecute(connection
->m_topicName
,
767 (wxIPCFormat
) wFmt
) )
769 return (DDERETURN
)(DWORD
)DDE_FACK
;
773 return (DDERETURN
)DDE_FNOTPROCESSED
;
778 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
782 wxString item_name
= DDEStringFromAtom(hsz2
);
785 wxChar
*data
= connection
->OnRequest(connection
->m_topicName
,
792 user_size
= wxStrlen((wxChar
*)data
) + 1;
794 HDDEDATA handle
= DdeCreateDataHandle(DDEIdInst
,
801 return (DDERETURN
)handle
;
809 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
813 wxString item_name
= DDEStringFromAtom(hsz2
);
815 DWORD len
= DdeGetData(hData
, NULL
, 0, 0);
817 wxChar
*data
= connection
->GetBufferAtLeast( len
);
818 wxASSERT_MSG(data
!= NULL
,
819 _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
821 DdeGetData(hData
, (LPBYTE
)data
, len
, 0);
823 DdeFreeDataHandle(hData
);
825 connection
->OnPoke(connection
->m_topicName
,
831 return (DDERETURN
)DDE_FACK
;
835 return (DDERETURN
)DDE_FNOTPROCESSED
;
841 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
845 wxString item_name
= DDEStringFromAtom(hsz2
);
847 return (DDERETURN
)connection
->
848 OnStartAdvise(connection
->m_topicName
, item_name
);
856 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
860 wxString item_name
= DDEStringFromAtom(hsz2
);
862 return (DDERETURN
)connection
->
863 OnStopAdvise(connection
->m_topicName
, item_name
);
871 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
873 if (connection
&& connection
->m_sendingData
)
875 HDDEDATA data
= DdeCreateDataHandle
878 (LPBYTE
)connection
->m_sendingData
,
879 connection
->m_dataSize
,
882 connection
->m_dataType
,
886 connection
->m_sendingData
= NULL
;
888 return (DDERETURN
)data
;
896 wxDDEConnection
*connection
= DDEFindConnection(hConv
);
900 wxString item_name
= DDEStringFromAtom(hsz2
);
902 DWORD len
= DdeGetData(hData
, NULL
, 0, 0);
904 wxChar
*data
= connection
->GetBufferAtLeast( len
);
905 wxASSERT_MSG(data
!= NULL
,
906 _T("Buffer too small in _DDECallback (XTYP_ADVDATA)") );
908 DdeGetData(hData
, (LPBYTE
)data
, len
, 0);
910 DdeFreeDataHandle(hData
);
911 if ( connection
->OnAdvise(connection
->m_topicName
,
915 (wxIPCFormat
) wFmt
) )
917 return (DDERETURN
)(DWORD
)DDE_FACK
;
921 return (DDERETURN
)DDE_FNOTPROCESSED
;
928 // ----------------------------------------------------------------------------
929 // DDE strings and atoms
930 // ----------------------------------------------------------------------------
933 static HSZ
DDEAddAtom(const wxString
& str
)
935 HSZ atom
= DDEAtomFromString(str
);
936 wxAtomTable
[str
] = atom
;
940 static HSZ
DDEGetAtom(const wxString
& str
)
942 wxAtomMap::iterator it
= wxAtomTable
.find(str
);
944 if (it
!= wxAtomTable
.end())
947 return DDEAddAtom(str
);
951 static HSZ
DDEAtomFromString(const wxString
& s
)
953 wxASSERT_MSG( DDEIdInst
, _T("DDE not initialized") );
955 HSZ hsz
= DdeCreateStringHandle(DDEIdInst
, (wxChar
*) s
.c_str(), DDE_CP
);
958 DDELogError(_("Failed to create DDE string"));
964 static wxString
DDEStringFromAtom(HSZ hsz
)
966 // all DDE strings are normally limited to 255 bytes
967 static const size_t len
= 256;
970 (void)DdeQueryString(DDEIdInst
, hsz
, wxStringBuffer(s
, len
), len
, DDE_CP
);
975 // ----------------------------------------------------------------------------
977 // ----------------------------------------------------------------------------
979 static void DDELogError(const wxString
& s
, UINT error
)
983 error
= DdeGetLastError(DDEIdInst
);
986 wxLogError(s
+ _T(": ") + DDEGetErrorMsg(error
));
989 static wxString
DDEGetErrorMsg(UINT error
)
994 case DMLERR_NO_ERROR
:
995 err
= _("no DDE error.");
998 case DMLERR_ADVACKTIMEOUT
:
999 err
= _("a request for a synchronous advise transaction has timed out.");
1002 err
= _("the response to the transaction caused the DDE_FBUSY bit to be set.");
1004 case DMLERR_DATAACKTIMEOUT
:
1005 err
= _("a request for a synchronous data transaction has timed out.");
1007 case DMLERR_DLL_NOT_INITIALIZED
:
1008 err
= _("a DDEML function was called without first calling the DdeInitialize function,\nor an invalid instance identifier\nwas passed to a DDEML function.");
1010 case DMLERR_DLL_USAGE
:
1011 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.");
1013 case DMLERR_EXECACKTIMEOUT
:
1014 err
= _("a request for a synchronous execute transaction has timed out.");
1016 case DMLERR_INVALIDPARAMETER
:
1017 err
= _("a parameter failed to be validated by the DDEML.");
1019 case DMLERR_LOW_MEMORY
:
1020 err
= _("a DDEML application has created a prolonged race condition.");
1022 case DMLERR_MEMORY_ERROR
:
1023 err
= _("a memory allocation failed.");
1025 case DMLERR_NO_CONV_ESTABLISHED
:
1026 err
= _("a client's attempt to establish a conversation has failed.");
1028 case DMLERR_NOTPROCESSED
:
1029 err
= _("a transaction failed.");
1031 case DMLERR_POKEACKTIMEOUT
:
1032 err
= _("a request for a synchronous poke transaction has timed out.");
1034 case DMLERR_POSTMSG_FAILED
:
1035 err
= _("an internal call to the PostMessage function has failed. ");
1037 case DMLERR_REENTRANCY
:
1038 err
= _("reentrancy problem.");
1040 case DMLERR_SERVER_DIED
:
1041 err
= _("a server-side transaction was attempted on a conversation\nthat was terminated by the client, or the server\nterminated before completing a transaction.");
1043 case DMLERR_SYS_ERROR
:
1044 err
= _("an internal error has occurred in the DDEML.");
1046 case DMLERR_UNADVACKTIMEOUT
:
1047 err
= _("a request to end an advise transaction has timed out.");
1049 case DMLERR_UNFOUND_QUEUE_ID
:
1050 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.");
1053 err
.Printf(_("Unknown DDE error %08x"), error
);