C# – WMEncoder Throws COMException (0x80004005) Unspecified Error

ccom

So I'm running this code

    public static void ConvertToWma(string inFile, string outFile, string profileName)
    {
        // Create a WMEncoder object.
        WMEncoder encoder = new WMEncoder();
        ManualResetEvent stopped = new ManualResetEvent(false);
        encoder.OnStateChange += delegate(WMENC_ENCODER_STATE enumState)
        {
            if (enumState == WMENC_ENCODER_STATE.WMENC_ENCODER_STOPPED)
                stopped.Set();
        };
        // Retrieve the source group collection.
        IWMEncSourceGroupCollection srcGrpColl = encoder.SourceGroupCollection;

        // Add a source group to the collection.
        IWMEncSourceGroup srcGrp = srcGrpColl.Add("SG_1");

        // Add an audio source to the source group.
        IWMEncSource srcAud = srcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
        srcAud.SetInput(inFile, "", "");

        // Specify a file object in which to save encoded content.
        IWMEncFile file = encoder.File;
        file.LocalFileName = outFile;

        // Choose a profile from the collection.
        IWMEncProfileCollection proColl = encoder.ProfileCollection;
        proColl.ProfileDirectory = AssemblyInformation.GetExecutingAssemblyDirectory();
        proColl.Refresh();
        IWMEncProfile pro;

        for (int i = 0; i < proColl.Count; i++)
        {
            pro = proColl.Item(i);
            if (pro.Name == profileName)
            {
                srcGrp.set_Profile(pro);
                break;
            }
        }
        // Start the encoding process.
        // Wait until the encoding process stops before exiting the application.
        encoder.SynchronizeOperation = false;
        encoder.PrepareToEncode(true);
        encoder.Start();
        stopped.WaitOne();
    }

And I get a COMException (0x80004005) when encoder.PrepareToEncode gets executed.

Some notes:

1) The process is spawned by an ASP.NET web service so it runs as NETWORK SERVICE
2) inFile and outFile are absolute local paths and their extensions are correct, in addition inFile definitely exists (this has been a source of problems in the past)
3) The program works when I run it as myself but doesn't work in the ASP.NET context.

This says to me its a security permission issue so in addition I've granted Full Control to the directory containing the program AND the directories containing the audio files to NETWORK SERVICE. So I really don't have any idea what more I can do on the security front. Any help?

Best Answer

Running WM Encoder SDK based app in windows service is not supported. It uses hidden windows for various reasons, and there isn't a desktop window in service. DRM would certainly fail with no user profile. Besides, even when you make your service talk to WME instance on a user's desktop, Microsoft only supports 4 concurrent requests per machine because the global lock in WME (I know, not pretty programming, but WME is old). For more scalable solutions, consider Windows Media Format SDK.

You may want to move your WM Encoder based app to Expression Encoder SDK as WM Encoder's support is ending.