MCPcopy Create free account
hub / github.com/ActiveState/code / DDEClient

Class DDEClient

recipes/Python/577654_DDE_Client/recipe-577654.py:126–218  ·  view source on GitHub ↗

The DDEClient class. Use this class to create and manage a connection to a service/topic. To get classbacks subclass DDEClient and overwrite callback.

Source from the content-addressed store, hash-verified

124 RuntimeError.__init__(self, "%s (err=%s)" % (msg, hex(DDE.GetLastError(idInst))))
125
126class DDEClient(object):
127 """The DDEClient class.
128
129 Use this class to create and manage a connection to a service/topic. To get
130 classbacks subclass DDEClient and overwrite callback."""
131
132 def __init__(self, service, topic):
133 """Create a connection to a service/topic."""
134 from ctypes import byref
135
136 self._idInst = DWORD(0)
137 self._hConv = HCONV()
138
139 self._callback = DDECALLBACK(self._callback)
140 res = DDE.Initialize(byref(self._idInst), self._callback, 0x00000010, 0)
141 if res != DMLERR_NO_ERROR:
142 raise DDEError("Unable to register with DDEML (err=%s)" % hex(res))
143
144 hszService = DDE.CreateStringHandle(self._idInst, service, 1200)
145 hszTopic = DDE.CreateStringHandle(self._idInst, topic, 1200)
146 self._hConv = DDE.Connect(self._idInst, hszService, hszTopic, PCONVCONTEXT())
147 DDE.FreeStringHandle(self._idInst, hszTopic)
148 DDE.FreeStringHandle(self._idInst, hszService)
149 if not self._hConv:
150 raise DDEError("Unable to establish a conversation with server", self._idInst)
151
152 def __del__(self):
153 """Cleanup any active connections."""
154 if self._hConv:
155 DDE.Disconnect(self._hConv)
156 if self._idInst:
157 DDE.Uninitialize(self._idInst)
158
159 def advise(self, item, stop=False):
160 """Request updates when DDE data changes."""
161 from ctypes import byref
162
163 hszItem = DDE.CreateStringHandle(self._idInst, item, 1200)
164 hDdeData = DDE.ClientTransaction(LPBYTE(), 0, self._hConv, hszItem, CF_TEXT, XTYP_ADVSTOP if stop else XTYP_ADVSTART, TIMEOUT_ASYNC, LPDWORD())
165 DDE.FreeStringHandle(self._idInst, hszItem)
166 if not hDdeData:
167 raise DDEError("Unable to %s advise" % ("stop" if stop else "start"), self._idInst)
168 DDE.FreeDataHandle(hDdeData)
169
170 def execute(self, command, timeout=5000):
171 """Execute a DDE command."""
172 pData = c_char_p(command)
173 cbData = DWORD(len(command) + 1)
174 hDdeData = DDE.ClientTransaction(pData, cbData, self._hConv, HSZ(), CF_TEXT, XTYP_EXECUTE, timeout, LPDWORD())
175 if not hDdeData:
176 raise DDEError("Unable to send command", self._idInst)
177 DDE.FreeDataHandle(hDdeData)
178
179 def request(self, item, timeout=5000):
180 """Request data from DDE service."""
181 from ctypes import byref
182
183 hszItem = DDE.CreateStringHandle(self._idInst, item, 1200)

Callers 1

recipe-577654.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected