1

I'm making a C# Windows Forms program in Visual Studio which keeps crashing whenever I set the URL to a windows media player axWindowsMediaPLayer object.

There are two form windows: MainWindow and VideoForm. There are buttons on MainWindow to select a file via a fileDialog, and a button to open the VideoForm window which has the media player in it. I created a custom Video.cs class to capture the video file information. It also handles the fileDialog. However, when I set the URL to the media player object, the program crashes with code 3221225477 (0xc0000005) 'Access violation'. So, the URL of the media player in the VideoForm window is being set from the button click event in the MainWindow. I'm not sure if this is what's causing the access violation. I have run Visual Studio as an administrator to ensure that it should have access to the file. I have checked the file path and it is correct. I've tried with and without the preceding @.

This is the line that causes the crash:

VideoWindow.MediaPlayer.URL = @auditVideo.Path;

The relevant code is below:

MainWindow.cs:

        Video auditVideo = new Video();
        private void ButtonImportVideo_Click(object sender, EventArgs e)
        {
            auditVideo.InitializeFile(openFileDialogVideo);

            textBoxVideoFile.Text = auditVideo.Name;
        }
        private void ButtonPlayVideo_Click(object sender, EventArgs e)
        {
            VideoForm VideoWindow = new VideoForm();
            try
            {
                VideoWindow.MediaPlayer.URL = @auditVideo.Path; // This is what causes the crash
            }
            catch(Exception ex)
            {
                MessageBox.Show("could not load the file" + ex.Message);
            }
            Console.WriteLine("VideoWindow.MediaPlayer.URL is {0}", @VideoWindow.MediaPlayer.URL);

            VideoWindow.Show();
        }

Video.cs class:

namespace AVCAudit
{
    internal class Video
    {
        internal OpenFileDialog OpenFileDialog { get; private set; } //This is an AutoProperty which generates the private field behind the scenes
        internal string Path { get; private set; } //set should be private for Path and Name since they refer to the actual file on disk which doesn't change
        internal string Name { get; private set; }
        internal void InitializeFile(OpenFileDialog fileDialogArg)
        {
            OpenFileDialog = fileDialogArg;
            OpenFileDialog.InitialDirectory = @"C:\Users\schaney\Desktop\Neology\AVC Audit Project\avc_audit\Recordings";
            OpenFileDialog.Title = "Open audit video file";
            OpenFileDialog.Filter = "(mp3,wav,mp4,mov,wmv,mpg)|*.mp3;*.wav;*.mp4;*.mov;*.wmv;*.mpg|all files|*.*";
            if (OpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                Path = fileDialogArg.FileName;
                Name = fileDialogArg.SafeFileName;
            }
        }
    }
}

VideoForm.cs

namespace AVCAudit
{
    public partial class VideoForm : Form
    {
        internal String test { get; set; }
        public VideoForm() //constructor for VideoForm class. The rest of the partial class is defined in VideoForm.Designer.cs
        {
            InitializeComponent(); //so the constructor for this class just runs the InitializeComponent method defined in the Designer.cs file
        }
        private void MediaPlayer_Enter(object sender, EventArgs e)
        {

        }
    }
}
Samwise Ganges
  • 395
  • 6
  • 11
  • 1
    Are you running in 32-bit or 64-bit? – Charlieface Jul 20 '22 at 21:08
  • @Charlieface It's Windows 10 Pro 64 bit version 10.0.18362 Build 18362. Also I'm running Visual Studio 2022 version 17.2.3 – Samwise Ganges Jul 20 '22 at 21:47
  • No I'm asking about your build. Are you building the app in 64 or 32 bit, or as Any CPU – Charlieface Jul 20 '22 at 21:59
  • @Charlieface, For the build Platform dropdown, the only option is Active (Any CPU) regardless of Configuration. I had the Platform target set to Any CPU. I have tried running it with Platform target set to 32 (x86) and 64 (x64) but the behavior is the same – Samwise Ganges Jul 21 '22 at 17:23
  • 1
    It is recommended that you try to modify the url address directly in the properties. – Jiale Xue - MSFT Jul 28 '22 at 08:21
  • @JialeXue-MSFT, when I set the URL in the Properties window (or right clicking on the media player and going into properties) nothing happens. The program doesn't crash on startup, but also no file is loaded. If I Ctlcontrols.play(); it doesn't play because no file is loaded. If I click the Set URL button to change the URL during runtime, it still crashes. – Samwise Ganges Jul 29 '22 at 17:58

1 Answers1

2

I have found the root cause of this crash which is the way these Dell laptops work with external graphics cards. There is a specific arrangement where all graphics must be piped through the CPU's integrated graphics, even those which are processed through the external graphics card. I have an nVidia Quadro card, but any application that wants to use it has to first go through the integrated graphics, then the CPU can determine whether or not it wants to use the Quadro for processing that request. Basically from what I understand, applications should be blind to the Quadro, and do everything through the integrated graphics.

For some reason, the C# axWindowsMediaPlayer object does not work properly with this arrangement and causes an access violation. I would guess that it's trying to interact with the Quadro directly.

As a workaround, I can stop the crash by configuring nVidia Control Panel to use Integrated Graphics for the project's Debug and Release executable files. This forces them to only interface with the integrated graphics, thus stopping the access violation.

Samwise Ganges
  • 395
  • 6
  • 11