Воспроизведение звука wav из ресурсов - C#

Узнай цену своей работы

Формулировка задачи:

не могу понять в чем проблема ,если использую так работает но не стабильно (то и дело выдает ошибку vshost.exe не работает)
SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.аа);
            sp.Load(); sp.Play();
если прописываю путь на жестком все работает без крашей...
sp = new SoundPlayer(@"E:\Музыка\аа.wav");
как можно добиться стабильности работы , великие умы помогите))
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System.Media;

namespace Injector
{
    public partial class 2013 : Form
    {
        # region Импорт
 
        [DllImport("kernel32")]
        public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId);
 
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, Int32 dwProcessId);
 
        [DllImport("kernel32.dll")]
        public static extern Int32 CloseHandle(IntPtr hObject);
 
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
 
        [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
        public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
 
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
 
        [DllImport("kernel32.dll")]
        static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten);
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetModuleHandle(string lpModuleName);
 
        [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
        internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
 
        public Int32 GetProcessId(String proc)
        {
            Process[] ProcList;
            ProcList = Process.GetProcessesByName(proc);
            return ProcList[0].Id;
        }
        #endregion
 
        # region Inject
        public void InjectDLL(IntPtr hProcess, String strDLLName)
        {
            IntPtr bytesout;
            Int32 LenWrite = strDLLName.Length + 1;
            IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
            WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
            UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
 
            if (Injector == null)
            {
                MessageBox.Show(" Injecto Error! \n ");
                return;
            }
            IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
            if (hThread == null)
            {
                MessageBox.Show("Thread injection Failed");
                return;
            }
            int Result = WaitForSingleObject(hThread, 10 * 1000);
            if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
            {
                MessageBox.Show("Thread 2 inject failed");
                if (hThread != null)
                {
                    CloseHandle(hThread);
                }
                return;
            }
            Thread.Sleep(1000);
            VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
            if (hThread != null)
            {
                CloseHandle(hThread);
            }
            return;
        }
        #endregion
 
        public 123()
        {
            InitializeComponent();
            timer1.Start();
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {

            SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.aa);
            sp.Load(); sp.Play();
        }

        String NameDLL = (Application.StartupPath + "\\1.dll");
        String ProcName = "ere";
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Process.GetProcessesByName(ProcName).Length == 1)
            {
                timer1.Stop();
                button1.Enabled = true;
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (File.Exists(NameDLL))
            {
                Int32 ProcID = GetProcessId(ProcName);
                if (ProcID >= 0)
                {
                    IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
                    if (hProcess == null)
                    {
                        MessageBox.Show("...");
                        return;
                    }
                    else
                    {
                        InjectDLL(hProcess, NameDLL);
                        Application.Exit();
                    }
                }
 
            }
            else
            {
                MessageBox.Show("1.dll не существует", "...");
                Application.Exit();
            }
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            
        }

    }

    }

Решение задачи: «Воспроизведение звука wav из ресурсов»

textual
Листинг программы
 SoundPlayer sp = new SoundPlayer(Properties.Resources.тут_фай_из_ресурсов);
            sp.Play();

Оцени полезность:

7   голосов , оценка 4.286 из 5