public void Advise(string topic, string item) { if (IsDisposed) { throw new ObjectDisposedException(this.GetType().ToString()); } if (!IsRegistered) { throw new InvalidOperationException("NotRegisteredMessage"); } if (topic == null || topic.Length > Ddeml.MAX_STRING_SIZE) { throw new ArgumentNullException("topic"); } if (item == null || item.Length > Ddeml.MAX_STRING_SIZE) { throw new ArgumentNullException("item"); } // Assume the topic name and item name are wild. IntPtr topicHandle = topic != "*" ? Ddeml.DdeCreateStringHandle(_InstanceId, topic, Ddeml.CP_WINANSI) : IntPtr.Zero; IntPtr itemHandle = item != "*" ? Ddeml.DdeCreateStringHandle(_InstanceId, item, Ddeml.CP_WINANSI) : IntPtr.Zero; // Check the result to see if the post failed. if (!Ddeml.DdePostAdvise(_InstanceId, topicHandle, itemHandle)) { int error = Ddeml.DdeGetLastError(_InstanceId); var msg = Ddeml.DDEGetErrorMsg(error); if (msg != null) { } } Ddeml.DdeFreeStringHandle(_InstanceId, itemHandle); Ddeml.DdeFreeStringHandle(_InstanceId, topicHandle); }
public string Register(string service) { if (IsRegistered || _InstanceId != 0) { throw new InvalidOperationException("AlreadyRegisteredMessage"); } if (service == null || service.Length > Ddeml.MAX_STRING_SIZE) { throw new ArgumentNullException("service"); } _Service = service; _ConversationTable.Clear(); _InstanceId = Initialize(Ddeml.APPCLASS_STANDARD); _ServiceHandle = Ddeml.DdeCreateStringHandle(_InstanceId, _Service, Ddeml.CP_WINANSI); // Register the service name. if (Ddeml.DdeNameService(_InstanceId, _ServiceHandle, IntPtr.Zero, Ddeml.DNS_REGISTER) == IntPtr.Zero) { Ddeml.DdeFreeStringHandle(_InstanceId, _ServiceHandle); _ServiceHandle = IntPtr.Zero; } // If the service handle is null then the service name could not be registered. if (_ServiceHandle == IntPtr.Zero) { int error = Ddeml.DdeGetLastError(_InstanceId); return(Ddeml.DDEGetErrorMsg(error)); } return(null); }
// [10/24/2003] //主动请求Item的值 public unsafe string Request(string name) { IntPtr hszItem, hDDEData; int dwResult = 0; int dwLength = 0; if (name != null && hconvCurrent != IntPtr.Zero) { hszItem = Ddeml.DdeCreateStringHandle(dwDDEInst, name, Ddeml.CP_WINANSI); hDDEData = Ddeml.DdeClientTransaction(null, 0, hconvCurrent, hszItem, ConversionFormat.TEXT, //CF_TEXT, (int)Ddeml.XTYP_REQUEST, 5000, // ms timeout ref dwResult); Ddeml.DdeFreeStringHandle(dwDDEInst, hszItem); if (hDDEData != IntPtr.Zero) { sbyte *pData = (sbyte *)Ddeml.DdeAccessData(hDDEData, ref dwLength); { if ((pData != null) && (dwLength != 0)) { return(new string(pData, 0, dwLength)); } } } } return(null); }
public void Unregister() { Ddeml.DdeNameService(_InstanceId, _ServiceHandle, IntPtr.Zero, Ddeml.DNS_UNREGISTER); // Free the service string handle. Ddeml.DdeFreeStringHandle(_InstanceId, _ServiceHandle); // Indicate that the service name is no longer registered. _ServiceHandle = IntPtr.Zero; _InstanceId = 0; }
//与Server建立Hoot连接 public unsafe bool TransactItem(string name) { IntPtr hszItem, hDDEData; int dwResult = 0; if (string.IsNullOrEmpty(name)) { return(false); } if (hconvCurrent != IntPtr.Zero) { hszItem = Ddeml.DdeCreateStringHandle(dwDDEInst, name, Ddeml.CP_WINANSI); hDDEData = Ddeml.DdeClientTransaction(null, 0, hconvCurrent, hszItem, ConversionFormat.TEXT, //CF_TEXT, Ddeml.XTYP_ADVSTART, Ddeml.TIMEOUT_ASYNC, // ms timeout ref dwResult); Ddeml.DdeFreeStringHandle(dwDDEInst, hszItem); if (hDDEData != IntPtr.Zero) { try { int dwLength = 0; sbyte *pData = (sbyte *)Ddeml.DdeAccessData(hDDEData, ref dwLength); if ((pData != null) && (dwLength != 0)) { if (DDEValueChange != null) { DDEValueChange(this, new DDEValueChangeEventArgs(name, new string(pData, 0, dwLength))); } } } catch { } finally { Ddeml.DdeUnaccessData(hDDEData); } return(true); } else { return(false); } } else { return(false); } }
public unsafe bool Connect() { IntPtr hConv, hszService, hszTopic; if (dwDDEInst != 0) { Ddeml.DdeUninitialize(dwDDEInst); dwDDEInst = 0; } if (hconvCurrent != IntPtr.Zero) { Ddeml.DdeDisconnect(hconvCurrent); hconvCurrent = IntPtr.Zero; } ; //如果是第一次,则什么也不做 Ddeml.DdeInitialize(ref dwDDEInst, _Callback, 0x3f000, 0); // Connect to the server hszTopic = Ddeml.DdeCreateStringHandle(dwDDEInst, _topic, Ddeml.CP_WINANSI); hszService = Ddeml.DdeCreateStringHandle(dwDDEInst, _server, Ddeml.CP_WINANSI); CONVCONTEXT cc = new CONVCONTEXT(); cc.cb = sizeof(CONVCONTEXT); hConv = Ddeml.DdeConnect(dwDDEInst, hszService, hszTopic, ref cc); //int DdeErrcode = Win32.DdeGetLastError(dwDDEInst); Ddeml.DdeFreeStringHandle(dwDDEInst, hszTopic); Ddeml.DdeFreeStringHandle(dwDDEInst, hszService); if (hConv != IntPtr.Zero) { if (hconvCurrent != IntPtr.Zero) { Ddeml.DdeDisconnect(hconvCurrent); } hconvCurrent = hConv; _connected = true; return(true); } else { _connected = false; return(false); } }
//必须设置item.ItemName和item.ItemValue后调用 public int Poke(string name, string value) { IntPtr hszItem, hDDEData; int dwResult = 0; if (string.IsNullOrEmpty(name)) { return(-2); } if (hconvCurrent != IntPtr.Zero) { hszItem = Ddeml.DdeCreateStringHandle(dwDDEInst, name, Ddeml.CP_WINANSI); if (!string.IsNullOrEmpty(name)) { //int errcode=Win32.DdeGetLastError(dwDDEInst); byte[] ByteArray = Encoding.ASCII.GetBytes(value); IntPtr hd = Ddeml.DdeCreateDataHandle(dwDDEInst, ByteArray, ByteArray.Length, 0, hszItem, ConversionFormat.TEXT, 0); hDDEData = Ddeml.DdeClientTransaction( ByteArray, ByteArray.Length, hconvCurrent, hszItem, ConversionFormat.TEXT, //CF_TEXT, Ddeml.XTYP_POKE, Ddeml.TIMEOUT_ASYNC, // ms timeout ref dwResult); Ddeml.DdeFreeStringHandle(dwDDEInst, hszItem); if (hDDEData != IntPtr.Zero) { return(1); } } return(-1); } else { return(-1); } }
// Arrête une transaction Advise. public void AdvStop(IntPtr hConv, string item, ConversionFormat wFormat) { if (!IsRegistered) { return; } // Création de la chaîne DDE de l'élément. IntPtr hszItem = Ddeml.DdeCreateStringHandle(_InstanceId, item, Ddeml.CP_WINANSI); if ((hszItem == IntPtr.Zero) && (item.Length != 0)) { return; } // Exécution de la transaction. int res = 0; Ddeml.DdeClientTransaction(null, 0, hConv, hszItem, wFormat, Ddeml.XTYP_ADVSTOP, Ddeml.TIMEOUT_ASYNC, ref res); // Libération de la chaîne DDE. if (hszItem != IntPtr.Zero) { Ddeml.DdeFreeStringHandle(_InstanceId, hszItem); } }
// Démarre une transaction Advise. public bool AdvStart(IntPtr hConv, string item, ConversionFormat wFormat, int wFlag) { if (!IsRegistered) { return(false); } int res = 0; // Création de la chaîne DDE de l'élément. IntPtr hszItem = Ddeml.DdeCreateStringHandle(_InstanceId, item, Ddeml.CP_WINANSI); if ((hszItem == IntPtr.Zero) && (item.Length != 0)) { return(false); } // Exécution de la transaction. Ddeml.DdeClientTransaction(null, 0, hConv, hszItem, wFormat, (wFlag == Ddeml.XTYPF_ACKREQ) ? (Ddeml.XTYP_ADVSTARTACKREQ) : ((wFlag == Ddeml.XTYPF_NODATA) ? (Ddeml.XTYP_ADVSTARTNODATA) : (Ddeml.XTYP_ADVSTART)), Ddeml.TIMEOUT_ASYNC, ref res); // Libération de la chaîne DDE. if (hszItem != IntPtr.Zero) { Ddeml.DdeFreeStringHandle(_InstanceId, hszItem); } return(res != 0); }