]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dde.cpp
Fix for probably rare but potential refcount leak.
[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 #ifdef __GNUWIN32_OLD__
49 #include "wx/msw/gnuwin32/extra.h"
50 #endif
51
52 // ----------------------------------------------------------------------------
53 // macros and constants
54 // ----------------------------------------------------------------------------
55
56 #ifdef __WIN32__
57 #define _EXPORT
58 #else
59 #define _EXPORT _export
60 #endif
61
62 #if wxUSE_UNICODE
63 #define DDE_CP CP_WINUNICODE
64 #else
65 #define DDE_CP CP_WINANSI
66 #endif
67
68 #define GetHConv() ((HCONV)m_hConv)
69
70 // default timeout for DDE operations (5sec)
71 #define DDE_TIMEOUT 5000
72
73 // ----------------------------------------------------------------------------
74 // private functions
75 // ----------------------------------------------------------------------------
76
77 static wxDDEConnection *DDEFindConnection(HCONV hConv);
78 static void DDEDeleteConnection(HCONV hConv);
79 static wxDDEServer *DDEFindServer(const wxString& s);
80
81 extern "C" HDDEDATA EXPENTRY _EXPORT _DDECallback(WORD wType,
82 WORD wFmt,
83 HCONV hConv,
84 HSZ hsz1,
85 HSZ hsz2,
86 HDDEDATA hData,
87 DWORD lData1,
88 DWORD lData2);
89
90 // Add topic name to atom table before using in conversations
91 static HSZ DDEAddAtom(const wxString& string);
92 static HSZ DDEGetAtom(const wxString& string);
93
94 // string handles
95 static HSZ DDEAtomFromString(const wxString& s);
96 static wxString DDEStringFromAtom(HSZ hsz);
97 static void DDEFreeString(HSZ hsz);
98
99 // error handling
100 static wxString DDEGetErrorMsg(UINT error);
101 static void DDELogError(const wxString& s, UINT error = DMLERR_NO_ERROR);
102
103 // ----------------------------------------------------------------------------
104 // global variables
105 // ----------------------------------------------------------------------------
106
107 WX_DECLARE_STRING_HASH_MAP( HSZ, wxAtomMap );
108
109 static DWORD DDEIdInst = 0L;
110 static wxDDEConnection *DDECurrentlyConnecting = NULL;
111 static wxAtomMap wxAtomTable;
112
113 #include "wx/listimpl.cpp"
114
115 WX_DEFINE_LIST(wxDDEClientList);
116 WX_DEFINE_LIST(wxDDEServerList);
117 WX_DEFINE_LIST(wxDDEConnectionList);
118
119 static wxDDEClientList wxDDEClientObjects;
120 static wxDDEServerList wxDDEServerObjects;
121
122 static bool DDEInitialized = false;
123
124 // ----------------------------------------------------------------------------
125 // private classes
126 // ----------------------------------------------------------------------------
127
128 // A module to allow DDE cleanup without calling these functions
129 // from app.cpp or from the user's application.
130
131 class wxDDEModule : public wxModule
132 {
133 public:
134 wxDDEModule() {}
135 bool OnInit() { return true; }
136 void OnExit() { wxDDECleanUp(); }
137
138 private:
139 DECLARE_DYNAMIC_CLASS(wxDDEModule)
140 };
141
142 // ----------------------------------------------------------------------------
143 // wxWin macros
144 // ----------------------------------------------------------------------------
145
146 IMPLEMENT_DYNAMIC_CLASS(wxDDEServer, wxServerBase)
147 IMPLEMENT_DYNAMIC_CLASS(wxDDEClient, wxClientBase)
148 IMPLEMENT_CLASS(wxDDEConnection, wxConnectionBase)
149 IMPLEMENT_DYNAMIC_CLASS(wxDDEModule, wxModule)
150
151 // ============================================================================
152 // implementation
153 // ============================================================================
154
155 // ----------------------------------------------------------------------------
156 // initialization and cleanup
157 // ----------------------------------------------------------------------------
158
159 extern void wxDDEInitialize()
160 {
161 if ( !DDEInitialized )
162 {
163 // Should insert filter flags
164 PFNCALLBACK callback = (PFNCALLBACK)
165 MakeProcInstance((FARPROC)_DDECallback, wxGetInstance());
166 UINT rc = DdeInitialize(&DDEIdInst, callback, APPCLASS_STANDARD, 0L);
167 if ( rc != DMLERR_NO_ERROR )
168 {
169 DDELogError(_T("Failed to initialize DDE"), rc);
170 }
171 else
172 {
173 DDEInitialized = true;
174 }
175 }
176 }
177
178 void wxDDECleanUp()
179 {
180 // deleting them later won't work as DDE won't be initialized any more
181 wxASSERT_MSG( wxDDEServerObjects.empty() &&
182 wxDDEClientObjects.empty(),
183 _T("all DDE objects should be deleted by now") );
184
185 wxAtomTable.clear();
186
187 if ( DDEIdInst != 0 )
188 {
189 DdeUninitialize(DDEIdInst);
190 DDEIdInst = 0;
191 }
192 }
193
194 // ----------------------------------------------------------------------------
195 // functions working with the global connection list(s)
196 // ----------------------------------------------------------------------------
197
198 // Global find connection
199 static wxDDEConnection *DDEFindConnection(HCONV hConv)
200 {
201 wxDDEServerList::compatibility_iterator serverNode = wxDDEServerObjects.GetFirst();
202 wxDDEConnection *found = NULL;
203 while (serverNode && !found)
204 {
205 wxDDEServer *object = serverNode->GetData();
206 found = object->FindConnection((WXHCONV) hConv);
207 serverNode = serverNode->GetNext();
208 }
209
210 if (found)
211 {
212 return found;
213 }
214
215 wxDDEClientList::compatibility_iterator clientNode = wxDDEClientObjects.GetFirst();
216 while (clientNode && !found)
217 {
218 wxDDEClient *object = clientNode->GetData();
219 found = object->FindConnection((WXHCONV) hConv);
220 clientNode = clientNode->GetNext();
221 }
222 return found;
223 }
224
225 // Global delete connection
226 static void DDEDeleteConnection(HCONV hConv)
227 {
228 wxDDEServerList::compatibility_iterator serverNode = wxDDEServerObjects.GetFirst();
229 bool found = false;
230 while (serverNode && !found)
231 {
232 wxDDEServer *object = serverNode->GetData();
233 found = object->DeleteConnection((WXHCONV) hConv);
234 serverNode = serverNode->GetNext();
235 }
236 if (found)
237 {
238 return;
239 }
240
241 wxDDEClientList::compatibility_iterator clientNode = wxDDEClientObjects.GetFirst();
242 while (clientNode && !found)
243 {
244 wxDDEClient *object = clientNode->GetData();
245 found = object->DeleteConnection((WXHCONV) hConv);
246 clientNode = clientNode->GetNext();
247 }
248 }
249
250 // Find a server from a service name
251 static wxDDEServer *DDEFindServer(const wxString& s)
252 {
253 wxDDEServerList::compatibility_iterator node = wxDDEServerObjects.GetFirst();
254 wxDDEServer *found = NULL;
255 while (node && !found)
256 {
257 wxDDEServer *object = node->GetData();
258
259 if (object->GetServiceName() == s)
260 {
261 found = object;
262 }
263 else
264 {
265 node = node->GetNext();
266 }
267 }
268
269 return found;
270 }
271
272 // ----------------------------------------------------------------------------
273 // wxDDEServer
274 // ----------------------------------------------------------------------------
275
276 wxDDEServer::wxDDEServer()
277 {
278 wxDDEInitialize();
279
280 wxDDEServerObjects.Append(this);
281 }
282
283 bool wxDDEServer::Create(const wxString& server)
284 {
285 m_serviceName = server;
286
287 HSZ hsz = DDEAtomFromString(server);
288
289 if ( !hsz )
290 {
291 return false;
292 }
293
294
295 bool success = (DdeNameService(DDEIdInst, hsz, (HSZ) NULL, DNS_REGISTER)
296 != NULL);
297
298 if (!success)
299 {
300 DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
301 server.c_str()));
302 }
303
304 DDEFreeString(hsz);
305
306 return success;
307 }
308
309 wxDDEServer::~wxDDEServer()
310 {
311 if ( !m_serviceName.IsEmpty() )
312 {
313 HSZ hsz = DDEAtomFromString(m_serviceName);
314
315 if (hsz)
316 {
317 if ( !DdeNameService(DDEIdInst, hsz,
318 (HSZ) NULL, DNS_UNREGISTER) )
319 {
320 DDELogError(wxString::Format(
321 _("Failed to unregister DDE server '%s'"),
322 m_serviceName.c_str()));
323 }
324
325 DDEFreeString(hsz);
326 }
327 }
328
329 wxDDEServerObjects.DeleteObject(this);
330
331 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
332 while (node)
333 {
334 wxDDEConnection *connection = node->GetData();
335 wxDDEConnectionList::compatibility_iterator next = node->GetNext();
336 connection->SetConnected(false);
337 connection->OnDisconnect(); // May delete the node implicitly
338 node = next;
339 }
340
341 // If any left after this, delete them
342 node = m_connections.GetFirst();
343 while (node)
344 {
345 wxDDEConnection *connection = node->GetData();
346 wxDDEConnectionList::compatibility_iterator next = node->GetNext();
347 delete connection;
348 node = next;
349 }
350 }
351
352 wxConnectionBase *wxDDEServer::OnAcceptConnection(const wxString& /* topic */)
353 {
354 return new wxDDEConnection;
355 }
356
357 wxDDEConnection *wxDDEServer::FindConnection(WXHCONV conv)
358 {
359 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
360 wxDDEConnection *found = NULL;
361 while (node && !found)
362 {
363 wxDDEConnection *connection = node->GetData();
364 if (connection->m_hConv == conv)
365 found = connection;
366 else node = node->GetNext();
367 }
368 return found;
369 }
370
371 // Only delete the entry in the map, not the actual connection
372 bool wxDDEServer::DeleteConnection(WXHCONV conv)
373 {
374 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
375 while (node)
376 {
377 wxDDEConnection *connection = node->GetData();
378 if (connection->m_hConv == conv)
379 {
380 m_connections.Erase(node);
381 return true;
382 }
383 else
384 {
385 node = node->GetNext();
386 }
387 }
388 return false;
389 }
390
391 // ----------------------------------------------------------------------------
392 // wxDDEClient
393 // ----------------------------------------------------------------------------
394
395 wxDDEClient::wxDDEClient()
396 {
397 wxDDEInitialize();
398
399 wxDDEClientObjects.Append(this);
400 }
401
402 wxDDEClient::~wxDDEClient()
403 {
404 wxDDEClientObjects.DeleteObject(this);
405 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
406 while (node)
407 {
408 wxDDEConnection *connection = node->GetData();
409 delete connection; // Deletes the node implicitly (see ~wxDDEConnection)
410 node = m_connections.GetFirst();
411 }
412 }
413
414 bool wxDDEClient::ValidHost(const wxString& /* host */)
415 {
416 return true;
417 }
418
419 wxConnectionBase *wxDDEClient::MakeConnection(const wxString& WXUNUSED(host),
420 const wxString& server,
421 const wxString& topic)
422 {
423 HSZ hszServer = DDEAtomFromString(server);
424
425 if ( !hszServer )
426 {
427 return (wxConnectionBase*) NULL;
428 }
429
430
431 HSZ hszTopic = DDEAtomFromString(topic);
432
433 if ( !hszTopic )
434 {
435 DDEFreeString(hszServer);
436 return (wxConnectionBase*) NULL;
437 }
438
439
440 HCONV hConv = ::DdeConnect(DDEIdInst, hszServer, hszTopic,
441 (PCONVCONTEXT) NULL);
442
443 DDEFreeString(hszServer);
444 DDEFreeString(hszTopic);
445
446
447 if ( !hConv )
448 {
449 DDELogError( wxString::Format(
450 _("Failed to create connection to server '%s' on topic '%s'"),
451 server.c_str(), topic.c_str()) );
452 }
453 else
454 {
455 wxDDEConnection *connection = (wxDDEConnection*) OnMakeConnection();
456 if (connection)
457 {
458 connection->m_hConv = (WXHCONV) hConv;
459 connection->m_topicName = topic;
460 connection->m_client = this;
461 m_connections.Append(connection);
462 return connection;
463 }
464 }
465
466 return (wxConnectionBase*) NULL;
467 }
468
469 wxConnectionBase *wxDDEClient::OnMakeConnection()
470 {
471 return new wxDDEConnection;
472 }
473
474 wxDDEConnection *wxDDEClient::FindConnection(WXHCONV conv)
475 {
476 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
477 wxDDEConnection *found = NULL;
478 while (node && !found)
479 {
480 wxDDEConnection *connection = node->GetData();
481 if (connection->m_hConv == conv)
482 found = connection;
483 else node = node->GetNext();
484 }
485 return found;
486 }
487
488 // Only delete the entry in the map, not the actual connection
489 bool wxDDEClient::DeleteConnection(WXHCONV conv)
490 {
491 wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
492 while (node)
493 {
494 wxDDEConnection *connection = node->GetData();
495 if (connection->m_hConv == conv)
496 {
497 m_connections.Erase(node);
498 return true;
499 }
500 else node = node->GetNext();
501 }
502 return false;
503 }
504
505 // ----------------------------------------------------------------------------
506 // wxDDEConnection
507 // ----------------------------------------------------------------------------
508
509 wxDDEConnection::wxDDEConnection(wxChar *buffer, int size)
510 : wxConnectionBase(buffer, size)
511 {
512 m_client = NULL;
513 m_server = NULL;
514
515 m_hConv = 0;
516 m_sendingData = NULL;
517 }
518
519 wxDDEConnection::wxDDEConnection()
520 : wxConnectionBase()
521 {
522 m_hConv = 0;
523 m_sendingData = NULL;
524 m_server = NULL;
525 m_client = NULL;
526 }
527
528 wxDDEConnection::~wxDDEConnection()
529 {
530 Disconnect();
531 if (m_server)
532 m_server->GetConnections().DeleteObject(this);
533 else
534 m_client->GetConnections().DeleteObject(this);
535 }
536
537 // Calls that CLIENT can make
538 bool wxDDEConnection::Disconnect()
539 {
540 if ( !GetConnected() )
541 return true;
542
543 DDEDeleteConnection(GetHConv());
544
545 bool ok = DdeDisconnect(GetHConv()) != 0;
546 if ( !ok )
547 {
548 DDELogError(_T("Failed to disconnect from DDE server gracefully"));
549 }
550
551 SetConnected( false ); // so we don't try and disconnect again
552
553 return ok;
554 }
555
556 bool wxDDEConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
557 {
558 DWORD result;
559 if (size < 0)
560 {
561 size = wxStrlen(data) + 1;
562 }
563
564 bool ok = DdeClientTransaction((LPBYTE)data,
565 size * sizeof(wxChar),
566 GetHConv(),
567 NULL,
568 format,
569 XTYP_EXECUTE,
570 DDE_TIMEOUT,
571 &result) != 0;
572
573 if ( !ok )
574 {
575 DDELogError(_T("DDE execute request failed"));
576 }
577
578 return ok;
579 }
580
581 wxChar *wxDDEConnection::Request(const wxString& item, int *size, wxIPCFormat format)
582 {
583 DWORD result;
584
585 HSZ atom = DDEGetAtom(item);
586
587 HDDEDATA returned_data = DdeClientTransaction(NULL, 0,
588 GetHConv(),
589 atom, format,
590 XTYP_REQUEST,
591 DDE_TIMEOUT,
592 &result);
593 if ( !returned_data )
594 {
595 DDELogError(_T("DDE data request failed"));
596
597 return NULL;
598 }
599
600 DWORD len = DdeGetData(returned_data, NULL, 0, 0);
601
602 wxChar *data = GetBufferAtLeast( len/sizeof(wxChar) );
603 wxASSERT_MSG(data != NULL,
604 _T("Buffer too small in wxDDEConnection::Request") );
605 (void) DdeGetData(returned_data, (LPBYTE)data, len, 0);
606
607 (void) DdeFreeDataHandle(returned_data);
608
609 if (size)
610 *size = (int)len/sizeof(wxChar);
611
612 return data;
613 }
614
615 bool wxDDEConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format)
616 {
617 DWORD result;
618 if (size < 0)
619 {
620 size = wxStrlen(data) + 1;
621 }
622
623 HSZ item_atom = DDEGetAtom(item);
624 bool ok = DdeClientTransaction((LPBYTE)data,
625 size * sizeof(wxChar),
626 GetHConv(),
627 item_atom, format,
628 XTYP_POKE,
629 DDE_TIMEOUT,
630 &result) != 0;
631 if ( !ok )
632 {
633 DDELogError(_("DDE poke request failed"));
634 }
635
636 return ok;
637 }
638
639 bool wxDDEConnection::StartAdvise(const wxString& item)
640 {
641 DWORD result;
642 HSZ atom = DDEGetAtom(item);
643
644 bool ok = DdeClientTransaction(NULL, 0,
645 GetHConv(),
646 atom, CF_TEXT,
647 XTYP_ADVSTART,
648 DDE_TIMEOUT,
649 &result) != 0;
650 if ( !ok )
651 {
652 DDELogError(_("Failed to establish an advise loop with DDE server"));
653 }
654
655 return ok;
656 }
657
658 bool wxDDEConnection::StopAdvise(const wxString& item)
659 {
660 DWORD result;
661 HSZ atom = DDEGetAtom(item);
662
663 bool ok = DdeClientTransaction(NULL, 0,
664 GetHConv(),
665 atom, CF_TEXT,
666 XTYP_ADVSTOP,
667 DDE_TIMEOUT,
668 &result) != 0;
669 if ( !ok )
670 {
671 DDELogError(_("Failed to terminate the advise loop with DDE server"));
672 }
673
674 return ok;
675 }
676
677 // Calls that SERVER can make
678 bool wxDDEConnection::Advise(const wxString& item,
679 wxChar *data,
680 int size,
681 wxIPCFormat format)
682 {
683 if (size < 0)
684 {
685 size = wxStrlen(data) + 1;
686 }
687
688 HSZ item_atom = DDEGetAtom(item);
689 HSZ topic_atom = DDEGetAtom(m_topicName);
690 m_sendingData = data; // mrf: potential for scope problems here?
691 m_dataSize = size;
692 m_dataType = format;
693
694 bool ok = DdePostAdvise(DDEIdInst, topic_atom, item_atom) != 0;
695 if ( !ok )
696 {
697 DDELogError(_("Failed to send DDE advise notification"));
698 }
699
700 return ok;
701 }
702
703 bool wxDDEConnection::OnDisconnect()
704 {
705 delete this;
706 return true;
707 }
708
709 // ----------------------------------------------------------------------------
710 // _DDECallback
711 // ----------------------------------------------------------------------------
712
713 #define DDERETURN HDDEDATA
714
715 HDDEDATA EXPENTRY _EXPORT
716 _DDECallback(WORD wType,
717 WORD wFmt,
718 HCONV hConv,
719 HSZ hsz1,
720 HSZ hsz2,
721 HDDEDATA hData,
722 DWORD WXUNUSED(lData1),
723 DWORD WXUNUSED(lData2))
724 {
725 switch (wType)
726 {
727 case XTYP_CONNECT:
728 {
729 wxString topic = DDEStringFromAtom(hsz1),
730 srv = DDEStringFromAtom(hsz2);
731 wxDDEServer *server = DDEFindServer(srv);
732 if (server)
733 {
734 wxDDEConnection *connection =
735 (wxDDEConnection*) server->OnAcceptConnection(topic);
736 if (connection)
737 {
738 connection->m_server = server;
739 server->GetConnections().Append(connection);
740 connection->m_hConv = 0;
741 connection->m_topicName = topic;
742 DDECurrentlyConnecting = connection;
743 return (DDERETURN)(DWORD)true;
744 }
745 }
746 break;
747 }
748
749 case XTYP_CONNECT_CONFIRM:
750 {
751 if (DDECurrentlyConnecting)
752 {
753 DDECurrentlyConnecting->m_hConv = (WXHCONV) hConv;
754 DDECurrentlyConnecting = NULL;
755 return (DDERETURN)(DWORD)true;
756 }
757 break;
758 }
759
760 case XTYP_DISCONNECT:
761 {
762 wxDDEConnection *connection = DDEFindConnection(hConv);
763 if (connection)
764 {
765 connection->SetConnected( false );
766 if (connection->OnDisconnect())
767 {
768 DDEDeleteConnection(hConv); // Delete mapping: hConv => connection
769 return (DDERETURN)(DWORD)true;
770 }
771 }
772 break;
773 }
774
775 case XTYP_EXECUTE:
776 {
777 wxDDEConnection *connection = DDEFindConnection(hConv);
778
779 if (connection)
780 {
781 DWORD len = DdeGetData(hData, NULL, 0, 0);
782
783 wxChar *data = connection->GetBufferAtLeast( len/sizeof(wxChar) );
784 wxASSERT_MSG(data != NULL,
785 _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
786
787 DdeGetData(hData, (LPBYTE)data, len, 0);
788
789 DdeFreeDataHandle(hData);
790
791 if ( connection->OnExecute(connection->m_topicName,
792 data,
793 (int)len/sizeof(wxChar),
794 (wxIPCFormat) wFmt) )
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;
820
821 HDDEDATA handle = DdeCreateDataHandle(DDEIdInst,
822 (LPBYTE)data,
823 user_size*sizeof(wxChar),
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/sizeof(wxChar) );
845 wxASSERT_MSG(data != NULL,
846 _T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
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/sizeof(wxChar),
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*sizeof(wxChar),
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/sizeof(wxChar) );
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/sizeof(wxChar),
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