Ejemplo n.º 1
0
            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;
                }
            }
Ejemplo n.º 2
0
            public static void Unregister(int instanceId, IntPtr serviceHandle)
            {
                // This method could be called by the GC finalizer thread.  If it is then a direct call to the DDEML will fail since the DDEML is 
                // thread specific.  A message will be posted to the DDEML thread instead.
                lock (_Table) 
                {
                    if (_Table.ContainsKey(serviceHandle))
                    {
                        // Determine if the current thread matches what is in the table.
                        int threadId = (int)_Table[serviceHandle];
                        if (threadId == Ddeml.GetCurrentThreadId())
                        {
                            // Unregister the service name.
                            Ddeml.DdeNameService(instanceId, serviceHandle, IntPtr.Zero, Ddeml.DNS_UNREGISTER);

                            // Free the service string handle.    
                            Ddeml.DdeFreeStringHandle(instanceId, serviceHandle);
                        }
                        else
                        {
                            // Post a message to the thread that needs to execute the Ddeml.DdeXXX methods.
                            PostThreadMessage(threadId, WM_APP + 3, new IntPtr(instanceId), serviceHandle);
                        }

                        // Remove the service handle from the table because it is no longer in use.
                        _Table.Remove(serviceHandle);
                    }
                }
            }
Ejemplo n.º 3
0
            bool IMessageFilter.PreFilterMessage(ref Message m)
            {
                if (m.Msg == WM_APP + 3)
                {
                    // Unregister the service name.
                    Ddeml.DdeNameService(m.WParam.ToInt32(), m.LParam, IntPtr.Zero, Ddeml.DNS_UNREGISTER);

                    // Free the service string handle.    
                    Ddeml.DdeFreeStringHandle(m.WParam.ToInt32(), m.LParam);
                }
                return false;
            }
Ejemplo n.º 4
0
                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);
                            }
                    }