Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code that extracts frames from a video file using calls to the Windows avifil32.dll. I am running Windows 10 64-bit and VS2019.

This has been working fine for years in .NET 4 and the AnyCPU platform. Upon changing to .NET 4.5 it raises an AccessViolationException on the call to AVIFileGetStream. If the platform is set to x86 then it works again. Both 32 and 64-bit versions of the DLL are installed and I would have assumed Windows would use the appropriate version and that the int and IntPtr types work on both platforms, so am I missing a simple fix for this?

What I have tried:

The following (simplified) code demonstrates the problem, i.e. it works in .NET 4 on either platform but only for x86 in .NET 4.5:

public class Test
{
    public int OpenStream(string fileName)
    {
        int aviFile = 0;
        NativeMethods.AVIFileInit();
        NativeMethods.AVIFileOpen(ref aviFile, fileName, NativeMethods.OpenReadOnly, 0);
        return NativeMethods.AVIFileGetStream(aviFile, out IntPtr aviStream, NativeMethods.StreamTypeVideo, 0);
    }

    private static class NativeMethods
    {
        public const int OpenReadOnly = 0;
        public const int StreamTypeVideo = 1935960438;

        [DllImport("avifil32.dll")]
        public static extern int AVIFileGetStream(int pfile, out IntPtr ppavi, int fccType, int lParam);

        [DllImport("avifil32.dll")]
        public static extern void AVIFileInit();

        [DllImport("avifil32.dll", PreserveSig = true)]
        public static extern int AVIFileOpen(ref int ppfile, string szFile, int uMode, int lpHandler);
    }
}
Posted
Updated 23-Nov-20 3:42am
Comments
[no name] 22-Nov-20 4:14am    
What's the return code on AVIFileOpent()? Not important?
Samuel Carradice 23-Nov-20 9:47am    
The AVIFileOpen method was working correctly so I ignored the return for the sake of simplifying the example. The problem was the use of "int" instead of "IntPtr".

It could be a missing interop/marshalling pinvoke problem as explained here:
c# - Generating a video from images with avimanager is throwing an error - Stack Overflow[^]
 
Share this answer
 
For those that follow, the problem was indeed an incorrect declaration of the external methods. Some of the parameters declared as "int" should be "IntPtr" (which is of course different for 32 and 64-bit platforms), so always check the original API documentation and don't just assume code you copy from these pages is correct! These specific methods should be:

[DllImport("avifil32.dll", PreserveSig = true)]
public static extern int AVIFileOpen(ref IntPtr ppfile, string szFile, uint uMode, IntPtr pclsidHandler);

[DllImport("avifil32.dll")]
public static extern int AVIFileGetStream(IntPtr pfile, out IntPtr ppavi, uint fccType, int lParam);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900