Joining DirectSound to existing app

I am trying to use DirectX DirectSound which I need to attach to an existing Windows application in order to capture a signal. Since playing sounds is easier, I decided first to attach a button to the existing application that would play a sound. The DirectX 9.0 documentation seems to have no direct tutorials in which such a function is attached to something else. I have done a fair amount of experimenting but am stuck. Specifically, (1) (this is probably trivial) I don't know how to specify the "control" variable in the method Device.SetCooperativeLevel(). (2) (apparently more serious) the instruction "dsDevice = new Device();" throws the exception "[description of instruction] is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang." In fact, this instruction is in the event handler for loading the form. How do I fix this?
[1000 byte] By [prog.gabi] at [2007-12-22]
# 1

1) Typically, you'd use the handle for the main form in the call to SetCooperativeLevel.

2) Can you post all the relevant code?

JimPerry at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...
# 2

My attempted program consists of the files Form0.cs, Form0.designer.cs, Program.cs, and Form1.cs. The first three were produced by Visual Studio by my starting what they call a "windows application". The last is my own attempt to adapt the code from "DirectX SDK\DirectX Documentation\DirectX Documentation for Managed Languages\Direct Sound\Playing Sounds".
// Form0.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.DirectX.DirectSound;

namespace TryPlay0
{
public partial class Form0 : Form
{
public Form0()
{
InitializeComponent();
}

private void Form0_Load(object sender, EventArgs e)
{
Device dsDevice = null;
dsDevice = new Device();
return;
}
}

public class wfEnum : Form
{
private DevicesCollection myDevices = null;
private struct myDeviceDescription
{
public DeviceInformation info;
public override string ToString()
{
return info.Description;
}
public myDeviceDescription(DeviceInformation di)
{
info = di;
}
}
public wfEnum()
{
// Retrieve the available DirectSound devices
myDevices = new DevicesCollection();

foreach (DeviceInformation dev in myDevices)
{
myDeviceDescription dd = new myDeviceDescription(dev);

// Use DevicesCollection and DeviceInformation to query for devices.
}
}
public static Device dsDevice = null;
}
}
///Form0.Designer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.DirectX.DirectSound;


namespace TryPlay0
{
partial class Form0
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(121, 112);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// Form0
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Name = "Form0";
this.Text = "Form0";
this.Load += new System.EventHandler(this.Form0_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
}
}

//Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TryPlay0
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form0());
}
}
}
//Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.DirectSound;

//[C#]

using Microsoft.DirectX.DirectSound;

public class wfEnum : Form
{
private DevicesCollection myDevices = null;
private struct myDeviceDescription
{
public DeviceInformation info;
public override string ToString()
{
return info.Description;
}
public myDeviceDescription(DeviceInformation di)
{
info = di;
}
}
public wfEnum()
{
// Retrieve the available DirectSound devices
myDevices = new DevicesCollection();

foreach (DeviceInformation dev in myDevices)
{
myDeviceDescription dd = new myDeviceDescription(dev);

// Use DevicesCollection and DeviceInformation to query for devices.
}
}
private Device dsDevice/* = null*/;
}
//Thank you for your help.

prog.gabi at 2007-8-30 > top of Msdn Tech,Game Technologies: DirectX, XNA, XACT, etc.,Game Technologies: DirectX 101...