public static IntPtr Register(int instanceId, string service) { lock (_Table) { // Create a string handle for the service name. IntPtr serviceHandle = Ddeml.DdeCreateStringHandle(instanceId, service, Ddeml.CP_WINANSI); // Register the service name. IntPtr result = Ddeml.DdeNameService(instanceId, serviceHandle, IntPtr.Zero, Ddeml.DNS_REGISTER); if (result != IntPtr.Zero) { // Make sure this thread has an IMessageFilter on it. LocalDataStoreSlot slot = Thread.GetNamedDataSlot(DataSlot); if (Thread.GetData(slot) == null) { RegistrationManager filter = new RegistrationManager(); Application.AddMessageFilter(filter); Thread.SetData(slot, filter); } // Add an entry to the table that maps the service handle to the current thread. _Table.Add(serviceHandle, Ddeml.GetCurrentThreadId()); } else { // Free the string handle created earlier. Ddeml.DdeFreeStringHandle(instanceId, serviceHandle); serviceHandle = IntPtr.Zero; } return serviceHandle; } }
public virtual void Advise(string topic, string item) { if (IsDisposed) throw new ObjectDisposedException(GetType().ToString()); if (!IsRegistered) throw new InvalidOperationException(Resources.NotRegisteredMessage); if (topic == null) throw new ArgumentNullException("topic"); if (topic.Length > Ddeml.MAX_STRING_SIZE) throw new ArgumentException(Resources.StringParameterInvalidMessage, "topic"); if (item == null) throw new ArgumentNullException("item"); if (item.Length > Ddeml.MAX_STRING_SIZE) throw new ArgumentException(Resources.StringParameterInvalidMessage, "item"); // Assume the topic name and item name are wild. var topicHandle = IntPtr.Zero; var itemHandle = IntPtr.Zero; // Create a string handle for the topic name if it is not wild. if (topic != "*") topicHandle = Ddeml.DdeCreateStringHandle(_InstanceId, topic, Ddeml.CP_WINANSI); // Create a string handle for the item name if it is not wild. if (item != "*") itemHandle = Ddeml.DdeCreateStringHandle(_InstanceId, item, Ddeml.CP_WINANSI); // Post an advise notification. This will cause an XTYP_ADVREQ transaction for each conversation. bool result = Ddeml.DdePostAdvise(_InstanceId, topicHandle, itemHandle); // Free the string handles created earlier. Ddeml.DdeFreeStringHandle(_InstanceId, itemHandle); Ddeml.DdeFreeStringHandle(_InstanceId, topicHandle); // Check the result to see if the post failed. if (!result) { int error = Ddeml.DdeGetLastError(_InstanceId); string message = Resources.AdviseFailedMessage; message = message.Replace("${service}", _Service); message = message.Replace("${topic}", topic); message = message.Replace("${item}", item); throw new DdemlException(message, error); } }