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

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

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

не могу понять в чем проблема ,если использую так работает но не стабильно (то и дело выдает ошибку vshost.exe не работает)
Листинг программы
  1. SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.аа);
  2. sp.Load(); sp.Play();
если прописываю путь на жестком все работает без крашей...
Листинг программы
  1. sp = new SoundPlayer(@"E:\Музыка\аа.wav");
как можно добиться стабильности работы , великие умы помогите))
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.Runtime.InteropServices;
  11. using System.Threading;
  12. using System.IO;
  13. using System.Media;
  14.  
  15. namespace Injector
  16. {
  17. public partial class 2013 : Form
  18. {
  19. # region Импорт
  20. [DllImport("kernel32")]
  21. public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, UIntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId);
  22. [DllImport("kernel32.dll")]
  23. public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, Int32 dwProcessId);
  24. [DllImport("kernel32.dll")]
  25. public static extern Int32 CloseHandle(IntPtr hObject);
  26. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  27. static extern bool VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint dwFreeType);
  28. [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
  29. public static extern UIntPtr GetProcAddress(IntPtr hModule, string procName);
  30. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  31. static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, uint flAllocationType, uint flProtect);
  32. [DllImport("kernel32.dll")]
  33. static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, string lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten);
  34. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  35. public static extern IntPtr GetModuleHandle(string lpModuleName);
  36. [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
  37. internal static extern Int32 WaitForSingleObject(IntPtr handle, Int32 milliseconds);
  38. public Int32 GetProcessId(String proc)
  39. {
  40. Process[] ProcList;
  41. ProcList = Process.GetProcessesByName(proc);
  42. return ProcList[0].Id;
  43. }
  44. #endregion
  45. # region Inject
  46. public void InjectDLL(IntPtr hProcess, String strDLLName)
  47. {
  48. IntPtr bytesout;
  49. Int32 LenWrite = strDLLName.Length + 1;
  50. IntPtr AllocMem = (IntPtr)VirtualAllocEx(hProcess, (IntPtr)null, (uint)LenWrite, 0x1000, 0x40);
  51. WriteProcessMemory(hProcess, AllocMem, strDLLName, (UIntPtr)LenWrite, out bytesout);
  52. UIntPtr Injector = (UIntPtr)GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
  53. if (Injector == null)
  54. {
  55. MessageBox.Show(" Injecto Error! \n ");
  56. return;
  57. }
  58. IntPtr hThread = (IntPtr)CreateRemoteThread(hProcess, (IntPtr)null, 0, Injector, AllocMem, 0, out bytesout);
  59. if (hThread == null)
  60. {
  61. MessageBox.Show("Thread injection Failed");
  62. return;
  63. }
  64. int Result = WaitForSingleObject(hThread, 10 * 1000);
  65. if (Result == 0x00000080L || Result == 0x00000102L || Result == 0xFFFFFFFF)
  66. {
  67. MessageBox.Show("Thread 2 inject failed");
  68. if (hThread != null)
  69. {
  70. CloseHandle(hThread);
  71. }
  72. return;
  73. }
  74. Thread.Sleep(1000);
  75. VirtualFreeEx(hProcess, AllocMem, (UIntPtr)0, 0x8000);
  76. if (hThread != null)
  77. {
  78. CloseHandle(hThread);
  79. }
  80. return;
  81. }
  82. #endregion
  83. public 123()
  84. {
  85. InitializeComponent();
  86. timer1.Start();
  87. }
  88. private void Form1_Load(object sender, EventArgs e)
  89. {
  90.  
  91. SoundPlayer sp = new System.Media.SoundPlayer(Properties.Resources.aa);
  92. sp.Load(); sp.Play();
  93. }
  94.  
  95. String NameDLL = (Application.StartupPath + "\\1.dll");
  96. String ProcName = "ere";
  97. private void timer1_Tick(object sender, EventArgs e)
  98. {
  99. if (Process.GetProcessesByName(ProcName).Length == 1)
  100. {
  101. timer1.Stop();
  102. button1.Enabled = true;
  103. }
  104. }
  105. private void button1_Click(object sender, EventArgs e)
  106. {
  107. if (File.Exists(NameDLL))
  108. {
  109. Int32 ProcID = GetProcessId(ProcName);
  110. if (ProcID >= 0)
  111. {
  112. IntPtr hProcess = (IntPtr)OpenProcess(0x1F0FFF, 1, ProcID);
  113. if (hProcess == null)
  114. {
  115. MessageBox.Show("...");
  116. return;
  117. }
  118. else
  119. {
  120. InjectDLL(hProcess, NameDLL);
  121. Application.Exit();
  122. }
  123. }
  124. }
  125. else
  126. {
  127. MessageBox.Show("1.dll не существует", "...");
  128. Application.Exit();
  129. }
  130. }
  131. private void button2_Click(object sender, EventArgs e)
  132. {
  133. }
  134. private void button3_Click(object sender, EventArgs e)
  135. {
  136. }
  137.  
  138. }
  139.  
  140. }

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

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

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

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

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

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут