Visual Studio C#WFP应用程序与与DLL文件的外部接口交互

发布于 2025-02-10 20:20:17 字数 281 浏览 2 评论 0原文

因此,我正在创建一个独立的模拟器,该模拟器在接口和PLC之间进行通信。我正在创建软件读取初始CSV配置文件,而不是从地址寄存器或线圈的接口请求值,而不是将值返回到接口。到目前为止,一切都很好,我的意思是我需要进行一些测试,但是问题在于与接口的通信实现。我有一个.dll的外部界面,当我脱离时,我可以毫无问题地上传.dll,问题是该接口没有被吸引。这是我的第一次,我已经对C#(NetBeans等)编程了一些程序,从来没有以WPF格式进行程序,并且不会让人忽视问题,请有人帮助我吗?

此致, 罗伯托·索萨(Roberto Sousa)。

So i'm creating a standalone simulator that does a communication between a interface and a PLC. I'm creating the software the reads a initial csv configuration file, than the interface does requests of values from a address register or coil and than i return the value to the interface. So far, so good, i mean i need to do some tests but the problem is with the implementation of the communication with the interface. I have one .dll of that external interface, when i debbug i can upload the .dll without problems, the problem is that the interface is not lanched. It's my first time, i already programed some programs with c# (NetBeans, etc), never did a program in wpf format and canno't understant the problem, someone to help me please ?

Best Regards,
Roberto Sousa.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白首有我共你 2025-02-17 20:20:17

因此,我为我的界面的.dll加载文件提供了此代码:

private void LoadDll(string DllAbsolutePath)
{
        if (File.Exists(DllAbsolutePath))
        {
            pDll = LoadLibrary(DllAbsolutePath);
            
            if (pDll == IntPtr.Zero)
            {
                var lasterror = Marshal.GetLastWin32Error();
                var innerEx = new Win32Exception(lasterror);
                innerEx.Data.Add("LastWin32Error", lasterror);
                _DllNotLoaded = true;
                _log.Error("Dll from SD application could not be loaded" + innerEx);
            }
            else
            {
                pSimuCreateContext = GetProcAddress(pDll, "SimuCreateContext");
                pSimuCreateWindow = GetProcAddress(pDll, "SimuCreateWindow");
                pSimuGetValue = GetProcAddress(pDll, "SimuGetValue");
                pSimuGetType = GetProcAddress(pDll, "SimuGetType");
                pSimuSetValue = GetProcAddress(pDll, "SimuSetValue");
                pSimuUseInteractiveEvents = GetProcAddress(pDll, "SimuUseInteractiveEvents");
                pSimuShowEventCursors = GetProcAddress(pDll, "SimuShowEventCursors");
                pSimuCycle = GetProcAddress(pDll, "SimuCycle");
                pSimuReset = GetProcAddress(pDll, "SimuReset");
                pSimuDeleteContext = GetProcAddress(pDll, "SimuDeleteContext");
                pSimuGetScreenWidth = GetProcAddress(pDll, "SimuGetScreenWidth");
                pSimuHasEncounteredErrors = GetProcAddress(pDll, "SimuHasEncounteredErrors");

                if (pSimuCreateContext == IntPtr.Zero ||
                    pSimuCreateWindow == IntPtr.Zero ||
                    pSimuGetValue == IntPtr.Zero ||
                    pSimuGetType == IntPtr.Zero ||
                    pSimuSetValue == IntPtr.Zero ||
                    pSimuUseInteractiveEvents == IntPtr.Zero ||
                    pSimuShowEventCursors == IntPtr.Zero ||
                    pSimuCycle == IntPtr.Zero ||
                    pSimuReset == IntPtr.Zero ||
                    pSimuDeleteContext == IntPtr.Zero ||
                    pSimuGetScreenWidth == IntPtr.Zero ||
                    pSimuHasEncounteredErrors == IntPtr.Zero)
                {
                    _log.Error("Not all functions from SD application could be loaded");
                    _DllNotLoaded = true;
                }
                else
                {
                    simuCreateContext = (SimuCreateContext)Marshal.GetDelegateForFunctionPointer(
                                                                                    pSimuCreateContext,
                                                                                    typeof(SimuCreateContext));

                    simuCreateWindow = (SimuCreateWindow)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuCreateWindow,
                                                                        typeof(SimuCreateWindow));

                    simuGetValue = (SimuGetValue)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuGetValue,
                                                                        typeof(SimuGetValue));

                    simuGetType = (SimuGetType)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuGetType,
                                                                        typeof(SimuGetType));

                    simuSetValue = (SimuSetValue)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuSetValue,
                                                                        typeof(SimuSetValue));

                    simuUseInteractiveEvents = (SimuUseInteractiveEvents)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuUseInteractiveEvents,
                                                                        typeof(SimuUseInteractiveEvents));

                    simuShowEventCursors = (SimuShowEventCursors)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuShowEventCursors,
                                                                        typeof(SimuShowEventCursors));

                    simuCycle = (SimuCycle)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuCycle,
                                                                        typeof(SimuCycle));

                    simuReset = (SimuReset)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuReset,
                                                                        typeof(SimuReset));

                    simuDeleteContext = (SimuDeleteContext)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuDeleteContext,
                                                                        typeof(SimuDeleteContext));

                    simuGetScreenWidth = (SimuGetScreenWidth)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuGetScreenWidth,
                                                                        typeof(SimuGetScreenWidth));

                    simuHasEncounteredErrors = (SimuHasEncounteredErrors)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuHasEncounteredErrors,
                                                                      typeof(SimuHasEncounteredErrors));
                }
            }
        }
        else
        {
            _DllNotLoaded = true;
            _log.Error("file does not exists: " +DllAbsolutePath);
        }

