public void TryConnect(string service, string topic, IDDEConversationListener listener) { var op = this.dispatcher.BeginInvoke(new Action(delegate() { if (idInst == 0) { listener.OnConnect(null); } using (var hszService = new DdeStringHandle(idInst, service)) using (var hszTopic = new DdeStringHandle(idInst, topic)) { var hConv = DDEML.DdeConnect(idInst, hszService.Handle, hszTopic.Handle, IntPtr.Zero); if (hConv == IntPtr.Zero) { var errno = DDEML.DdeGetLastError(idInst); var error = DDEML.GetErrorString(errno) + " (" + errno.ToString() + ")"; Log.Emit("dde-connect-failed", "service", service, "topic", topic, "error", error); listener.OnConnect(null); } else { Log.Emit("dde-connect", "service", service, "topic", topic, "hConv", hConv); var conv = new Conversation(this, service, topic, hConv, listener); this.conversations.Add(hConv, conv); listener.OnConnect(conv); } } })); }
void ThreadMain(object readyEvent) { var ddeCallback = new DDEML.DdeCallback(this.DdeCallback); try { dispatcher = Dispatcher.CurrentDispatcher; int res = DDEML.DdeInitialize(ref idInst, ddeCallback, DDEML.APPCMD_CLIENTONLY, 0); if (res != DDEML.DMLERR_NO_ERROR) { var err_no = DDEML.DdeGetLastError(idInst); string error = DDEML.GetErrorString(err_no) + " (" + err_no.ToString() + ")"; Log.Emit("dde-init-err", "error", error); InitializationMessage = error; idInst = 0; } else { Log.Emit("dde-init-ok", "id", idInst); InitializationMessage = "OK"; } (readyEvent as AutoResetEvent).Set(); Dispatcher.Run(); } catch (Exception e) { Log.Emit("dde-thread-exception", "exception", e); foreach (var kv in conversations) { try { kv.Value.Disconnect(); } catch (Exception e2) { Log.Emit("dde-thread-exception", "exception", e2); } } } GC.KeepAlive(ddeCallback); }
void Transact(string itemName, int wFmt, int wType, DDETransactionCallback callback) { Client.dispatcher.BeginInvoke(new Action(delegate() { if (Handle == IntPtr.Zero) { return; } int res = 0; using (var hsz = new DdeStringHandle(Client.idInst, itemName)) { using (var hData = new DdeDataHandle(DDEML.DdeClientTransaction(null, 0, Handle, hsz.Handle, wFmt, wType, DDEML.TIMEOUT_ASYNC, ref res))) { Log.Emit("dde-transact", "hConv", this.Handle, "item", itemName, "wFmt", wFmt, "wType", DDEML.GetTypeString(wType), "callback", callback != null, "id", res); if (hData.Handle == IntPtr.Zero) { var errno = DDEML.DdeGetLastError(Client.idInst); Log.Emit("dde-transact-failed", "error", DDEML.GetErrorString(errno) + " (" + errno.ToString() + ")"); if (callback != null) { callback(itemName, false, null); } this.Disconnect(); } else { if (callback != null) { if (res == 0) { callback(itemName, false, null); } else if (callback != null & res != 0) { var id = new IntPtr(res); transactions.Add(id, new Transaction(id, callback)); } } } } } })); }