3

Recently, I was learning to make a form program Demo. After calling the DLL, it crashed and flashed back.

Below is the code:

`[DllImport("vspdctl.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern bool CreatePair(string comName1, string comName2);

public bool CreatePort(string com1,string com2)
        {
            string s = com1;
            string s2 = com2;
            bool IsCrateture=CreatePair(com1, com2);
            return IsCrateture;
        }`

`private void button1_Click(object sender, EventArgs e)
        {
                bool IsCrateSeccuss = GetVSPD.CreatePort(CreatePortName.Text, CreatePortName2.Text);
                if (IsCrateSeccuss)
                {
                    MessageBox.Show("OK");
                }
                else
                {
                    MessageBox.Show("Fail");
                }`

Reported this error, the error is as follows:

System.DllNotFoundException: Unable to load DLL 'vspdctl.dll': A dynamic link library (DLL) initialization routine failed.

Not sure what caused it

kiko yar
  • 45
  • 7
  • The code looking for the DLL can't find the DLL at run time. Like the executable and DLL are not in the same folder. or the DLL is not in the executable's searchable path/s – Steve Mar 15 '23 at 21:40
  • You might add some context like what OS, Webserver, build suite are you using. – Steve Mar 15 '23 at 21:42
  • It has been resolved, thank you all. The reason is that the DLL is not the same as my computer's VSPD version. – kiko yar Mar 16 '23 at 11:39

1 Answers1

1

The DLL file must be located in the current directory of the program or in the query path defined by the system (ie: the path set by Path in the system environment variable)

DllNotFoundException clearly stated: The exception that is thrown when a DLL specified in a DLL import cannot be found.

Try using DllImportAttribute to directly add the directory of the dll. Here is a reference example: How can I specify a [DllImport] path at runtime?

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21