C # dllimport error handling deficiencies and Solutions

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.