首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

mixerGetLineInfo 得不到回到的结构体的字符串内容?

2013-04-21 
mixerGetLineInfo 得不到返回的结构体的字符串内容??想做一个XP下的音量控制方面的程序。是通过一个MFC的代

mixerGetLineInfo 得不到返回的结构体的字符串内容??
想做一个XP下的音量控制方面的程序。是通过一个MFC的代码改的,但是刚到 函数mixerGetLineInfo 时就不对了...不知如何修改... 求帮助~~~

下面分别贴上 C#里面的代码 和 MFC下的代码 以及他们的执行效果图。

MFC的一段代码和效果图 如下:
要加上
#include <mmsystem.h>
#pragma comment(lib,"Winmm.lib")



C# 的一段代码和效果图 如下:

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.Runtime.InteropServices;

namespace _cs_test
{
    public partial class Form1 : Form
    {
        public const uint MAXPNAMELEN = 32;
        public const uint CALLBACK_WINDOW = 0x00010000;
        public const uint MIXER_OBJECTF_MIXER = 0x00000000;
        public const uint MMSYSERR_NOERROR = 0;
        public const uint MIXERLINE_COMPONENTTYPE_DST_SPEAKERS = 4;
        public const uint MIXER_OBJECTF_HANDLE = 0x80000000;
        public const uint MIXER_OBJECTF_HMIXER = (MIXER_OBJECTF_HANDLE | MIXER_OBJECTF_MIXER);
        public const uint MIXER_GETLINEINFOF_COMPONENTTYPE = 0x00000003;
        public const uint MIXER_SHORT_NAME_CHARS = 16;
        public const uint MIXER_LONG_NAME_CHARS = 64;



// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct MIXERCAPSW
        {
            ushort wMid;                   /* manufacturer id */
            ushort wPid;                   /* product id */
            uint vDriverVersion;         /* version of the driver */
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)MAXPNAMELEN)]
            String szPname;   /* product name */
            uint fdwSupport;             /* misc. support bits */
            uint cDestinations;          /* count of destinations */
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct MIXERLINEW
        {
            public uint cbStruct;               /* size of MIXERLINE structure */
            public uint dwDestination;          /* zero based destination index */
            public uint dwSource;               /* zero based source index (if source) */
            public uint dwLineID;               /* unique line id for mixer device */
            public uint fdwLine;                /* state/information about line */
            public uint dwUser;                 /* driver specific information */
            public uint dwComponentType;        /* component type line connects to */


            public uint cChannels;              /* number of channels line supports */
            public uint cConnections;           /* number of connections [possible] */
            public uint cControls;              /* number of controls at this line */
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)MIXER_SHORT_NAME_CHARS)] // 16
            public String szShortName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)MIXER_LONG_NAME_CHARS)]  // 64
            public String szName;
            //public struct Target
            //{
            public uint dwType;                 /* MIXERLINE_TARGETTYPE_xxxx */
            public uint dwDeviceID;             /* target device ID of device type */
            public ushort wMid;                   /* of target device */
            public ushort wPid;                   /*      " */
            public uint vDriverVersion;       /*      " */
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = (int)MAXPNAMELEN)] // 32
            public String szPname;   /*      " */
            //}
        }
    // *** ***

        [DllImport("Winmm.dll", SetLastError = true, EntryPoint = "mixerGetNumDevs")]
        public static extern uint mixerGetNumDevs();

        [DllImport("Winmm.dll", SetLastError = true, EntryPoint = "mixerOpen")]
        public static extern uint mixerOpen(
            ref IntPtr phmx,
            uint uMxId,


            IntPtr dwCallback,
            IntPtr dwInstance,
            uint fdwOpen);

        [DllImport("Winmm.dll", SetLastError = true, EntryPoint = "mixerGetDevCaps")]
        public static extern uint mixerGetDevCaps(
            IntPtr uMxId,
            ref MIXERCAPSW pmxcaps,
            uint cbmxcaps);

        [DllImport("Winmm.dll", SetLastError = true, EntryPoint = "mixerGetLineInfo")]
        public static extern uint mixerGetLineInfo(
            IntPtr hmxobj,
            ref MIXERLINEW pmxl,
            uint fdwInfo);

// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        public Form1()
        {
            InitializeComponent();
        }


        uint m_nNumMixers = 0;
        IntPtr m_hMixer = IntPtr.Zero;
        MIXERCAPSW m_mxcaps = new MIXERCAPSW();

        private void button1_Click(object sender, EventArgs e)
        {
            m_nNumMixers = mixerGetNumDevs();
            //MessageBox.Show("m_nNumMixers : " + m_nNumMixers.ToString());

            if (m_nNumMixers != 0)
            {
                uint uiRtn = mixerOpen(
                    ref m_hMixer,
                    0,
                    this.Handle,
                    IntPtr.Zero,
                    MIXER_OBJECTF_MIXER | CALLBACK_WINDOW);
                if (uiRtn != MMSYSERR_NOERROR)


                {
                    MessageBox.Show("mixerOpen failed");
                    return;
                }

                uiRtn = mixerGetDevCaps(m_hMixer, ref m_mxcaps, (uint)Marshal.SizeOf(m_mxcaps));
                if (uiRtn != MMSYSERR_NOERROR)
                {
                    MessageBox.Show("mixerGetDevCaps failed");
                    return;
                }
            }

            if (m_hMixer == IntPtr.Zero)
            {
                MessageBox.Show("m_hMixer == IntPtr.Zero");
                return;
            }

            // get dwLineID
            MIXERLINEW mxl = new MIXERLINEW();
            mxl.cbStruct = (uint)Marshal.SizeOf(mxl);
            mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
            uint uiRst = mixerGetLineInfo(
                m_hMixer,
                ref mxl,
                MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE);
            if (uiRst != MMSYSERR_NOERROR)
            {
                MessageBox.Show("mixerGetLineInfo return : " + Convert.ToString(uiRst, 16));
                return;
            }

            MessageBox.Show(" mxl.dwLineID : " + ((int)mxl.dwLineID).ToString() + " , " + Convert.ToString(mxl.dwLineID, 16) + "\r\n mxl.szName : " + mxl.szName);
            // 本来是想将  mxl.szName 的内容显示在界面上,但是这里居然是空的...MFC里面是有值的啊,在C#里面该怎么弄啊??


            // 要怎么样改才能像MFC显示的效果那样显示“音量控制”啊?
        }
    }
}


mixerGetLineInfo 得不到回到的结构体的字符串内容?

C#里面的效果如上图,mxl.szName 里面是空的,但是 上面的MFC的效果是有值“音量控制”的啊,该要怎么改啊??
求指点啊~~~
[解决办法]
http://www.cnblogs.com/zll922/archive/2009/07/10/1520576.html
[解决办法]
你定义了Unicode的MIXERLINEW结构,却可能使用了Ansi版的函数入口。
试试把入口也定为Unicode版本:
[DllImport("Winmm.dll", SetLastError = true, EntryPoint = "mixerGetDevCapsW")]
...
[DllImport("Winmm.dll", SetLastError = true, EntryPoint = "mixerGetLineInfoW")]
...

热点排行