Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
He,can anyone teach me how to sovle the problem which when i complie, there are still get 2 error and other link error.
And i am a programming beginner

Thanks in advance:):)
C++
#include < windows.h >
#include < mmsystem.h >
#include < iostream >
#pragma comment (lib,"winmm.lib")
using namespace std;

int DeviceManager::getAudioInputVolumn( const std::string &deviceName )
{
 MMRESULT          rc;     
 HMIXER            hMixer; 
 MIXERLINE         mxl;    
 MIXERLINECONTROLS mxlc;   
 MIXERCONTROL      mxc;    

 for( int deviceID =0 ; true ; deviceID++ )
 {
   rc = mixerOpen(&hMixer,deviceID,0,0,MIXER_OBJECTF_MIXER);

  if( MMSYSERR_NOERROR!=rc )
  {
   break;
  }

  ZeroMemory(&mxl, sizeof(MIXERLINE));

  mxl.cbStruct = sizeof(MIXERLINE); 
  mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

  rc = mixerGetLineInfo((HMIXEROBJ)hMixer,
   &mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);
  if( rc!=MMSYSERR_NOERROR )
  {
   continue;
  }

 
  DWORD   dwConnections   =   mxl.cConnections; 
  DWORD   dwLineID = -1; 
  for( DWORD i=0 ; i<dwconnections;>  { 
   mxl.dwSource = i;
   rc = mixerGetLineInfo((HMIXEROBJ)hMixer,&mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_SOURCE);
 
   if( MMSYSERR_NOERROR==rc )
   {  
    if( mxl.dwComponentType==MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE )
    { 
     dwLineID = mxl.dwLineID; 
     break; 
    }
   }
  } 
  if( dwLineID==-1 )
  { 
   continue;
  }

  ZeroMemory(&mxc, sizeof(MIXERCONTROL));

  mxc.cbStruct = sizeof(mxc); 

  ZeroMemory(&mxlc, sizeof(MIXERLINECONTROLS));

  mxlc.cbStruct = sizeof(mxlc); 
  mxlc.dwLineID = dwLineID; 
  mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
  mxlc.cControls = 1;          
  mxlc.pamxctrl = &mxc;       
  mxlc.cbmxctrl = sizeof(mxc);

  rc = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE);

  if (MMSYSERR_NOERROR == rc)
  {
   MIXERCONTROLDETAILS        mxcd;      
   MIXERCONTROLDETAILS_SIGNED volStruct; 

   ZeroMemory(&mxcd, sizeof(mxcd));

   mxcd.cbStruct = sizeof(mxcd);       
   mxcd.dwControlID = mxc.dwControlID; 
   mxcd.paDetails = &volStruct;        
   mxcd.cbDetails = sizeof(volStruct); 
   mxcd.cChannels = 1;                 

   rc = mixerGetControlDetails((HMIXEROBJ)hMixer,&mxcd,MIXER_GETCONTROLDETAILSF_VALUE);

   if (MMSYSERR_NOERROR == rc)
   {
    long step = ( mxc.Bounds.lMaximum - mxc.Bounds.lMinimum ) /100;

    int index = ( volStruct.lValue - mxc.Bounds.lMinimum) /step ;
    printf("volume:%X", volStruct.lValue);
    return index;
   }
  }
 }

 return -1;
}
Posted
Updated 10-Oct-11 0:08am
v2
Comments
André Kraak 10-Oct-11 6:17am    
I might be useful if you mentioned the error messages you are getting and specify which lines are generating the errors.

If you wish to change your question use the Improve Question button.

1.
Add #include <stdio.h> to the top of the file


2.
Change
C++
int DeviceManager::getAudioInputVolumn( const std::string &deviceName )

to
int getAudioInputVolumn( const std::string &deviceName )



3.
Change
C++
for( DWORD i=0 ; i<dwconnections;>

to
for( DWORD i=0 ; i<dwConnections; )


It will now compile, though won't link because the program has no entry point.

4.
Add either a main or a winMain function to the code.
 
Share this answer
 
v2
Comments
lijing123456 12-Oct-11 3:09am    
Hi, sorry for disturbing you,
I just follow your instructions, now it can comile successful but when I click Debug, the window will be closed immediately without displaying anything.
thanks in advance :)

and after your instructions, I chang my code like that:

