In general, dllimport is very convenient, but when an error occurs, the error is also very vague and not comprehensive.
For example, a third-party DLL was encountered before, and dllimport said that the DLL could not be found.
But I'm sure the error is in this DLL, and the path is correct.
No way, you can only make a VC call to try, and then the VC call gets the message:
Only then can we find that what we could not find was not a third-party DLL, but another DLL called by the third party, but C # could not get the message at all.
However, for future convenience, only a test function can be prepared to test the loading of third-party dll:
public static class DllTest { public static bool TestDll(string strDllPath) { try { var test = NativeMethods.LoadLibrary(strDllPath); MessageBox.Show(test.ToInt32().ToString()); return true; } catch (System.Exception ex) { MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace); return false; } } }
You can call this function before dllimport for testing!
If the returned value is not 0, it indicates normal.