比在此部分启动窗口的那部分中,它只是“ bugged”的软件,而不是什么都没有启动...在我的主体中,我创建了这条代码行:

SDHost SDHostBox = new SDHost(this.AppBorder.ActualHeight, 
this.AppBorder.ActualWidth);
                this.AppBorder.Child = SDHostBox;
                if (SDHostBox != null)
                {
                    _gui.Start(SDHostBox.Handle);
                }

当代码到达_gui.start()时应转到:

public void Start(IntPtr ParentWindowPtr)
    {           
        try
        {
            //Load DLL functions
            LoadDll(_DllPath);

            if (!_DllNotLoaded)
            {
                //Load Context
                Context = simuCreateContext();
                // Activate interactive events (mouse)
                simuUseInteractiveEvents(Context, 1);
                // Deactivate internal mouse cursor
                simuShowEventCursors(Context, 0);
                //Create window
                WindowPtr = simuCreateWindow(Context, ParentWindowPtr);

                var watch = System.Diagnostics.Stopwatch.StartNew();
                IsRunning = IsWindow(WindowPtr);
                //start has been requested so it is not in stop
                _bStop = false;

                Initializing();

                while (IsRunning)
                {
                    if (_Reset)
                    {
                        simuReset(Context);
                        eventsQueue = new ConcurrentQueue<eventType>();
                        Reset = false;
                        InitializingAfterReset();
                    }
                    if (eventsQueue.Count >= 0)
                    {
                        ProcessEvent();
                    }
                    //Execute a cycle
                    simuCycle(Context);
                    //Check if new commands have been received
                    ReadVariablesValues();


                    watch.Stop();
                    lastTimeCycle = watch.ElapsedMilliseconds;
                    watch.Restart();
                    iterationNr++;

                    ManageTimeInfo();
                    //Check if windows is active and if any error has been encountered
                    var windowsIsActive = IsWindow(WindowPtr);
                    var SDAppOk = simuHasEncounteredErrors() == 0;

                    if (!windowsIsActive)
                    {
                        _log.Debug("SD application window has been closed");
                    }

                    if (!SDAppOk)
                    {
                        _log.Error("An error has been found in SD application, application will close");
                    }

                    IsRunning = windowsIsActive && SDAppOk && !_bStop;
                    Thread.Sleep(_TaskCycleTime);
                }
                simuDeleteContext(Context);
                FreeLibrary(pDll);
            }

        }
        catch (Exception ex)
        {
            _log.Error(ex.Message);
            _log.Debug(ex.StackTrace);
            IsRunning = false;
            simuDeleteContext(Context);
            FreeLibrary(pDll);
        }
    }

So i have this code for the .dll load file of my interface:

