none
Catch DllImport exception on missing dll? RRS feed

  • Question

  • Hi,

    Is it possible to trap a missing dll exception when using [DllImport] in C#?

    My code is like this:

     

     public partial class Form1 : Form
     {
     [DllImport("rapi.dll")]
     public static extern void CeRapiInitEx(ref RAPIINIT pRapiInit);
    
     [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
     public static extern Int32 CeRapiUninit();
    
     [StructLayout(LayoutKind.Sequential)]
     public struct RAPIINIT
     {
      public int cbsize;
      public IntPtr heRapiInit;
      public UInt32 hrRapiInit;
     };
    

     

    It's trying to import RAPI.DLL from ActiveSync, but on a machine without ActiveSync the app will throw an exception.

    I would like to trap the exception.

     

    Thanks,

    Michael.

    Wednesday, May 26, 2010 5:47 PM

Answers

  • Use a try/catch block to catch the DllNotFoundException exception.
    • Marked as answer by SamAgain Thursday, May 27, 2010 10:00 AM
    Wednesday, May 26, 2010 9:49 PM
  • Hi,

       Anon got the point, here is the sample code:

    using System;
    using System.Runtime.InteropServices;
    
    namespace Trap_DLL_Missing_Exception
    {
      class Program
      {
        static void Main(string[] args)
        {
          try
          {
            Int32 result = CeRapiUninit();
          }
          catch (DllNotFoundException e)
          {
            Console.WriteLine(e.Message);
          }
        }
    
        [DllImport("non-exsist.dll")]
        public static extern void CeRapiInitEx(ref RAPIINIT pRapiInit);
    
        [DllImport("non-exsist.dll", CharSet = CharSet.Unicode)]
        public static extern Int32 CeRapiUninit();
    
        [StructLayout(LayoutKind.Sequential)]
        public struct RAPIINIT
        {
          public int cbsize;
          public IntPtr heRapiInit;
          public UInt32 hrRapiInit;
        };
      }
    }
    

    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by mpgjunky1 Thursday, May 27, 2010 6:20 AM
    Thursday, May 27, 2010 2:37 AM

All replies

  • Use a try/catch block to catch the DllNotFoundException exception.
    • Marked as answer by SamAgain Thursday, May 27, 2010 10:00 AM
    Wednesday, May 26, 2010 9:49 PM
  • Hi,

       Anon got the point, here is the sample code:

    using System;
    using System.Runtime.InteropServices;
    
    namespace Trap_DLL_Missing_Exception
    {
      class Program
      {
        static void Main(string[] args)
        {
          try
          {
            Int32 result = CeRapiUninit();
          }
          catch (DllNotFoundException e)
          {
            Console.WriteLine(e.Message);
          }
        }
    
        [DllImport("non-exsist.dll")]
        public static extern void CeRapiInitEx(ref RAPIINIT pRapiInit);
    
        [DllImport("non-exsist.dll", CharSet = CharSet.Unicode)]
        public static extern Int32 CeRapiUninit();
    
        [StructLayout(LayoutKind.Sequential)]
        public struct RAPIINIT
        {
          public int cbsize;
          public IntPtr heRapiInit;
          public UInt32 hrRapiInit;
        };
      }
    }
    

    Please mark the right answer at right time.
    Thanks,
    Sam
    • Marked as answer by mpgjunky1 Thursday, May 27, 2010 6:20 AM
    Thursday, May 27, 2010 2:37 AM
  • Guys,

    Many thanks, and allow me to apologize for my stupidity. I actually had a try/catch block, but forgot about it.

    Additionally, for some (weird) reason, I kept thinking the exception message which I was showing using MessageBox.Show(ex.Message) was being generated by some part of the application that was out of my reach.

    I'll chalk it down to being overstressed and overworked. :)

     

    Regards,

    Michael.

    Thursday, May 27, 2010 6:23 AM