Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to add a new ACM driver from a file
        /// </summary>
        /// <param name="driverFile">Full path of the .acm or dll file containing the driver</param>
        /// <returns>Handle to the driver</returns>
        public static AcmDriver AddLocalDriver(string driverFile)
        {
            IntPtr handle = NativeMethods.LoadLibrary(driverFile);

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("Failed to load driver file");
            }
            var driverProc = NativeMethods.GetProcAddress(handle, "DriverProc");

            if (driverProc == IntPtr.Zero)
            {
                NativeMethods.FreeLibrary(handle);
                throw new ArgumentException("Failed to discover DriverProc");
            }
            IntPtr driverHandle;
            var    result = AcmInterop.acmDriverAdd(out driverHandle,
                                                    handle, driverProc, 0, AcmDriverAddFlags.Function);

            if (result != MmResult.NoError)
            {
                NativeMethods.FreeLibrary(handle);
                throw new MmException(result, "acmDriverAdd");
            }
            var driver = new AcmDriver(driverHandle);

            // long name seems to be missing when we use acmDriverAdd
            if (string.IsNullOrEmpty(driver.details.longName))
            {
                driver.details.longName = "Local driver: " + Path.GetFileName(driverFile);
                driver.localDllHandle   = handle;
            }
            return(driver);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes a driver previously added using AddLocalDriver
        /// </summary>
        /// <param name="localDriver">Local driver to remove</param>
        public static void RemoveLocalDriver(AcmDriver localDriver)
        {
            if (localDriver.localDllHandle == IntPtr.Zero)
            {
                throw new ArgumentException("Please pass in the AcmDriver returned by the AddLocalDriver method");
            }
            var removeResult = AcmInterop.acmDriverRemove(localDriver.driverId, 0); // gets stored as a driver Id

            NativeMethods.FreeLibrary(localDriver.localDllHandle);
            MmException.Try(removeResult, "acmDriverRemove");
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Finds a Driver by its short name
 /// </summary>
 /// <param name="shortName">Short Name</param>
 /// <returns>The driver, or null if not found</returns>
 public static AcmDriver FindByShortName(string shortName)
 {
     foreach (AcmDriver driver in AcmDriver.EnumerateAcmDrivers())
     {
         if (driver.ShortName == shortName)
         {
             return(driver);
         }
     }
     return(null);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Removes a driver previously added using AddLocalDriver
 /// </summary>
 /// <param name="localDriver">Local driver to remove</param>
 public static void RemoveLocalDriver(AcmDriver localDriver)
 {
     if (localDriver.localDllHandle == IntPtr.Zero)
     {
         throw new ArgumentException("Please pass in the AcmDriver returned by the AddLocalDriver method");
     }
     var removeResult = AcmInterop.acmDriverRemove(localDriver.driverId, 0); // gets stored as a driver Id
     NativeMethods.FreeLibrary(localDriver.localDllHandle);
     MmException.Try(removeResult, "acmDriverRemove");
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Attempts to add a new ACM driver from a file
 /// </summary>
 /// <param name="driverFile">Full path of the .acm or dll file containing the driver</param>
 /// <returns>Handle to the driver</returns>
 public static AcmDriver AddLocalDriver(string driverFile)
 {
     IntPtr handle = NativeMethods.LoadLibrary(driverFile);
     if (handle == IntPtr.Zero)
     {
         throw new ArgumentException("Failed to load driver file");
     }
     var driverProc = NativeMethods.GetProcAddress(handle, "DriverProc");
     if (driverProc == IntPtr.Zero)
     {
         NativeMethods.FreeLibrary(handle);
         throw new ArgumentException("Failed to discover DriverProc");
     }
     IntPtr driverHandle;
     var result = AcmInterop.acmDriverAdd(out driverHandle,
         handle, driverProc, 0, AcmDriverAddFlags.Function);
     if (result != MmResult.NoError)
     {
         NativeMethods.FreeLibrary(handle);
         throw new MmException(result, "acmDriverAdd");
     }
     var driver = new AcmDriver(driverHandle);
     // long name seems to be missing when we use acmDriverAdd
     if (string.IsNullOrEmpty(driver.details.longName))
     {
         driver.details.longName = "Local driver: " + Path.GetFileName(driverFile);
         driver.localDllHandle = handle;
     }
     return driver;
 }