Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone.
Im building an project using Visual studio 2008 express c#. Basically im trying to catch audio via directsound on Window 7 x64 using the below code but it is giving me the error:
BadImageFormatException is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

Can anyone see where ive went wrong with my code or how i could fix this issue?
I've set it to compile using x86 rather than x64 or AnyCpu but have tried those aswell

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.DirectX.DirectSound;

namespace USBGlowLightDirectX
{
    public partial class Form1 : Form
    {
        WaveFormat waveFormat = default(WaveFormat);
        CaptureBuffer _dwCapBuffer = default(CaptureBuffer);
        AutoResetEvent _resetEvent = new AutoResetEvent(false);
        Notify _notify = default(Notify);
        Int32 _dwOutputBufferSize;
        Int32 _dwCaptureBufferSize;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Setup the Capturing Format
            waveFormat.SamplesPerSecond = 96000;
            waveFormat.BitsPerSample = 16;
            waveFormat.Channels = 2;
            waveFormat.FormatTag = WaveFormatTag.Pcm;
            waveFormat.BlockAlign = (short)(waveFormat.Channels * (waveFormat.BitsPerSample / 8));
            waveFormat.AverageBytesPerSecond = waveFormat.SamplesPerSecond * waveFormat.BlockAlign;

            var _dwNotifySize = Math.Max(4096, waveFormat.AverageBytesPerSecond / 8);
            _dwNotifySize -= _dwNotifySize % waveFormat.BlockAlign;
            _dwCaptureBufferSize = _dwNotifySize;
            _dwOutputBufferSize = _dwNotifySize / 2;

            //Setup the Capture Buffer
            var cap = default(Capture);
            var cdc = new CaptureDevicesCollection();
            for (int i = 0; i < cdc.Count; i++)
            {
                if (cdc[i].Description.ToLower().Contains("Stereo Mix".ToLower()))
                {
                    cap = new Capture(cdc[i].DriverGuid);
                    break;
                }
            }
            var capDesc = new CaptureBufferDescription
            {
                Format = waveFormat,
                BufferBytes = _dwCaptureBufferSize
            };
            _dwCapBuffer = new CaptureBuffer(capDesc, cap); //This is the Capture Buffer

            //setup buffer notifications to allow buffer to reset when full

            var bpn1 = new BufferPositionNotify();
            bpn1.Offset = _dwCapBuffer.Caps.BufferBytes / 2 - 1;
            bpn1.EventNotifyHandle = _resetEvent.SafeWaitHandle.DangerousGetHandle();
            var bpn2 = new BufferPositionNotify();
            bpn2.Offset = _dwCapBuffer.Caps.BufferBytes - 1;
            bpn2.EventNotifyHandle = _resetEvent.SafeWaitHandle.DangerousGetHandle();
            _notify.SetNotificationPositions(new BufferPositionNotify[] { bpn1, bpn2 });
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            {
                int offset = 0;
                Thread _dwCaptureThread = new Thread((ThreadStart)delegate
                {
                    _dwCapBuffer.Start(true);

                    while (this.checkBox1.Checked == true)
                    {
                        _resetEvent.WaitOne();
                        _dwCapBuffer.Read(offset, typeof(short), LockFlag.None, _dwOutputBufferSize);
                        offset = (offset + _dwOutputBufferSize) % _dwCaptureBufferSize;
                    }
                    _dwCapBuffer.Stop();
                });
                _dwCaptureThread.Start();
            }
        }
    }
}



Oh and the reason for using DirectSound instead of Xaudio is for backward compatability for XP systems.
Posted

1 solution

You said so, but did you set the "Target Platform" property in the property page or the Combobox on the mainform of the ide?
 
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