private void LoadDll(string DllAbsolutePath)
{
        if (File.Exists(DllAbsolutePath))
        {
            pDll = LoadLibrary(DllAbsolutePath);
            
            if (pDll == IntPtr.Zero)
            {
                var lasterror = Marshal.GetLastWin32Error();
                var innerEx = new Win32Exception(lasterror);
                innerEx.Data.Add("LastWin32Error", lasterror);
                _DllNotLoaded = true;
                _log.Error("Dll from SD application could not be loaded" + innerEx);
            }
            else
            {
                pSimuCreateContext = GetProcAddress(pDll, "SimuCreateContext");
                pSimuCreateWindow = GetProcAddress(pDll, "SimuCreateWindow");
                pSimuGetValue = GetProcAddress(pDll, "SimuGetValue");
                pSimuGetType = GetProcAddress(pDll, "SimuGetType");
                pSimuSetValue = GetProcAddress(pDll, "SimuSetValue");
                pSimuUseInteractiveEvents = GetProcAddress(pDll, "SimuUseInteractiveEvents");
                pSimuShowEventCursors = GetProcAddress(pDll, "SimuShowEventCursors");
                pSimuCycle = GetProcAddress(pDll, "SimuCycle");
                pSimuReset = GetProcAddress(pDll, "SimuReset");
                pSimuDeleteContext = GetProcAddress(pDll, "SimuDeleteContext");
                pSimuGetScreenWidth = GetProcAddress(pDll, "SimuGetScreenWidth");
                pSimuHasEncounteredErrors = GetProcAddress(pDll, "SimuHasEncounteredErrors");

                if (pSimuCreateContext == IntPtr.Zero ||
                    pSimuCreateWindow == IntPtr.Zero ||
                    pSimuGetValue == IntPtr.Zero ||
                    pSimuGetType == IntPtr.Zero ||
                    pSimuSetValue == IntPtr.Zero ||
                    pSimuUseInteractiveEvents == IntPtr.Zero ||
                    pSimuShowEventCursors == IntPtr.Zero ||
                    pSimuCycle == IntPtr.Zero ||
                    pSimuReset == IntPtr.Zero ||
                    pSimuDeleteContext == IntPtr.Zero ||
                    pSimuGetScreenWidth == IntPtr.Zero ||
                    pSimuHasEncounteredErrors == IntPtr.Zero)
                {
                    _log.Error("Not all functions from SD application could be loaded");
                    _DllNotLoaded = true;
                }
                else
                {
                    simuCreateContext = (SimuCreateContext)Marshal.GetDelegateForFunctionPointer(
                                                                                    pSimuCreateContext,
                                                                                    typeof(SimuCreateContext));

                    simuCreateWindow = (SimuCreateWindow)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuCreateWindow,
                                                                        typeof(SimuCreateWindow));

                    simuGetValue = (SimuGetValue)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuGetValue,
                                                                        typeof(SimuGetValue));

                    simuGetType = (SimuGetType)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuGetType,
                                                                        typeof(SimuGetType));

                    simuSetValue = (SimuSetValue)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuSetValue,
                                                                        typeof(SimuSetValue));

                    simuUseInteractiveEvents = (SimuUseInteractiveEvents)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuUseInteractiveEvents,
                                                                        typeof(SimuUseInteractiveEvents));

                    simuShowEventCursors = (SimuShowEventCursors)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuShowEventCursors,
                                                                        typeof(SimuShowEventCursors));

                    simuCycle = (SimuCycle)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuCycle,
                                                                        typeof(SimuCycle));

                    simuReset = (SimuReset)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuReset,
                                                                        typeof(SimuReset));

                    simuDeleteContext = (SimuDeleteContext)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuDeleteContext,
                                                                        typeof(SimuDeleteContext));

                    simuGetScreenWidth = (SimuGetScreenWidth)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuGetScreenWidth,
                                                                        typeof(SimuGetScreenWidth));

                    simuHasEncounteredErrors = (SimuHasEncounteredErrors)Marshal.GetDelegateForFunctionPointer(
                                                                        pSimuHasEncounteredErrors,
                                                                      typeof(SimuHasEncounteredErrors));
                }
            }
        }
        else
        {
            _DllNotLoaded = true;
            _log.Error("file does not exists: " +DllAbsolutePath);
        }

Than in this section, where it starts the window, the software it's simply "bugged" dosen't launch nothing... In my main i created this line of code :

SDHost SDHostBox = new SDHost(this.AppBorder.ActualHeight, 
this.AppBorder.ActualWidth);
                this.AppBorder.Child = SDHostBox;
                if (SDHostBox != null)
                {
                    _gui.Start(SDHostBox.Handle);
                }

When the code reaches the _gui.Start() should go to :

public void Start(IntPtr ParentWindowPtr)
    {           
        try
        {
            //Load DLL functions
            LoadDll(_DllPath);

            if (!_DllNotLoaded)
            {
                //Load Context
                Context = simuCreateContext();
                // Activate interactive events (mouse)
                simuUseInteractiveEvents(Context, 1);
                // Deactivate internal mouse cursor
                simuShowEventCursors(Context, 0);
                //Create window
                WindowPtr = simuCreateWindow(Context, ParentWindowPtr);

                var watch = System.Diagnostics.Stopwatch.StartNew();
                IsRunning = IsWindow(WindowPtr);
                //start has been requested so it is not in stop
                _bStop = false;

                Initializing();

                while (IsRunning)
                {
                    if (_Reset)
                    {
                        simuReset(Context);
                        eventsQueue = new ConcurrentQueue<eventType>();
                        Reset = false;
                        InitializingAfterReset();
                    }
                    if (eventsQueue.Count >= 0)
                    {
                        ProcessEvent();
                    }
                    //Execute a cycle
                    simuCycle(Context);
                    //Check if new commands have been received
                    ReadVariablesValues();


                    watch.Stop();
                    lastTimeCycle = watch.ElapsedMilliseconds;
                    watch.Restart();
                    iterationNr++;

                    ManageTimeInfo();
                    //Check if windows is active and if any error has been encountered
                    var windowsIsActive = IsWindow(WindowPtr);
                    var SDAppOk = simuHasEncounteredErrors() == 0;

                    if (!windowsIsActive)
                    {
                        _log.Debug("SD application window has been closed");
                    }

                    if (!SDAppOk)
                    {
                        _log.Error("An error has been found in SD application, application will close");
                    }

                    IsRunning = windowsIsActive && SDAppOk && !_bStop;
                    Thread.Sleep(_TaskCycleTime);
                }
                simuDeleteContext(Context);
                FreeLibrary(pDll);
            }

        }
        catch (Exception ex)
        {
            _log.Error(ex.Message);
            _log.Debug(ex.StackTrace);
            IsRunning = false;
            simuDeleteContext(Context);
            FreeLibrary(pDll);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文