Run C++Console on Winform.

Hello every body.

I use VC++2012.
This code run ok with Console. But i want to run on winform VC++2012.
Can you help me ?

Thankyou very much.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// This console app enumerates and mutes all audio capture endpoints using the
// IAudioEndpointVolume::SetMute() method of the Vista/Win7 Core Audio API
// 
// Copyright (c) Fotios Basagiannis. All rights reserved
//
// EndpointVolumeChanger.cpp : Endpoint Volume Changing sample application.
//

#include "stdafx.h"
#include <EndpointVolume.h>
#include "Audioclient.h"
#include <mmdeviceapi.h>
#include <functiondiscoverykeys.h>

#include "CmdLine.h"

#include <mmdeviceapi.h>
#include <endpointvolume.h>
#define EXIT_ON_ERROR(hres)  \
              if (FAILED(hres)) { goto Exit; }
#define SAFE_RELEASE(punk)  \
              if ((punk) != NULL)  \
                { (punk)->Release(); (punk) = NULL; }

const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);

int main(int argc, CHAR* argv[])
{
 HRESULT hr=NULL;
    bool decibels = false;
    bool scalar = false;
    double newVolume=0.5;
 
    CoInitialize(NULL);
    IMMDeviceEnumerator *deviceEnumerator = NULL;
    hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER,__uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator);
    IMMDevice *defaultDevice = NULL;
 
    hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender,eMultimedia,  &defaultDevice);
    deviceEnumerator->Release();
    deviceEnumerator = NULL;
 
    IAudioEndpointVolume *endpointVolume = NULL;
    hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), 
         CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume);
    defaultDevice->Release();
    defaultDevice = NULL;
 
    // -------------------------
    float currentVolume = 0.8;
    endpointVolume->GetMasterVolumeLevel(&currentVolume);
    printf("Current volume in dB is: %f\n", currentVolume);

    hr = endpointVolume->GetMasterVolumeLevelScalar(&currentVolume);
    //CString strCur=L"";
    //strCur.Format(L"%f",currentVolume);
    //AfxMessageBox(strCur);

   
       // hr = endpointVolume->SetMasterVolumeLevel((float)newVolume, NULL);
  system("pause");
       hr = endpointVolume->SetMasterVolumeLevelScalar((float)newVolume, NULL);
   
    endpointVolume->Release();

	
    CoUninitialize();
 
    return FALSE;
	
}
Last edited on
Well, I think I have to disappoint you.
Basically nothing of what you have there is compatible with Windows Forms which is part of the .net framework.

stdafx.h - Nope
mmdeviceapi.h - Nope (?)
CoInitialize(NULL); which is COM - Nope

The mmdeviceapi.h might work, but it seems to be part of COM.
Maybe via mmsystem.h which is the classic Windows multimedia lib. But I never tried it within a .net (Windows Forms) environment.

But you will have to rewrite your code completely. Sorry.
Hello everybody.
This code run ok on Vc++ Console 2012 . I want to run on Winform.
Can you help me ? Because i only used to Vb.net
I will send 40usd for you.

Thankyou very much.
Seriously? Only $40? My help is worth $41, at least ;)

Ok, kidding aside, if you want to spend money, donate it to cplusplus.com.

Back to topic: I'm afraid you can't use the MMDevice API with .net. At least not without some effort.
Windows Forms uses managed code and I can't find any wrapper or class or namespace which provides the MMDevice API functions.

Maybe one of the .net buffs knows how to use non-managed COM API's in a WinForms environment.

@dotNET Buffs: COM & .net ? Anyone ?
Hi Plexus.

I am newbie with Vc++2012. I only used to Vb.net.
Can you help me step by step to run this code on Winform with Vc++2012.
Do you know about AudiovolumeCoreApi .

I will send for you some gift ...

Thankyou very much.
Can you help me step by step to run this code on Winform with Vc++2012.

Well, unfortunately, I can't.

The problem is that you try to use two different environments AND libraries here.

Windows Forms is the .Net world.
MMDevice is the COM world. (Component Object Model)

This makes the problem extra hard.

The only thing I found which should help you is:
http://msdn.microsoft.com/en-us/library/sd10k43k.aspx
Hi Plexus.
I very scare when read msdn.microsoft.com hic hic ....
Topic archived. No new replies allowed.