#include < stdio.h >
#include < windows.h >
#include < mmsystem.h >
#include < iostream >
#pragma comment (lib,"winmm.lib")
using namespace std;

int main()
{
}

int getAudioInputVolumn( const std::string &deviceName )
{
MMRESULT rc;
HMIXER hMixer;
MIXERLINE mxl;
MIXERLINECONTROLS mxlc;
MIXERCONTROL mxc;

for( int deviceID =0 ; true ; deviceID++ )
{
rc = mixerOpen(&hMixer,deviceID,0,0,MIXER_OBJECTF_MIXER);

if( MMSYSERR_NOERROR!=rc )
{
break;
}

ZeroMemory(&mxl, sizeof(MIXERLINE));

mxl.cbStruct = sizeof(MIXERLINE);
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

rc = mixerGetLineInfo((HMIXEROBJ)hMixer,
&mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);
if( rc!=MMSYSERR_NOERROR )
{
continue;
}


DWORD dwConnections = mxl.cConnections;
DWORD dwLineID = -1;
for( DWORD i=0 ; i
Hi, sorry for disturbing you,
I just follow your instructions, now it can comile successful but when I click Debug, the window will be closed immediately without displaying anything.
thanks in advance :)
and after your instructions, I chang my code like that:

#include < stdio.h >
#include < windows.h >
#include < mmsystem.h >
#include < iostream >
#pragma comment (lib,"winmm.lib")
using namespace std;

int main()
{
}

int getAudioInputVolumn( const std::string &deviceName )
{
MMRESULT rc;
HMIXER hMixer;
MIXERLINE mxl;
MIXERLINECONTROLS mxlc;
MIXERCONTROL mxc;

for( int deviceID =0 ; true ; deviceID++ )
{
rc = mixerOpen(&hMixer,deviceID,0,0,MIXER_OBJECTF_MIXER);

if( MMSYSERR_NOERROR!=rc )
{
break;
}

ZeroMemory(&mxl, sizeof(MIXERLINE));

mxl.cbStruct = sizeof(MIXERLINE);
mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

rc = mixerGetLineInfo((HMIXEROBJ)hMixer,
&mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);
if( rc!=MMSYSERR_NOERROR )
{
continue;
}


DWORD dwConnections = mxl.cConnections;
DWORD dwLineID = -1;
for( DWORD i=0 ; i<dwconnections;)>
mxl.dwSource = i;
rc = mixerGetLineInfo((HMIXEROBJ)hMixer,&mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_SOURCE);

if( MMSYSERR_NOERROR==rc )
{
if( mxl.dwComponentType==MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE )
{
dwLineID = mxl.dwLineID;
break;
}
}
}
if( dwLineID==-1 )
{
continue;
}

ZeroMemory(&mxc, sizeof(MIXERCONTROL));

mxc.cbStruct = sizeof(mxc);

ZeroMemory(&mxlc, sizeof(MIXERLINECONTROLS));

mxlc.cbStruct = sizeof(mxlc);
mxlc.dwLineID = dwLineID;
mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
mxlc.cControls = 1;
mxlc.pamxctrl = &mxc;
mxlc.cbmxctrl = sizeof(mxc);

rc = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE);

if (MMSYSERR_NOERROR == rc)
{
MIXERCONTROLDETAILS mxcd;
MIXERCONTROLDETAILS_SIGNED volStruct;

ZeroMemory(&mxcd, sizeof(mxcd));

mxcd.cbStruct = sizeof(mxcd);
mxcd.dwControlID = mxc.dwControlID;
mxcd.paDetails = &volStruct;
mxcd.cbDetails = sizeof(volStruct);
mxcd.cChannels = 1;

rc = mixerGetControlDetails((HMIXEROBJ)hMixer,&mxcd,MIXER_GETCONTROLDETAILSF_VALUE);

if (MMSYSERR_NOERROR == rc)
{
long step = ( mxc.Bounds.lMaximum - mxc.Bounds.lMinimum ) /100;

int index = ( volStruct.lValue - mxc.Bounds.lMinimum) /step ;
printf("volume:%X", volStruct.lValue);
return index;
}
}
}

return -1;
}
 
Share this answer
 
Comments
enhzflep 12-Oct-11 3:28am    
It's no problem, you're not disturbing me. :)

