示例#1
0
        bool InternalOpen(object form, string topicName)
        {
            bool       result = false;
            Connection conn;

            if (m_connections.TryGetValue(form, out conn))
            {
                return(true);
            }

            IntPtr hszService = DDEML.DdeCreateStringHandle(m_idInst, "Excel\0", DDEML.CP_WINANSI);
            IntPtr hszTopic   = DDEML.DdeCreateStringHandle(m_idInst, topicName, DDEML.CP_WINANSI);

            // Подключаемся к разделу
            IntPtr hConv = DDEML.DdeConnect(m_idInst, hszService, hszTopic, (IntPtr)null);

            if (hConv != IntPtr.Zero)
            {
                m_connections.TryAdd(form, new Connection(topicName, hConv));
                result = true;
            }

            DDEML.DdeFreeStringHandle(m_idInst, hszTopic);
            DDEML.DdeFreeStringHandle(m_idInst, hszService);
            return(result);
        }
示例#2
0
        public void Dispose()
        {
            if (m_idInst != 0)
            {
                m_terminate.Set();
                m_complete.Dispose();
                m_terminate.Dispose();

                foreach (var conn in m_connections.ToList())
                {
                    Connection c;
                    m_connections.TryRemove(conn.Key, out c);
                    if (conn.Key is IDDESender)
                    {
                        ((IDDESender)conn.Key).OnComplete(DDEAction.Close, null, null);
                    }
                    if (conn.Key is IDisposable)
                    {
                        ((IDisposable)conn.Key).Dispose();
                    }
                    DDEML.DdeDisconnect(conn.Value.Conv);
                }

                DDEML.DdeUninitialize(m_idInst);
            }
        }
示例#3
0
        void InternalPokeData(object form, string itemName, string strData)
        {
            Connection conn;

            if (m_connections.TryGetValue(form, out conn))
            {
                IntPtr  hszItem     = DDEML.DdeCreateStringHandle(m_idInst, itemName, DDEML.CP_WINANSI);
                byte [] data        = Encoding.GetBytes(strData);
                IntPtr  hszDat      = DDEML.DdeCreateDataHandle(m_idInst, data, data.Length, 0, hszItem, DDEML.CF_TEXT, 0);
                uint    returnFlags = 0;
                DDEML.DdeClientTransaction(hszDat, -1, conn.Conv, hszItem, DDEML.CF_TEXT, DDEML.XTYP_POKE, Timeout.Infinite, ref returnFlags);
                DDEML.DdeFreeStringHandle(m_idInst, hszItem);
            }
        }
示例#4
0
        void InternalClose(object form)
        {
            Connection conn;

            if (m_connections.TryGetValue(form, out conn))
            {
                DDEML.DdeDisconnect(conn.Conv);
                m_connections.TryRemove(form, out conn);

                IDDESender sender = form as IDDESender;
                if (sender != null)
                {
                    sender.OnComplete(DDEAction.Close, conn.Name, null);
                }
            }
        }
示例#5
0
        void InternalExecuteMacro(object form, string strText)
        {
            Connection conn;

            if (m_connections.TryGetValue(form, out conn))
            {
                uint   idErr  = 0;
                byte[] data   = Encoding.GetBytes(strText);
                IntPtr hszDat = DDEML.DdeCreateDataHandle(m_idInst, data, data.Length, 0, IntPtr.Zero, DDEML.CF_TEXT, 0);
                if (hszDat == IntPtr.Zero)
                {
                    idErr = DDEML.DdeGetLastError(m_idInst);
                }
                uint returnFlags = 0;
                DDEML.DdeClientTransaction(hszDat, -1, conn.Conv, IntPtr.Zero, DDEML.CF_TEXT, DDEML.XTYP_EXECUTE, Timeout.Infinite, ref returnFlags);
            }
        }
示例#6
0
        public ExcelDDEManager()
        {
            System   = new object();
            Encoding = Encoding.GetEncoding("windows-1251");

            m_connections = new ConcurrentDictionary <object, Connection>();
            m_commands    = new ConcurrentQueue <Command>();
            m_terminate   = new ManualResetEvent(false);
            m_complete    = new AutoResetEvent(false);

            // запуск потока экспорта DDE, все системные вызовы DDE должны делаться в контексте одного потока!
            m_threadForProcess = CoreHelper.GetThread(() =>
            {
                try
                {
                    // подключение и все дальнейшие команды dde должны выполнятся в одном и том же потоке!
                    m_DDECallBack = new DDEML.DDECallBackDelegate(DDECallBack);
                    DDEML.DdeInitialize(ref m_idInst, m_DDECallBack, DDEML.APPCMD_CLIENTONLY, 0);

                    do
                    {
                        RefreshData();
                    } while (!m_terminate.WaitOne(AppConfig.Common.ExcelExportRefreshDelay));
                }
                catch (Exception e)
                {
                    LogFileManager.AddError("DDE export", e);
                }
            }, true, "Excel DDE processing");
            m_threadForProcess.SetApartmentState(ApartmentState.STA);
            m_threadForProcess.Start();

            //DDECompletitionManager.Instance.Post(() =>
            //    {
            //        m_DDECallBack = new DDEML.DDECallBackDelegate(DDECallBack);
            //        DDEML.DdeInitialize(ref m_idInst, m_DDECallBack, DDEML.APPCLASS_STANDARD, 0);
            //    });
        }
示例#7
0
        public void InternalRequest(object form, string itemName, out byte[] data)
        {
            Connection conn;

            data = new byte[0];
            if (m_connections.TryGetValue(form, out conn))
            {
                IntPtr hszItem     = DDEML.DdeCreateStringHandle(m_idInst, itemName, DDEML.CP_WINANSI);
                uint   returnFlags = 0;
                IntPtr dataHandle  = DDEML.DdeClientTransaction(IntPtr.Zero, 0, conn.Conv, hszItem, DDEML.CF_TEXT, DDEML.XTYP_REQUEST, 1000, ref returnFlags);
                DDEML.DdeFreeStringHandle(m_idInst, hszItem);

                if (dataHandle != IntPtr.Zero)
                {
                    uint length = DDEML.DdeGetData(dataHandle, null, 0, 0);
                    data   = new byte[length];
                    length = DDEML.DdeGetData(dataHandle, data, (uint)data.Length, 0);

                    // Free the data handle created by the server.
                    DDEML.DdeFreeDataHandle(dataHandle);
                }
            }
        }