A solution to the abnormality of secondary development by calling the VSPD DLL.

I was studying recently and imagined making a C# program that implements a virtual serial port. Since I don't know how to write a driver, I followed my brother's method and called the DLL open interface of VSPD to implement it .

Extract the independent driver installation package from the VSPD virtual COM port installation package (virtual serial port development and EXE program decompression) http://blog.csdn.net/xxdddail/article/details/17163035

Because it was the first time to call a DLL, many errors occurred. I will share them with you here.

First of all, the VSPD dll cannot be referenced directly.

Otherwise, the following error message will appear.

The correct way is to put the DLL into the Debug folder in your program folder and let it be together with your exe file. Then right-click in the program and regenerate it.

 Then refer to the VSPD User Manual, and the corresponding interface (in Functions under Using serial port driver in your program), and write the corresponding call in C#. The following is the code. (Here is a demonstration of the call to create a serial port.)

[DllImport("vspdctl.dll")]//引用DLL
        public static extern bool CreatePair(string comName1, string comName2);

Then call this method. I am writing a form, so I write the method in a Button.

using static CreatePortDemo.VSPDClass;//注意引用你写接口的类

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool IsCrateSeccuss = CreatePair(CreatePortName.Text, CreatePortName2.Text);;//因为接口返回是一个bool值,用一个bool值存储起来是否创建成功
                if (IsCrateSeccuss)
                {
                    MessageBox.Show("创建成功");
                }
                else
                {
                    MessageBox.Show("失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            
            
            
        }

Then debug, and then you may have the following problem.

Or it does not respond for a long time, and no error is reported in the end, but it crashes and exits, and the output list shows: AccessViolationException .

What kind of anomaly is this? I didn't know it at first, because even try catch couldn't catch it, but I checked it later.

AccessViolationException: Attempt to read or write to protected memory. This usually indicates that other memory is corrupted .

So I began to wonder if my DLL was wrong. So I went online and downloaded a VSPD DLL again, and compared it with my own DLL. Hmm... the new download is more than 3000 kb, and mine is more than 100 kb .

It must be that my DLL is broken and cannot run anymore . So after I changed the dll and tested it again, no error was reported .

But just because there is no error - it does not mean it has been solved !

[DllImport("vspdctl.dll")]//引用DLL
        public static extern bool CreatePair(string comName1, string comName2);

 I used this function to create a virtual serial port, which will return true if the creation is successful and false if it fails.

I used if else to write a simple prompt. If true is received, it will prompt "success", otherwise it will prompt "failure".

The result is that during testing, it kept returning true, but the serial port was not created at all !

 This situation is like when I ask a naughty kid to do his homework, and he tells me "It's done" when he's finished, but he tells me "It's done" without even writing . Feeling sad and angry, I decided to take a nap.

When I woke up in the afternoon, I suddenly realized that the VSPD in the computer did not match the newly downloaded DLL version .

So I quickly downloaded the same version of VSPD and installed it. As a result, the test was successful .

 No wonder I can't find anyone on Baidu who has the same situation as me. faint!

Solution: Download a VSPD that matches the DLL. The DLL must match the VSPD version of your computer to successfully use the interface function.

 Reference materials ( also my sad journey ):

Exceptions that C# cannot catch, such as AccessViolationException - A Lone Ape - Blog Garden (cnblogs.com)

(1 message) C#: AccessViolationException: Attempt to read or write to protected memory. This usually indicates that other memory is corrupted. Solution Collection_Debris Ball’s Blog-CSDN Blog

(2 messages) Extracting the independent driver installation package from the VSPD virtual COM port installation package (virtual serial port development and EXE program decompression) __learn from time to time_'s blog - CSDN blog

(2 messages) C# failure to call third-party dll_BIGE_BIGGER's blog-CSDN blog

C# calls C++dll and reports error ACCESS VIOLATION-CSDN Community

(2 messages) Two ways to call the interface in c#_Ink Direct Blog-CSDN Blog_C# Calling the Interface

(2 messages) Calling dll in C#_yaked19's Blog-CSDN Blog_C# Calling dll

(2 messages) C# calls C++ dll, PInvoke problem and callback function throw exception access violation_Zhiyuanfang-Wind and Rain Blog-CSDN Blog

(2 messages) Reasons and solutions for crashing when calling a third-party DLL_Brown Lihuamao's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/aa989111337/article/details/126033160