📄 dde.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: msw/dde.cpp
// Purpose: DDE classes
// Author: Julian Smart
// Modified by:
// Created: 01/02/97
// RCS-ID: $Id: dde.cpp,v 1.48.2.2 2006/01/18 16:32:49 JS Exp $
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "dde.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_IPC
#ifndef WX_PRECOMP
#include "wx/utils.h"
#include "wx/app.h"
#endif
#include "wx/module.h"
#include "wx/dde.h"
#include "wx/intl.h"
#include "wx/hashmap.h"
#include "wx/msw/private.h"
#include <string.h>
#include <ddeml.h>
// ----------------------------------------------------------------------------
// macros and constants
// ----------------------------------------------------------------------------
#ifdef __WIN32__
#define _EXPORT
#else
#define _EXPORT _export
#endif
#if wxUSE_UNICODE
#define DDE_CP CP_WINUNICODE
#else
#define DDE_CP CP_WINANSI
#endif
#define GetHConv() ((HCONV)m_hConv)
// default timeout for DDE operations (5sec)
#define DDE_TIMEOUT 5000
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
static wxDDEConnection *DDEFindConnection(HCONV hConv);
static void DDEDeleteConnection(HCONV hConv);
static wxDDEServer *DDEFindServer(const wxString& s);
extern "C" HDDEDATA EXPENTRY _EXPORT _DDECallback(WORD wType,
WORD wFmt,
HCONV hConv,
HSZ hsz1,
HSZ hsz2,
HDDEDATA hData,
DWORD lData1,
DWORD lData2);
// Add topic name to atom table before using in conversations
static HSZ DDEAddAtom(const wxString& string);
static HSZ DDEGetAtom(const wxString& string);
// string handles
static HSZ DDEAtomFromString(const wxString& s);
static wxString DDEStringFromAtom(HSZ hsz);
static void DDEFreeString(HSZ hsz);
// error handling
static wxString DDEGetErrorMsg(UINT error);
static void DDELogError(const wxString& s, UINT error = DMLERR_NO_ERROR);
// ----------------------------------------------------------------------------
// global variables
// ----------------------------------------------------------------------------
WX_DECLARE_STRING_HASH_MAP( HSZ, wxAtomMap );
static DWORD DDEIdInst = 0L;
static wxDDEConnection *DDECurrentlyConnecting = NULL;
static wxAtomMap wxAtomTable;
#include "wx/listimpl.cpp"
WX_DEFINE_LIST(wxDDEClientList);
WX_DEFINE_LIST(wxDDEServerList);
WX_DEFINE_LIST(wxDDEConnectionList);
static wxDDEClientList wxDDEClientObjects;
static wxDDEServerList wxDDEServerObjects;
static bool DDEInitialized = false;
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
// A module to allow DDE cleanup without calling these functions
// from app.cpp or from the user's application.
class wxDDEModule : public wxModule
{
public:
wxDDEModule() {}
bool OnInit() { return true; }
void OnExit() { wxDDECleanUp(); }
private:
DECLARE_DYNAMIC_CLASS(wxDDEModule)
};
// ----------------------------------------------------------------------------
// wxWin macros
// ----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxDDEServer, wxServerBase)
IMPLEMENT_DYNAMIC_CLASS(wxDDEClient, wxClientBase)
IMPLEMENT_CLASS(wxDDEConnection, wxConnectionBase)
IMPLEMENT_DYNAMIC_CLASS(wxDDEModule, wxModule)
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// initialization and cleanup
// ----------------------------------------------------------------------------
extern void wxDDEInitialize()
{
if ( !DDEInitialized )
{
// Should insert filter flags
PFNCALLBACK callback = (PFNCALLBACK)
MakeProcInstance((FARPROC)_DDECallback, wxGetInstance());
UINT rc = DdeInitialize(&DDEIdInst, callback, APPCLASS_STANDARD, 0L);
if ( rc != DMLERR_NO_ERROR )
{
DDELogError(_T("Failed to initialize DDE"), rc);
}
else
{
DDEInitialized = true;
}
}
}
void wxDDECleanUp()
{
// deleting them later won't work as DDE won't be initialized any more
wxASSERT_MSG( wxDDEServerObjects.empty() &&
wxDDEClientObjects.empty(),
_T("all DDE objects should be deleted by now") );
wxAtomTable.clear();
if ( DDEIdInst != 0 )
{
DdeUninitialize(DDEIdInst);
DDEIdInst = 0;
}
}
// ----------------------------------------------------------------------------
// functions working with the global connection list(s)
// ----------------------------------------------------------------------------
// Global find connection
static wxDDEConnection *DDEFindConnection(HCONV hConv)
{
wxDDEServerList::compatibility_iterator serverNode = wxDDEServerObjects.GetFirst();
wxDDEConnection *found = NULL;
while (serverNode && !found)
{
wxDDEServer *object = serverNode->GetData();
found = object->FindConnection((WXHCONV) hConv);
serverNode = serverNode->GetNext();
}
if (found)
{
return found;
}
wxDDEClientList::compatibility_iterator clientNode = wxDDEClientObjects.GetFirst();
while (clientNode && !found)
{
wxDDEClient *object = clientNode->GetData();
found = object->FindConnection((WXHCONV) hConv);
clientNode = clientNode->GetNext();
}
return found;
}
// Global delete connection
static void DDEDeleteConnection(HCONV hConv)
{
wxDDEServerList::compatibility_iterator serverNode = wxDDEServerObjects.GetFirst();
bool found = false;
while (serverNode && !found)
{
wxDDEServer *object = serverNode->GetData();
found = object->DeleteConnection((WXHCONV) hConv);
serverNode = serverNode->GetNext();
}
if (found)
{
return;
}
wxDDEClientList::compatibility_iterator clientNode = wxDDEClientObjects.GetFirst();
while (clientNode && !found)
{
wxDDEClient *object = clientNode->GetData();
found = object->DeleteConnection((WXHCONV) hConv);
clientNode = clientNode->GetNext();
}
}
// Find a server from a service name
static wxDDEServer *DDEFindServer(const wxString& s)
{
wxDDEServerList::compatibility_iterator node = wxDDEServerObjects.GetFirst();
wxDDEServer *found = NULL;
while (node && !found)
{
wxDDEServer *object = node->GetData();
if (object->GetServiceName() == s)
{
found = object;
}
else
{
node = node->GetNext();
}
}
return found;
}
// ----------------------------------------------------------------------------
// wxDDEServer
// ----------------------------------------------------------------------------
wxDDEServer::wxDDEServer()
{
wxDDEInitialize();
wxDDEServerObjects.Append(this);
}
bool wxDDEServer::Create(const wxString& server)
{
m_serviceName = server;
HSZ hsz = DDEAtomFromString(server);
if ( !hsz )
{
return false;
}
bool success = (DdeNameService(DDEIdInst, hsz, (HSZ) NULL, DNS_REGISTER)
!= NULL);
if (!success)
{
DDELogError(wxString::Format(_("Failed to register DDE server '%s'"),
server.c_str()));
}
DDEFreeString(hsz);
return success;
}
wxDDEServer::~wxDDEServer()
{
if ( !m_serviceName.IsEmpty() )
{
HSZ hsz = DDEAtomFromString(m_serviceName);
if (hsz)
{
if ( !DdeNameService(DDEIdInst, hsz,
(HSZ) NULL, DNS_UNREGISTER) )
{
DDELogError(wxString::Format(
_("Failed to unregister DDE server '%s'"),
m_serviceName.c_str()));
}
DDEFreeString(hsz);
}
}
wxDDEServerObjects.DeleteObject(this);
wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
while (node)
{
wxDDEConnection *connection = node->GetData();
wxDDEConnectionList::compatibility_iterator next = node->GetNext();
connection->SetConnected(false);
connection->OnDisconnect(); // May delete the node implicitly
node = next;
}
// If any left after this, delete them
node = m_connections.GetFirst();
while (node)
{
wxDDEConnection *connection = node->GetData();
wxDDEConnectionList::compatibility_iterator next = node->GetNext();
delete connection;
node = next;
}
}
wxConnectionBase *wxDDEServer::OnAcceptConnection(const wxString& /* topic */)
{
return new wxDDEConnection;
}
wxDDEConnection *wxDDEServer::FindConnection(WXHCONV conv)
{
wxDDEConnectionList::compatibility_iterator node = m_connections.GetFirst();
wxDDEConnection *found = NULL;
while (node && !found)
{
wxDDEConnection *connection = node->GetData();
if (connection->m_hConv == conv)
found = connection;
else node = node->GetNext();
}
return found;
}
// Only delete the entry in the map, not the actual connection
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -