Applied patch [ 1263950 ] wxConnection fixes for Unicode
[wxWidgets.git] / src / msw / dde.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/dde.cpp
3 // Purpose: DDE classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dde.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_IPC
32
33 #ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/app.h"
36 #endif
37
38 #include "wx/module.h"
39 #include "wx/dde.h"
40 #include "wx/intl.h"
41 #include "wx/hashmap.h"
42
43 #include "wx/msw/private.h"
44
45 #include <string.h>
46 #include <ddeml.h>
47
48 // ----------------------------------------------------------------------------
49 // macros and constants
50 // ----------------------------------------------------------------------------
51
52 #ifdef __WIN32__
53 #define _EXPORT
54 #else
55 #define _EXPORT _export
56 #endif
57
58 #if wxUSE_UNICODE
59 #define DDE_CP CP_WINUNICODE
60 #else
61 #define DDE_CP CP_WINANSI
62 #endif
63
64 #define GetHConv() ((HCONV)m_hConv)
65
66 // default timeout for DDE operations (5sec)
67 #define DDE_TIMEOUT 5000
68
69 // ----------------------------------------------------------------------------
70 // private functions
71 // ----------------------------------------------------------------------------
72
73 static wxDDEConnection *DDEFindConnection(HCONV hConv);
74 static void DDEDeleteConnection(HCONV hConv);
75 static wxDDEServer *DDEFindServer(const wxString& s);
76
77 extern "C" HDDEDATA EXPENTRY _EXPORT _DDECallback(WORD wType,
78 WORD wFmt,
79 HCONV hConv,
80 HSZ hsz1,
81 HSZ hsz2,
82 HDDEDATA hData,
83 DWORD lData1,
84 DWORD lData2);
85
86 // Add topic name to atom table before using in conversations
87 static HSZ DDEAddAtom(const wxString& string);
88 static HSZ DDEGetAtom(const wxString& string);
89
90 // string handles
91 static HSZ DDEAtomFromString(const wxString& s);
92 static wxString DDEStringFromAtom(HSZ hsz);
93 static void DDEFreeString(HSZ hsz);
94
95 // error handling
96 static wxString DDEGetErrorMsg(UINT error);
97 static void DDELogError(const wxString& s, UINT error = DMLERR_NO_ERROR);
98
99 // ----------------------------------------------------------------------------
100 // global variables
101 // ----------------------------------------------------------------------------
102
103 WX_DECLARE_STRING_HASH_MAP( HSZ, wxAtomMap );
104
105 static DWORD DDEIdInst = 0L;
106 static wxDDEConnection *DDECurrentlyConnecting = NULL;
107 static wxAtomMap wxAtomTable;
108
109 #include "wx/listimpl.cpp"
110
111 WX_DEFINE_LIST(wxDDEClientList);
112 WX_DEFINE_LIST(wxDDEServerList);
113 WX_DEFINE_LIST(wxDDEConnectionList);
114
115 static wxDDEClientList wxDDEClientObjects;
116 static wxDDEServerList wxDDEServerObjects;
117
118 static bool DDEInitialized = false;
119
120 // ----------------------------------------------------------------------------
121 // private classes
122 // ----------------------------------------------------------------------------
123
124 // A module to allow DDE cleanup without calling these functions
125 // from app.cpp or from the user's application.
126
127 class wxDDEModule : public wxModule
128 {
129 public:
130 wxDDEModule() {}
131 bool OnInit() { return true; }
132 void OnExit() { wxDDECleanUp(); }
133
134 private:
135 DECLARE_DYNAMIC_CLASS(wxDDEModule)
136 };
137
138 // ----------------------------------------------------------------------------
139 // wxWin macros
140 // ----------------------------------------------------------------------------
141
142 IMPLEMENT_DYNAMIC_CLASS(wxDDEServer, wxServerBase)
143 IMPLEMENT_DYNAMIC_CLASS(wxDDEClient, wxClientBase)
144 IMPLEMENT_CLASS(wxDDEConnection, wxConnectionBase)
145 IMPLEMENT_DYNAMIC_CLASS(wxDDEModule, wxModule)
146
147 // ============================================================================
148 // implementation
149 // ============================================================================
150
151 // ----------------------------------------------------------------------------
152 // initialization and cleanup
153 // ----------------------------------------------------------------------------
154
155 extern void wxDDEInitialize()
156 {
157 if ( !DDEInitialized )
158 {
159 // Should insert filter flags
160 PFNCALLBACK callback = (PFNCALLBACK)
161 MakeProcInstance((FARPROC)_DDECallback, wxGetInstance());
162 UINT rc = DdeInitialize(&DDEIdInst, callback, APPCLASS_STANDARD, 0L);
163 if ( rc != DMLERR_NO_ERROR )
164 {
165 DDELogError(_T("Failed to initialize DDE"), rc);
166 }
167 else
168 {
169 DDEInitialized = true;
170 }
171 }
172 }
173
174 void wxDDECleanUp()
175 {
176 // deleting them later won't work as DDE won't be initialized any more
177 wxASSERT_MSG( wxDDEServerObjects.empty() &&
178 wxDDEClientObjects.empty(),
179 _T("all DDE objects should be deleted by now") );
180
181 wxAtomTable.clear();
182
183 if ( DDEIdInst != 0 )
184 {
185 DdeUninitialize(DDEIdInst);
186 DDEIdInst = 0;
187 }
188 }
189
190 // ----------------------------------------------------------------------------
191 // functions working with the global connection list(s)
192 // ----------------------------------------------------------------------------
193
194 // Global find connection
195 static wxDDEConnection *DDEFindConnection(HCONV hConv)
196 {
197 wxDDEServerList::compatibility_iterator serverNode = wxDDEServerObjects.GetFirst();
198 wxDDEConnection *found = NULL;
199 while (serverNode && !found)
200 {
201 wxDDEServer *object = serverNode->GetData();
202 found = object->FindConnection((WXHCONV) hConv);
203 serverNode = serverNode->GetNext();
204 }
205
206 if (found)
207 {
208 return found;
209 }
210
211 wxDDEClientList::compatibility_iterator clientNode = wxDDEClientObjects.GetFirst();
212 while (clientNode && !found)
213 {
214 wxDDEClient *object = clientNode->GetData();
215 found = object->FindConnection((WXHCONV) hConv);
216 clientNode = clientNode->GetNext();
217 }
218 return found;
219 }
220
221 // Global delete connection
222 static void DDEDeleteConnection(HCONV hConv)
223 {
224 wxDDEServerList::compatibility_iterator serverNode = wxDDEServerObjects.GetFirst();
225 bool found = false;
226 while (serverNode && !found)
227 {
228 wxDDEServer *object = serverNode->GetData();
229 found = object->DeleteConnection((WXHCONV) hConv);
230 serverNode = serverNode->GetNext();
231 }
232 if (found)
233 {
234 return;
235 }
236
237 wxDDEClientList::compatibility_iterator clientNode = wxDDEClientObjects.GetFirst();
238 while (clientNode && !found)
239 {
240 wxDDEClient *object = clientNode->GetData();
241 found = object->DeleteConnection((WXHCONV) hConv);
242 clientNode = clientNode->GetNext();
243 }
244 }
245
246 // Find a server from a service name
247 static wxDDEServer *DDEFindServer(const wxString& s)
248 {
249 wxDDEServerList::compatibility_iterator node = wxDDEServerObjects.GetFirst();
250 wxDDEServer *found = NULL;
251 while (node && !found)
252 {
253 wxDDEServer *object = node->GetData();
254
255 if (object->GetServiceName() == s)
256 {
257 found = object;
258 }
259 else
260 {
261 node = node->GetNext();
262 }
263 }
264
265 return found;
266 }
267
268 // ----------------------------------------------------------------------------
269 // wxDDEServer
270 // ----------------------------------------------------------------------------
271
272 wxDDEServer::wxDDEServer()
273 {
274 wxDDEInitialize();
275
276 wxDDEServerObjects.Append(this);
277 }
278
279 bool wxDDEServer::Create(const wxString& server)
280 {
281 m_serviceName = server;
282
283 HSZ hsz = DDEAtomFromString(server);
284
285 if ( !hsz )
286 {
287 return false;
288 }
289
290
291 bool success = (DdeNameService(DDEIdInst, hsz, (HSZ) NULL, DNS_REGISTER)
292 != NULL);
293
294 if (!success)
295 {
296 DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
297 server.c_str()));
298 }
299
300 DDEFreeString(hsz);
301
302 return success;
303 }
304
305 wxDDEServer::~wxDDEServer()
306 {
307 if ( !m_serviceName.IsEmpty() )
308 {
309 HSZ hsz = DDEAtomFromString(m_serviceName);
310
311 if (hsz)
312 {
313 if ( !DdeNameService(DDEIdInst, hsz,
314 (HSZ) NULL, DNS_UNREGISTER) )
315 {
316 DDELogError(wxString::Format(
317 _("Failed to unregister DDE server '%s'"),
318 m_serviceName.c_str()));
319 }
320
321 DDEFreeString(hsz);
322 }
323 }
324
325 wxDDEServerObjects.DeleteObject(this);
326
327 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
328 while (node)
329 {
330 wxDDEConnection *connection = node->GetData();
331 wxDDEConnectionList::compatibility_iterator next = node->GetNext();
332 connection->SetConnected(false);
333 connection->OnDisconnect(); // May delete the node implicitly
334 node = next;
335 }
336
337 // If any left after this, delete them
338 node = m_connections.GetFirst();
339 while (node)
340 {
341 wxDDEConnection *connection = node->GetData();
342 wxDDEConnectionList::compatibility_iterator next = node->GetNext();
343 delete connection;
344 node = next;
345 }
346 }
347
348 wxConnectionBase *wxDDEServer::OnAcceptConnection(const wxString& /* topic */)
349 {
350 return new wxDDEConnection;
351 }
352
353 wxDDEConnection *wxDDEServer::FindConnection(WXHCONV conv)
354 {
355 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
356 wxDDEConnection *found = NULL;
357 while (node && !found)
358 {
359 wxDDEConnection *connection = node->GetData();
360 if (connection->m_hConv == conv)
361 found = connection;
362 else node = node->GetNext();
363 }
364 return found;
365 }
366
367 // Only delete the entry in the map, not the actual connection
368 bool wxDDEServer::DeleteConnection(WXHCONV conv)
369 {
370 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
371 while (node)
372 {
373 wxDDEConnection *connection = node->GetData();
374 if (connection->m_hConv == conv)
375 {
376 m_connections.Erase(node);
377 return true;
378 }
379 else
380 {
381 node = node->GetNext();
382 }
383 }
384 return false;
385 }
386
387 // ----------------------------------------------------------------------------
388 // wxDDEClient
389 // ----------------------------------------------------------------------------
390
391 wxDDEClient::wxDDEClient()
392 {
393 wxDDEInitialize();
394
395 wxDDEClientObjects.Append(this);
396 }
397
398 wxDDEClient::~wxDDEClient()
399 {
400 wxDDEClientObjects.DeleteObject(this);
401 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
402 while (node)
403 {
404 wxDDEConnection *connection = node->GetData();
405 delete connection; // Deletes the node implicitly (see ~wxDDEConnection)
406 node = m_connections.GetFirst();
407 }
408 }
409
410 bool wxDDEClient::ValidHost(const wxString& /* host */)
411 {
412 return true;
413 }
414
415 wxConnectionBase *wxDDEClient::MakeConnection(const wxString& WXUNUSED(host),
416 const wxString& server,
417 const wxString& topic)
418 {
419 HSZ hszServer = DDEAtomFromString(server);
420
421 if ( !hszServer )
422 {
423 return (wxConnectionBase*) NULL;
424 }
425
426
427 HSZ hszTopic = DDEAtomFromString(topic);
428
429 if ( !hszTopic )
430 {
431 DDEFreeString(hszServer);
432 return (wxConnectionBase*) NULL;
433 }
434
435
436 HCONV hConv = ::DdeConnect(DDEIdInst, hszServer, hszTopic,
437 (PCONVCONTEXT) NULL);
438
439 DDEFreeString(hszServer);
440 DDEFreeString(hszTopic);
441
442
443 if ( !hConv )
444 {
445 DDELogError( wxString::Format(
446 _("Failed to create connection to server '%s' on topic '%s'"),
447 server.c_str(), topic.c_str()) );
448 }
449 else
450 {
451 wxDDEConnection *connection = (wxDDEConnection*) OnMakeConnection();
452 if (connection)
453 {
454 connection->m_hConv = (WXHCONV) hConv;
455 connection->m_topicName = topic;
456 connection->m_client = this;
457 m_connections.Append(connection);
458 return connection;
459 }
460 }
461
462 return (wxConnectionBase*) NULL;
463 }
464
465 wxConnectionBase *wxDDEClient::OnMakeConnection()
466 {
467 return new wxDDEConnection;
468 }
469
470 wxDDEConnection *wxDDEClient::FindConnection(WXHCONV conv)
471 {
472 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
473 wxDDEConnection *found = NULL;
474 while (node && !found)
475 {
476 wxDDEConnection *connection = node->GetData();
477 if (connection->m_hConv == conv)
478 found = connection;
479 else node = node->GetNext();
480 }
481 return found;
482 }
483
484 // Only delete the entry in the map, not the actual connection
485 bool wxDDEClient::DeleteConnection(WXHCONV conv)
486 {
487 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
488 while (node)
489 {
490 wxDDEConnection *connection = node->GetData();
491 if (connection->m_hConv == conv)
492 {
493 m_connections.Erase(node);
494 return true;
495 }
496 else node = node->GetNext();
497 }
498 return false;
499 }
500
501 // ----------------------------------------------------------------------------
502 // wxDDEConnection
503 // ----------------------------------------------------------------------------
504
505 wxDDEConnection::wxDDEConnection(wxChar *buffer, int size)
506 : wxConnectionBase(buffer, size)
507 {
508 m_client = NULL;
509 m_server = NULL;
510
511 m_hConv = 0;
512 m_sendingData = NULL;
513 }
514
515 wxDDEConnection::wxDDEConnection()
516 : wxConnectionBase()
517 {
518 m_hConv = 0;
519 m_sendingData = NULL;
520 m_server = NULL;
521 m_client = NULL;
522 }
523
524 wxDDEConnection::~wxDDEConnection()
525 {
526 Disconnect();
527 if (m_server)
528 m_server->GetConnections().DeleteObject(this);
529 else
530 m_client->GetConnections().DeleteObject(this);
531 }
532
533 // Calls that CLIENT can make
534 bool wxDDEConnection::Disconnect()
535 {
536 if ( !GetConnected() )
537 return true;
538
539 DDEDeleteConnection(GetHConv());
540
541 bool ok = DdeDisconnect(GetHConv()) != 0;
542 if ( !ok )
543 {
544 DDELogError(_T("Failed to disconnect from DDE server gracefully"));
545 }
546
547 SetConnected( false ); // so we don't try and disconnect again
548
549 return ok;
550 }
551
552 bool wxDDEConnection::Execute(const wxChar *data, int size, wxIPCFormat WXUNUSED(format))
553 {
554 DWORD result;
555 if (size < 0)
556 {
557 size = (wxStrlen(data) + 1) * sizeof(wxChar); // includes final NUL
558 }
559
560 bool ok = DdeClientTransaction((LPBYTE)data,
561 size,
562 GetHConv(),
563 NULL,
564 // If the transaction specified by the wType parameter does not pass data or is XTYP_EXECUTE,
565 // wFmt should be zero.
566 0,
567 XTYP_EXECUTE,
568 DDE_TIMEOUT,
569 &result) != 0;
570
571 if ( !ok )
572 {
573 DDELogError(_T("DDE execute request failed"));
574 }
575
576 return ok;
577 }
578
579 wxChar *wxDDEConnection::Request(const wxString& item, int *size, wxIPCFormat format)
580 {
581 DWORD result;
582
583 HSZ atom = DDEGetAtom(item);
584
585 HDDEDATA returned_data = DdeClientTransaction(NULL, 0,
586 GetHConv(),
587 atom, format,
588 XTYP_REQUEST,
589 DDE_TIMEOUT,
590 &result);
591 if ( !returned_data )
592 {
593 DDELogError(_T("DDE data request failed"));
594
595 return NULL;
596 }
597
598 DWORD len = DdeGetData(returned_data, NULL, 0, 0);
599
600 wxChar *data = GetBufferAtLeast( len );
601 wxASSERT_MSG(data != NULL,
602 _T("Buffer too small in wxDDEConnection::Request") );
603 (void) DdeGetData(returned_data, (LPBYTE)data, len, 0);
604
605 (void) DdeFreeDataHandle(returned_data);
606
607 if (size)
608 *size = (int)len;
609
610 return data;
611 }
612
613 bool wxDDEConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format)
614 {
615 DWORD result;
616 if (size < 0)
617 {
618 size = (wxStrlen(data) + 1) * sizeof(wxChar); // includes final NUL
619 }
620
621 HSZ item_atom = DDEGetAtom(item);
622 bool ok = DdeClientTransaction((LPBYTE)data,
623 size,
624 GetHConv(),
625 item_atom, format,
626 XTYP_POKE,
627 DDE_TIMEOUT,
628 &result) != 0;
629 if ( !ok )
630 {
631 DDELogError(_("DDE poke request failed"));
632 }
633
634 return ok;
635 }
636
637 bool wxDDEConnection::StartAdvise(const wxString& item)
638 {
639 DWORD result;
640 HSZ atom = DDEGetAtom(item);
641
642 bool ok = DdeClientTransaction(NULL, 0,
643 GetHConv(),
644 atom, CF_TEXT,
645 XTYP_ADVSTART,
646 DDE_TIMEOUT,
647 &result) != 0;
648 if ( !ok )
649 {
650 DDELogError(_("Failed to establish an advise loop with DDE server"));
651 }
652
653 return ok;
654 }
655
656 bool wxDDEConnection::StopAdvise(const wxString& item)
657 {
658 DWORD result;
659 HSZ atom = DDEGetAtom(item);
660
661 bool ok = DdeClientTransaction(NULL, 0,
662 GetHConv(),
663 atom, CF_TEXT,
664 XTYP_ADVSTOP,
665 DDE_TIMEOUT,
666 &result) != 0;
667 if ( !ok )
668 {
669 DDELogError(_("Failed to terminate the advise loop with DDE server"));
670 }
671
672 return ok;
673 }
674
675 // Calls that SERVER can make
676 bool wxDDEConnection::Advise(const wxString& item,
677 wxChar *data,
678 int size,
679 wxIPCFormat format)
680 {
681 if (size < 0)
682 {
683 size = (wxStrlen(data) + 1) * sizeof(wxChar); // includes final NUL
684 }
685
686 HSZ item_atom = DDEGetAtom(item);
687 HSZ topic_atom = DDEGetAtom(m_topicName);
688 m_sendingData = data; // mrf: potential for scope problems here?
689 m_dataSize = size;
690 // wxIPC_PRIVATE does not succeed, so use text instead
691 m_dataType = format == wxIPC_PRIVATE ? wxIPC_TEXT : format;
692
693 bool ok = DdePostAdvise(DDEIdInst, topic_atom, item_atom) != 0;
694 if ( !ok )
695 {
696 DDELogError(_("Failed to send DDE advise notification"));
697 }
698
699 return ok;
700 }
701
702 bool wxDDEConnection::OnDisconnect()
703 {
704 delete this;
705 return true;
706 }
707
708 // ----------------------------------------------------------------------------
709 // _DDECallback
710 // ----------------------------------------------------------------------------
711
712 #define DDERETURN HDDEDATA
713
714 HDDEDATA EXPENTRY _EXPORT
715 _DDECallback(WORD wType,
716 WORD wFmt,
717 HCONV hConv,
718 HSZ hsz1,
719 HSZ hsz2,
720 HDDEDATA hData,
721 DWORD WXUNUSED(lData1),
722 DWORD WXUNUSED(lData2))
723 {
724 switch (wType)
725 {
726 case XTYP_CONNECT:
727 {
728 wxString topic = DDEStringFromAtom(hsz1),
729 srv = DDEStringFromAtom(hsz2);
730 wxDDEServer *server = DDEFindServer(srv);
731 if (server)
732 {
733 wxDDEConnection *connection =
734 (wxDDEConnection*) server->OnAcceptConnection(topic);
735 if (connection)
736 {
737 connection->m_server = server;
738 server->GetConnections().Append(connection);
739 connection->m_hConv = 0;
740 connection->m_topicName = topic;
741 DDECurrentlyConnecting = connection;
742 return (DDERETURN)(DWORD)true;
743 }
744 }
745 break;
746 }
747
748 case XTYP_CONNECT_CONFIRM:
749 {
750 if (DDECurrentlyConnecting)
751 {
752 DDECurrentlyConnecting->m_hConv = (WXHCONV) hConv;
753 DDECurrentlyConnecting = NULL;
754 return (DDERETURN)(DWORD)true;
755 }
756 break;
757 }
758
759 case XTYP_DISCONNECT:
760 {
761 wxDDEConnection *connection = DDEFindConnection(hConv);
762 if (connection)
763 {
764 connection->SetConnected( false );
765 if (connection->OnDisconnect())
766 {
767 DDEDeleteConnection(hConv); // Delete mapping: hConv => connection
768 return (DDERETURN)(DWORD)true;
769 }
770 }
771 break;
772 }
773
774 case XTYP_EXECUTE:
775 {
776 wxDDEConnection *connection = DDEFindConnection(hConv);
777
778 if (connection)
779 {
780 DWORD len = DdeGetData(hData, NULL, 0, 0);
781
782 wxChar *data = connection->GetBufferAtLeast( len );
783 wxASSERT_MSG(data != NULL,
784 _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
785
786 DdeGetData(hData, (LPBYTE)data, len, 0);
787
788 DdeFreeDataHandle(hData);
789
790 // XTYP_EXECUTE cannot be used for arbitrary data, but only for text
791 if ( connection->OnExecute(connection->m_topicName,
792 data,
793 (int)len,
794 wxIPC_TEXT ) )
795 {
796 return (DDERETURN)(DWORD)DDE_FACK;
797 }
798 }
799
800 return (DDERETURN)DDE_FNOTPROCESSED;
801 }
802
803 case XTYP_REQUEST:
804 {
805 wxDDEConnection *connection = DDEFindConnection(hConv);
806
807 if (connection)
808 {
809 wxString item_name = DDEStringFromAtom(hsz2);
810
811 int user_size = -1;
812 wxChar *data = connection->OnRequest(connection->m_topicName,
813 item_name,
814 &user_size,
815 (wxIPCFormat) wFmt);
816 if (data)
817 {
818 if (user_size < 0)
819 user_size = (wxStrlen((wxChar*)data) + 1) * sizeof(wxChar); // includes final NUL
820
821 HDDEDATA handle = DdeCreateDataHandle(DDEIdInst,
822 (LPBYTE)data,
823 user_size,
824 0,
825 hsz2,
826 wFmt,
827 0);
828 return (DDERETURN)handle;
829 }
830 }
831 break;
832 }
833
834 case XTYP_POKE:
835 {
836 wxDDEConnection *connection = DDEFindConnection(hConv);
837
838 if (connection)
839 {
840 wxString item_name = DDEStringFromAtom(hsz2);
841
842 DWORD len = DdeGetData(hData, NULL, 0, 0);
843
844 wxChar *data = connection->GetBufferAtLeast( len );
845 wxASSERT_MSG(data != NULL,
846 _T("Buffer too small in _DDECallback (XTYP_POKE)") );
847
848 DdeGetData(hData, (LPBYTE)data, len, 0);
849
850 DdeFreeDataHandle(hData);
851
852 connection->OnPoke(connection->m_topicName,
853 item_name,
854 data,
855 (int)len,
856 (wxIPCFormat) wFmt);
857
858 return (DDERETURN)DDE_FACK;
859 }
860 else
861 {
862 return (DDERETURN)DDE_FNOTPROCESSED;
863 }
864 }
865
866 case XTYP_ADVSTART:
867 {
868 wxDDEConnection *connection = DDEFindConnection(hConv);
869
870 if (connection)
871 {
872 wxString item_name = DDEStringFromAtom(hsz2);
873
874 return (DDERETURN)connection->
875 OnStartAdvise(connection->m_topicName, item_name);
876 }
877
878 break;
879 }
880
881 case XTYP_ADVSTOP:
882 {
883 wxDDEConnection *connection = DDEFindConnection(hConv);
884
885 if (connection)
886 {
887 wxString item_name = DDEStringFromAtom(hsz2);
888
889 return (DDERETURN)connection->
890 OnStopAdvise(connection->m_topicName, item_name);
891 }
892
893 break;
894 }
895
896 case XTYP_ADVREQ:
897 {
898 wxDDEConnection *connection = DDEFindConnection(hConv);
899
900 if (connection && connection->m_sendingData)
901 {
902 HDDEDATA data = DdeCreateDataHandle
903 (
904 DDEIdInst,
905 (LPBYTE)connection->m_sendingData,
906 connection->m_dataSize,
907 0,
908 hsz2,
909 connection->m_dataType,
910 0
911 );
912
913 connection->m_sendingData = NULL;
914
915 return (DDERETURN)data;
916 }
917
918 break;
919 }
920
921 case XTYP_ADVDATA:
922 {
923 wxDDEConnection *connection = DDEFindConnection(hConv);
924
925 if (connection)
926 {
927 wxString item_name = DDEStringFromAtom(hsz2);
928
929 DWORD len = DdeGetData(hData, NULL, 0, 0);
930
931 wxChar *data = connection->GetBufferAtLeast( len );
932 wxASSERT_MSG(data != NULL,
933 _T("Buffer too small in _DDECallback (XTYP_ADVDATA)") );
934
935 DdeGetData(hData, (LPBYTE)data, len, 0);
936
937 DdeFreeDataHandle(hData);
938 if ( connection->OnAdvise(connection->m_topicName,
939 item_name,
940 data,
941 (int)len,
942 (wxIPCFormat) wFmt) )
943 {
944 return (DDERETURN)(DWORD)DDE_FACK;
945 }
946 }
947
948 return (DDERETURN)DDE_FNOTPROCESSED;
949 }
950 }
951
952 return (DDERETURN)0;
953 }
954
955 // ----------------------------------------------------------------------------
956 // DDE strings and atoms
957 // ----------------------------------------------------------------------------
958
959 // Atom table stuff
960 static HSZ DDEAddAtom(const wxString& str)
961 {
962 HSZ atom = DDEAtomFromString(str);
963 wxAtomTable[str] = atom;
964 return atom;
965 }
966
967 static HSZ DDEGetAtom(const wxString& str)
968 {
969 wxAtomMap::iterator it = wxAtomTable.find(str);
970
971 if (it != wxAtomTable.end())
972 return it->second;
973
974 return DDEAddAtom(str);
975 }
976
977 /* atom <-> strings
978 The returned handle has to be freed by the caller (using
979 (static) DDEFreeString).
980 */
981 static HSZ DDEAtomFromString(const wxString& s)
982 {
983 wxASSERT_MSG( DDEIdInst, _T("DDE not initialized") );
984
985 HSZ hsz = DdeCreateStringHandle(DDEIdInst, (wxChar*) s.c_str(), DDE_CP);
986 if ( !hsz )
987 {
988 DDELogError(_("Failed to create DDE string"));
989 }
990
991 return hsz;
992 }
993
994 static wxString DDEStringFromAtom(HSZ hsz)
995 {
996 // all DDE strings are normally limited to 255 bytes
997 static const size_t len = 256;
998
999 wxString s;
1000 (void)DdeQueryString(DDEIdInst, hsz, wxStringBuffer(s, len), len, DDE_CP);
1001
1002 return s;
1003 }
1004
1005 static void DDEFreeString(HSZ hsz)
1006 {
1007 // DS: Failure to free a string handle might indicate there's
1008 // some other severe error.
1009 bool ok = (::DdeFreeStringHandle(DDEIdInst, hsz) != 0);
1010 wxASSERT_MSG( ok, wxT("Failed to free DDE string handle") );
1011 wxUnusedVar(ok);
1012 }
1013
1014 // ----------------------------------------------------------------------------
1015 // error handling
1016 // ----------------------------------------------------------------------------
1017
1018 static void DDELogError(const wxString& s, UINT error)
1019 {
1020 if ( !error )
1021 {
1022 error = DdeGetLastError(DDEIdInst);
1023 }
1024
1025 wxLogError(s + _T(": ") + DDEGetErrorMsg(error));
1026 }
1027
1028 static wxString DDEGetErrorMsg(UINT error)
1029 {
1030 wxString err;
1031 switch ( error )
1032 {
1033 case DMLERR_NO_ERROR:
1034 err = _("no DDE error.");
1035 break;
1036
1037 case DMLERR_ADVACKTIMEOUT:
1038 err = _("a request for a synchronous advise transaction has timed out.");
1039 break;
1040 case DMLERR_BUSY:
1041 err = _("the response to the transaction caused the DDE_FBUSY bit to be set.");
1042 break;
1043 case DMLERR_DATAACKTIMEOUT:
1044 err = _("a request for a synchronous data transaction has timed out.");
1045 break;
1046 case DMLERR_DLL_NOT_INITIALIZED:
1047 err = _("a DDEML function was called without first calling the DdeInitialize function,\nor an invalid instance identifier\nwas passed to a DDEML function.");
1048 break;
1049 case DMLERR_DLL_USAGE:
1050 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.");
1051 break;
1052 case DMLERR_EXECACKTIMEOUT:
1053 err = _("a request for a synchronous execute transaction has timed out.");
1054 break;
1055 case DMLERR_INVALIDPARAMETER:
1056 err = _("a parameter failed to be validated by the DDEML.");
1057 break;
1058 case DMLERR_LOW_MEMORY:
1059 err = _("a DDEML application has created a prolonged race condition.");
1060 break;
1061 case DMLERR_MEMORY_ERROR:
1062 err = _("a memory allocation failed.");
1063 break;
1064 case DMLERR_NO_CONV_ESTABLISHED:
1065 err = _("a client's attempt to establish a conversation has failed.");
1066 break;
1067 case DMLERR_NOTPROCESSED:
1068 err = _("a transaction failed.");
1069 break;
1070 case DMLERR_POKEACKTIMEOUT:
1071 err = _("a request for a synchronous poke transaction has timed out.");
1072 break;
1073 case DMLERR_POSTMSG_FAILED:
1074 err = _("an internal call to the PostMessage function has failed. ");
1075 break;
1076 case DMLERR_REENTRANCY:
1077 err = _("reentrancy problem.");
1078 break;
1079 case DMLERR_SERVER_DIED:
1080 err = _("a server-side transaction was attempted on a conversation\nthat was terminated by the client, or the server\nterminated before completing a transaction.");
1081 break;
1082 case DMLERR_SYS_ERROR:
1083 err = _("an internal error has occurred in the DDEML.");
1084 break;
1085 case DMLERR_UNADVACKTIMEOUT:
1086 err = _("a request to end an advise transaction has timed out.");
1087 break;
1088 case DMLERR_UNFOUND_QUEUE_ID:
1089 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.");
1090 break;
1091 default:
1092 err.Printf(_("Unknown DDE error %08x"), error);
1093 }
1094
1095 return err;
1096 }
1097
1098 #endif
1099 // wxUSE_IPC