I can see that you are very new to programming. Please excuse me for the lack of direction I gave earlier.

In order to actually show something, you'll have to call the function and display the results.

This is the main that I have used to test the code. The result is correct - Volume % of the microphone input, though there is no check against the passed deviceName. You can change the function signature to:
<pre>int getAudioInputVolumn()</pre>

<pre>int main()
{
printf("Mic volume: %d\n", getAudioInputVolumn());
}</pre>
lijing123456 17-Oct-11 5:16am    
Hi,
sorry..it is hard for me.can you give me the full cpp file or the whole programme becasue I really really new to the c++ programming, you may send it to my email(lijing3975@hotmail.com)
thx very much..
A true horror, however it does produce an accurate output..

#include <windows.h>
#include <mmsystem.h>
#include <iostream>

// EDITED s.b
#include <stdio.h>

using namespace std;

// CHANGED s.b int DeviceManager::getAudioInputVolumn( const std::string &deviceName)
int getAudioInputVolumn()
{
    MMRESULT          rc;
    HMIXER            hMixer;
    MIXERLINE         mxl;
    MIXERLINECONTROLS mxlc;
    MIXERCONTROL      mxc;

    for( int deviceID =0 ; true ; deviceID++ )
    {
        rc = mixerOpen(&hMixer,deviceID,0,0,MIXER_OBJECTF_MIXER);

        if( MMSYSERR_NOERROR!=rc )
        {
            break;
        }

        ZeroMemory(&mxl, sizeof(MIXERLINE));

        mxl.cbStruct = sizeof(MIXERLINE);
        mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;

        rc = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_COMPONENTTYPE);
        if( rc!=MMSYSERR_NOERROR )
        {
            continue;
        }


        DWORD   dwConnections   =   mxl.cConnections;
        DWORD   dwLineID = -1;
// EDITED s.b  for( DWORD i=0 ; i<dwconnections;>  {
        for( DWORD i=0 ; i<dwConnections;)
        {
            mxl.dwSource = i;
            rc = mixerGetLineInfo((HMIXEROBJ)hMixer,&mxl,MIXER_OBJECTF_HMIXER|MIXER_GETLINEINFOF_SOURCE);

            if( MMSYSERR_NOERROR==rc )
            {
                if( mxl.dwComponentType==MIXERLINE_COMPONENTTYPE_SRC_MICROPHONE )
                {
                    dwLineID = mxl.dwLineID;
                    break;
                }
            }
        }
        if( dwLineID==-1 )
        {
            continue;
        }

        ZeroMemory(&mxc, sizeof(MIXERCONTROL));

        mxc.cbStruct = sizeof(mxc);

        ZeroMemory(&mxlc, sizeof(MIXERLINECONTROLS));

        mxlc.cbStruct = sizeof(mxlc);
        mxlc.dwLineID = dwLineID;
        mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
        mxlc.cControls = 1;
        mxlc.pamxctrl = &mxc;
        mxlc.cbmxctrl = sizeof(mxc);

        rc = mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc,MIXER_GETLINECONTROLSF_ONEBYTYPE);

        if (MMSYSERR_NOERROR == rc)
        {
            MIXERCONTROLDETAILS        mxcd;
            MIXERCONTROLDETAILS_SIGNED volStruct;

            ZeroMemory(&mxcd, sizeof(mxcd));

            mxcd.cbStruct = sizeof(mxcd);
            mxcd.dwControlID = mxc.dwControlID;
            mxcd.paDetails = &volStruct;
            mxcd.cbDetails = sizeof(volStruct);
            mxcd.cChannels = 1;

            rc = mixerGetControlDetails((HMIXEROBJ)hMixer,&mxcd,MIXER_GETCONTROLDETAILSF_VALUE);

            if (MMSYSERR_NOERROR == rc)
            {
                long step = ( mxc.Bounds.lMaximum - mxc.Bounds.lMinimum ) /100;

                int index = ( volStruct.lValue - mxc.Bounds.lMinimum) /step ;
      //          printf("volume:%X", volStruct.lValue);
                return index;
            }
        }
    }

    return -1;
}

int main()
{
    printf("Mic volume: %d\n", getAudioInputVolumn());
}
 
Share this answer
 
Comments
lijing123456 18-Oct-11 23:00pm    
thank you very much!!

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