Recent

Author Topic: SystemVolume W6-10 CoInitialize CoUninitialize [MasterVolume, MasterMute]  (Read 4798 times)

RAW

  • Hero Member
  • *****
  • Posts: 867
Is it really necessary to use COINITIALIZE and COUNINITIALIZE ?
I remember a post where someone said that it is only necessary in Delphi not in FreePascal.

I was playing around with this Code and everything is working as it should.
Then I recognized that I miss those two lines...

And If so where should I put those lines ???

Code: Pascal  [Select][+][-]
  1. Unit uMasterMuteW7;
  2.  {$MODE OBJFPC}{$H+}
  3.  
  4. Interface
  5.  
  6.  USES
  7.   Classes, SysUtils, Forms, Controls,
  8.   Windows, ComObj, ActiveX, ShellApi;
  9.  
  10.  
  11.  TYPE
  12.   TMyBOOL = (myFalse = Integer(0),
  13.              myTrue  = Integer(1));
  14.  
  15.  TYPE
  16.   TwndGUI = Class(TForm)
  17.  
  18.    Procedure SetMasterVolume (    sinVol: Single );
  19.    Procedure GetMasterVolume (Out sinVol: Single );
  20.    Function  GetMasterMute              : TMyBOOL;
  21.    Procedure SetMasterMute   (     Value: TMyBOOL);
  22.  
  23.    Procedure FormKeyDown     (Sender: TObject; Var Key: Word;
  24.                               Shift : TShiftState);
  25.   End;
  26.  
  27.  
  28.  // WINDOWS 7 VOLUME BEGIN
  29.  TYPE
  30.   IAudioEndpointVolumeCallback = Interface(IUnknown)
  31.    ['{657804FA-D6AD-4496-8A60-352752AF4F89}']
  32.   End;
  33.  
  34.   IAudioEndpointVolume = Interface(IUnknown)
  35.    ['{5CDF2C82-841E-4546-9722-0CF74078229A}']
  36.  
  37.     Function RegisterControlChangeNotify(
  38.               AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; StdCall;
  39.     Function UnregisterControlChangeNotify(
  40.               AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; StdCall;
  41.     Function GetChannelCount(Out PInteger): HRESULT; StdCall;
  42.  
  43.     Function SetMasterVolumeLevel(
  44.               fLevelDB: Single; pguidEventContext: PGUID): HRESULT; StdCall;
  45.     Function SetMasterVolumeLevelScalar(
  46.               fLevelDB: Single; pguidEventContext: PGUID): HRESULT; StdCall;
  47.  
  48.     Function GetMasterVolumeLevel(
  49.               Out fLevelDB: Single): HRESULT; StdCall;
  50.     Function GetMasterVolumeLevelScalar(
  51.               Out fLevelDB: Single): HRESULT; StdCall;
  52.  
  53.     Function SetChannelVolumeLevel(
  54.               nChannel: Integer; fLevelDB: Double; pguidEventContext: PGUID):
  55.              HRESULT; StdCall;
  56.     Function SetChannelVolumeLevelScalar(
  57.               nChannel: Integer; fLevelDB: Double; pguidEventContext: PGUID):
  58.              HRESULT; StdCall;
  59.  
  60.     Function GetChannelVolumeLevel(
  61.               nChannel: Integer; Out fLevelDB: Double): HRESULT; StdCall;
  62.     Function GetChannelVolumeLevelScalar(
  63.               nChannel: Integer; Out fLevelDB: Double): HRESULT; StdCall;
  64.  
  65.     Function SetMute(
  66.               bSetMute: Bool; pguidEventContext: PGUID): HRESULT; StdCall;
  67.     Function GetMute(
  68.               Out bGetMute: Bool): HRESULT; StdCall;
  69.  
  70.     Function GetVolumeStepInfo(
  71.               pnStep: Integer; Out pnStepCount: Integer): HRESULT; StdCall;
  72.     Function VolumeStepUp(
  73.               pguidEventContext: PGUID): HRESULT; StdCall;
  74.     Function VolumeStepDown(
  75.               pguidEventContext: PGUID): HRESULT; StdCall;
  76.     Function GetVolumeRange(
  77.               Out pflVolumeMindB: Double; Out pflVolumeMaxdB: Double;
  78.               Out pflVolumeIncrementdB: Double): HRESULT; StdCall;
  79.     Function QueryHardwareSupport(
  80.               Out pdwHardwareSupportMask): HRESULT; StdCall;
  81.   End;
  82.  
  83.   IAudioMeterInformation = Interface(IUnknown)
  84.    ['{C02216F6-8C67-4B5B-9D00-D008E73E0064}']
  85.   End;
  86.  
  87.   IPropertyStore = Interface(IUnknown)
  88.   End;
  89.  
  90.   IMMDevice = Interface(IUnknown)
  91.    ['{D666063F-1587-4E43-81F1-B948E807363F}']
  92.     Function Activate(
  93.               Const refId: TGUID; dwClsCtx: DWORD; pActivationParams: PInteger;
  94.               Out pEndpointVolume: IAudioEndpointVolume): HRESULT; StdCall;
  95.     Function OpenPropertyStore(
  96.               stgmAccess: DWORD; Out ppProperties: IPropertyStore):
  97.              HRESULT; StdCall;
  98.     Function GetId   (Out ppstrId: PLPWSTR): HRESULT; StdCall;
  99.     Function GetState(Out State: Integer):   HRESULT; StdCall;
  100.   End;
  101.  
  102.   IMMDeviceCollection = interface(IUnknown)
  103.    ['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}']
  104.   End;
  105.  
  106.   IMMNotificationClient = interface(IUnknown)
  107.    ['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}']
  108.   End;
  109.  
  110.   IMMDeviceEnumerator = interface(IUnknown)
  111.    ['{A95664D2-9614-4F35-A746-DE8DB63617E6}']
  112.     Function EnumAudioEndpoints(
  113.               dataFlow: TOleEnum; deviceState: SYSUINT; DevCollection:
  114.               IMMDeviceCollection): HRESULT; StdCall;
  115.     Function GetDefaultAudioEndpoint(
  116.               EDF: SYSUINT; ER: SYSUINT; Out Dev: IMMDevice): HRESULT; StdCall;
  117.     Function GetDevice(pwstrId: Pointer; Out Dev: IMMDevice): HRESULT; StdCall;
  118.     Function RegisterEndpointNotificationCallback(
  119.               pClient: IMMNotificationClient): HRESULT; StdCall;
  120.   End;
  121.  
  122.  CONST
  123.   CLASS_IMMDeviceEnumerator: TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
  124.   IID_IMMDeviceEnumerator  : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';
  125.   IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';
  126.  // WINDOWS 7 VOLUME END
  127.  
  128.  VAR
  129.   wndGUI: TwndGUI;
  130.  
  131. Implementation
  132.  {$R *.LFM}
  133.  
  134.  
  135. Procedure TwndGUI.FormKeyDown(Sender: TObject; Var Key: Word;
  136.                               Shift : TShiftState);
  137.   Var
  138.    myMute: TMyBOOL;
  139.    sinVol: Single;
  140.  Begin
  141.   // MIXER
  142.   If Key = Ord('V')
  143.   Then ShellExecute(0, Nil, PChar('sndvol.exe'), '', '', SW_SHOWNORMAL);
  144.  
  145.   // MUTE
  146.   If Key = Ord('M')
  147.   Then
  148.    Begin
  149.     myMute:= GetMasterMute;
  150.  
  151.     If myMute = myTrue
  152.     Then SetMasterMute(myFalse)
  153.     Else SetMasterMute(myTrue);
  154.    End;
  155.  
  156.   // MAX VOLUME
  157.   If Key = Ord('F')
  158.   Then
  159.    Begin
  160.     myMute:= GetMasterMute;
  161.  
  162.     If myMute = myTrue
  163.     Then SetMasterMute(myFalse);
  164.  
  165.     SetMasterVolume(1.0);
  166.    End;
  167.  
  168.   // VOLUME UP 1%
  169.   If Key = VK_UP
  170.   Then
  171.    Begin
  172.     GetMasterVolume(sinVol);
  173.  
  174.     sinVol:= sinVol+0.01;
  175.  
  176.     If sinVol   > 1.0
  177.     Then sinVol:= 1.0;
  178.  
  179.     SetMasterVolume(sinVol);
  180.    End;
  181.  
  182.   // VOLUME DOWN 1%
  183.   If Key = VK_DOWN
  184.   Then
  185.    Begin
  186.     GetMasterVolume(sinVol);
  187.  
  188.     sinVol:= sinVol-0.01;
  189.  
  190.     If sinVol   < 0.0
  191.     Then sinVol:= 0.0;
  192.  
  193.     SetMasterVolume(sinVol);
  194.    End;
  195.  End;
  196.  
  197.  
  198. Procedure TwndGUI.SetMasterVolume(sinVol: Single);
  199.   Var
  200.    EndpointVolume  : IAudioEndpointVolume;
  201.    DeviceEnumerator: IMMDeviceEnumerator;
  202.    Device          : IMMDevice;
  203.  Begin
  204.   CoCreateInstance(
  205.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  206.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  207.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  208.   Device.Activate(
  209.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  210.   EndpointVolume.SetMasterVolumeLevelScalar(sinVol, Nil);
  211.  End;
  212.  
  213.  
  214. Procedure TwndGUI.GetMasterVolume(Out sinVol: Single);
  215.   Var
  216.    EndpointVolume  : IAudioEndpointVolume;
  217.    DeviceEnumerator: IMMDeviceEnumerator;
  218.    Device          : IMMDevice;
  219.  Begin
  220.   CoCreateInstance(
  221.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  222.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  223.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  224.   Device.Activate(
  225.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  226.   EndpointVolume.GetMasterVolumeLevelScalar(sinVol);
  227.  End;
  228.  
  229.  
  230. Function TwndGUI.GetMasterMute: TMyBOOL;
  231.   Var
  232.    EndpointVolume  : IAudioEndpointVolume;
  233.    DeviceEnumerator: IMMDeviceEnumerator;
  234.    Device          : IMMDevice;
  235.  
  236.    hrRes: HResult;
  237.    bRes : BOOL;
  238.  Begin
  239.   CoCreateInstance(
  240.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  241.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  242.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  243.   Device.Activate(
  244.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  245.  
  246.   hrRes:= EndpointVolume.GetMute(bRes);
  247.  
  248.    If FAILED(hrRes)
  249.    Then OleCheck(hrRes);
  250.  
  251.   Result:= TMyBOOL(bRes);
  252.  End;
  253.  
  254.  
  255. Procedure TwndGUI.SetMasterMute(Value: TMyBOOL);
  256.   Var
  257.    EndpointVolume  : IAudioEndpointVolume;
  258.    DeviceEnumerator: IMMDeviceEnumerator;
  259.    Device          : IMMDevice;
  260.  
  261.    hrRes: HResult;
  262.  Begin
  263.   CoCreateInstance(
  264.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  265.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  266.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  267.   Device.Activate(
  268.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  269.  
  270.   hrRes:= EndpointVolume.SetMute(BOOL(Value), Nil);
  271.  
  272.   OleCheck(hrRes);
  273.  End;
  274.  
  275. End.
  276.  
« Last Edit: March 23, 2017, 10:16:37 pm by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

Thaddy

  • Hero Member
  • *****
  • Posts: 10282
Re: CoInitialize CoUninitialize [MasterVolume, MasterMute]
« Reply #1 on: February 13, 2017, 07:12:37 am »
Is it really necessary to use COINITIALIZE and COUNINITIALIZE ?
I remember a post where someone said that it is only necessary in Delphi not in FreePascal.
That's only the case if you use the comobj unit, which is not necessary to use com in general. So that information is .... not correct...
That would be highly gambling to rely on. comobj needs only to be pulled in if you write your own com objects.
ActiveX is the one you want if you use third party com code, like in your case.
Every COM using program needs to initialize and uninitialize the COM sub-system in any language.
You only have to do it once so  do it in the initialization and finalization section of the main unit and/or in the unit above. There is no harm to do it in more than one unit though. May even be practical.
There is a refcount on it, so for every call to coInitialize you have to call a corresponding coUninitialize. Hence the initialization and finalization sections are the right place to call them.

so before  the end of your code add:
Code: Pascal  [Select][+][-]
  1. initialization
  2.   coInitialize(nil);
  3. finalization
  4.   coUnInitialize;
  5. end.

To summarize:

If you have comobj somewhere in the uses clause it is not necessary to call coInitialize/coUnInitialize.
If you use just external com objects and need just ActiveX to work with it, you need to call coInitialize and coUnInitialize.
Don't pull in comobj if you do not need it, i.e.: if you don't write com objects yourself.
There's no real harm in adding comobj to the uses clause, but your code will be much larger.
Com code from comobj can not be always eliminated by the compiler/linker if you do not use it due to its run-time callable nature.
« Last Edit: February 13, 2017, 09:05:52 am by Thaddy »
I am more like donkey than shrek

RAW

  • Hero Member
  • *****
  • Posts: 867
Re: SystemVolume W6-10 CoInitialize CoUninitialize [MasterVolume, MasterMute]
« Reply #2 on: February 13, 2017, 10:17:27 pm »
Thank you very much for your explanation...
So the first code doesn't need coInitialize and coUninitialize because of COMOBJ, but if I don't need OLECHECK (it's optional), then I can use only ACTIVEX in combination with coInitialize and coUninitialize and eliminate COMOBJ. Sounds good...

Code: Pascal  [Select][+][-]
  1. Unit uMasterMuteW7;
  2.  {$MODE OBJFPC}{$H+}
  3.  
  4. Interface
  5.  
  6.  USES
  7.   Classes, SysUtils, Forms, Controls,
  8.   Windows, ActiveX,  ShellApi;
  9.  
  10.  TYPE
  11.   TMyBOOL = (myFalse = Integer(0),
  12.              myTrue  = Integer(1));
  13.  
  14.  TYPE
  15.   TwndGUI = Class(TForm)
  16.  
  17.    Procedure SetMasterVolume (    sinVol: Single );
  18.    Procedure GetMasterVolume (Out sinVol: Single );
  19.    Function  GetMasterMute              : TMyBOOL;
  20.    Procedure SetMasterMute   (     Value: TMyBOOL);
  21.  
  22.    Procedure FormKeyDown     (Sender: TObject; Var Key: Word;
  23.                               Shift : TShiftState);
  24.   End;
  25.  
  26.  // WINDOWS 7 VOLUME BEGIN
  27.  TYPE
  28.   IAudioEndpointVolumeCallback = Interface(IUnknown)
  29.    ['{657804FA-D6AD-4496-8A60-352752AF4F89}']
  30.   End;
  31.  
  32.   IAudioEndpointVolume = Interface(IUnknown)
  33.    ['{5CDF2C82-841E-4546-9722-0CF74078229A}']
  34.  
  35.     Function RegisterControlChangeNotify(
  36.               AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; StdCall;
  37.     Function UnregisterControlChangeNotify(
  38.               AudioEndPtVol: IAudioEndpointVolumeCallback): HRESULT; StdCall;
  39.     Function GetChannelCount(Out PInteger): HRESULT; StdCall;
  40.  
  41.     Function SetMasterVolumeLevel(
  42.               fLevelDB: Single; pguidEventContext: PGUID): HRESULT; StdCall;
  43.     Function SetMasterVolumeLevelScalar(
  44.               fLevelDB: Single; pguidEventContext: PGUID): HRESULT; StdCall;
  45.  
  46.     Function GetMasterVolumeLevel(
  47.               Out fLevelDB: Single): HRESULT; StdCall;
  48.     Function GetMasterVolumeLevelScalar(
  49.               Out fLevelDB: Single): HRESULT; StdCall;
  50.  
  51.     Function SetChannelVolumeLevel(
  52.               nChannel: Integer; fLevelDB: Double; pguidEventContext: PGUID):
  53.              HRESULT; StdCall;
  54.     Function SetChannelVolumeLevelScalar(
  55.               nChannel: Integer; fLevelDB: Double; pguidEventContext: PGUID):
  56.              HRESULT; StdCall;
  57.  
  58.     Function GetChannelVolumeLevel(
  59.               nChannel: Integer; Out fLevelDB: Double): HRESULT; StdCall;
  60.     Function GetChannelVolumeLevelScalar(
  61.               nChannel: Integer; Out fLevelDB: Double): HRESULT; StdCall;
  62.  
  63.     Function SetMute(
  64.               bSetMute: Bool; pguidEventContext: PGUID): HRESULT; StdCall;
  65.     Function GetMute(
  66.               Out bGetMute: Bool): HRESULT; StdCall;
  67.  
  68.     Function GetVolumeStepInfo(
  69.               pnStep: Integer; Out pnStepCount: Integer): HRESULT; StdCall;
  70.     Function VolumeStepUp(
  71.               pguidEventContext: PGUID): HRESULT; StdCall;
  72.     Function VolumeStepDown(
  73.               pguidEventContext: PGUID): HRESULT; StdCall;
  74.     Function GetVolumeRange(
  75.               Out pflVolumeMindB: Double; Out pflVolumeMaxdB: Double;
  76.               Out pflVolumeIncrementdB: Double): HRESULT; StdCall;
  77.     Function QueryHardwareSupport(
  78.               Out pdwHardwareSupportMask): HRESULT; StdCall;
  79.   End;
  80.  
  81.   IAudioMeterInformation = Interface(IUnknown)
  82.    ['{C02216F6-8C67-4B5B-9D00-D008E73E0064}']
  83.   End;
  84.  
  85.   IPropertyStore = Interface(IUnknown)
  86.   End;
  87.  
  88.   IMMDevice = Interface(IUnknown)
  89.    ['{D666063F-1587-4E43-81F1-B948E807363F}']
  90.     Function Activate(
  91.               Const refId: TGUID; dwClsCtx: DWORD; pActivationParams: PInteger;
  92.               Out pEndpointVolume: IAudioEndpointVolume): HRESULT; StdCall;
  93.     Function OpenPropertyStore(
  94.               stgmAccess: DWORD; Out ppProperties: IPropertyStore):
  95.              HRESULT; StdCall;
  96.     Function GetId   (Out ppstrId: PLPWSTR): HRESULT; StdCall;
  97.     Function GetState(Out State: Integer):   HRESULT; StdCall;
  98.   End;
  99.  
  100.   IMMDeviceCollection = interface(IUnknown)
  101.    ['{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}']
  102.   End;
  103.  
  104.   IMMNotificationClient = interface(IUnknown)
  105.    ['{7991EEC9-7E89-4D85-8390-6C703CEC60C0}']
  106.   End;
  107.  
  108.   IMMDeviceEnumerator = interface(IUnknown)
  109.    ['{A95664D2-9614-4F35-A746-DE8DB63617E6}']
  110.     Function EnumAudioEndpoints(
  111.               dataFlow: TOleEnum; deviceState: SYSUINT; DevCollection:
  112.               IMMDeviceCollection): HRESULT; StdCall;
  113.     Function GetDefaultAudioEndpoint(
  114.               EDF: SYSUINT; ER: SYSUINT; Out Dev: IMMDevice): HRESULT; StdCall;
  115.     Function GetDevice(pwstrId: Pointer; Out Dev: IMMDevice): HRESULT; StdCall;
  116.     Function RegisterEndpointNotificationCallback(
  117.               pClient: IMMNotificationClient): HRESULT; StdCall;
  118.   End;
  119.  
  120.  CONST
  121.   CLASS_IMMDeviceEnumerator: TGUID = '{BCDE0395-E52F-467C-8E3D-C4579291692E}';
  122.   IID_IMMDeviceEnumerator  : TGUID = '{A95664D2-9614-4F35-A746-DE8DB63617E6}';
  123.   IID_IAudioEndpointVolume : TGUID = '{5CDF2C82-841E-4546-9722-0CF74078229A}';
  124.  // WINDOWS 7 VOLUME END
  125.  
  126.  VAR
  127.   wndGUI: TwndGUI;
  128.  
  129. Implementation
  130.  {$R *.LFM}
  131.  
  132.  
  133. Procedure TwndGUI.FormKeyDown(Sender: TObject; Var Key: Word;
  134.                               Shift : TShiftState);
  135.   Var
  136.    myMute: TMyBOOL;
  137.    sinVol: Single;
  138.  Begin
  139.   // MIXER
  140.   If Key = Ord('V')
  141.   Then ShellExecute(0, Nil, PChar('sndvol.exe'), '', '', SW_SHOWNORMAL);
  142.  
  143.   // MUTE
  144.   If Key = Ord('M')
  145.   Then
  146.    Begin
  147.     myMute:= GetMasterMute;
  148.  
  149.     If myMute = myTrue
  150.     Then SetMasterMute(myFalse)
  151.     Else SetMasterMute(myTrue);
  152.    End;
  153.  
  154.   // MAX VOLUME
  155.   If Key = Ord('F')
  156.   Then
  157.    Begin
  158.     myMute:= GetMasterMute;
  159.  
  160.     If myMute = myTrue
  161.     Then SetMasterMute(myFalse);
  162.  
  163.     SetMasterVolume(1.0);
  164.    End;
  165.  
  166.   // VOLUME UP 1%
  167.   If Key = VK_UP
  168.   Then
  169.    Begin
  170.     GetMasterVolume(sinVol);
  171.    
  172.     If (1-sinVol) < 0.01
  173.     Then sinVol:= 1
  174.     Else sinVol:= sinVol+0.01;
  175.  
  176.     SetMasterVolume(sinVol);
  177.    End;
  178.  
  179.   // VOLUME DOWN 1%
  180.   If Key = VK_DOWN
  181.   Then
  182.    Begin
  183.     GetMasterVolume(sinVol);
  184.  
  185.     If (sinVol-0.01) < 0
  186.     Then sinVol:= 0
  187.     Else sinVol:= sinVol-0.01;
  188.  
  189.     SetMasterVolume(sinVol);
  190.    End;
  191.  End;
  192.  
  193.  
  194. Procedure TwndGUI.SetMasterVolume(sinVol: Single);
  195.   Var
  196.    EndpointVolume  : IAudioEndpointVolume;
  197.    DeviceEnumerator: IMMDeviceEnumerator;
  198.    Device          : IMMDevice;
  199.  Begin
  200.   CoCreateInstance(
  201.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  202.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  203.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  204.   Device.Activate(
  205.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  206.   EndpointVolume.SetMasterVolumeLevelScalar(sinVol, Nil);
  207.  End;
  208.  
  209.  
  210. Procedure TwndGUI.GetMasterVolume(Out sinVol: Single);
  211.   Var
  212.    EndpointVolume  : IAudioEndpointVolume;
  213.    DeviceEnumerator: IMMDeviceEnumerator;
  214.    Device          : IMMDevice;
  215.  Begin
  216.   CoCreateInstance(
  217.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  218.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  219.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  220.   Device.Activate(
  221.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  222.   EndpointVolume.GetMasterVolumeLevelScalar(sinVol);
  223.  End;
  224.  
  225.  
  226. Function TwndGUI.GetMasterMute: TMyBOOL;
  227.   Var
  228.    EndpointVolume  : IAudioEndpointVolume;
  229.    DeviceEnumerator: IMMDeviceEnumerator;
  230.    Device          : IMMDevice;
  231.  
  232.    bRes: BOOL;
  233.  Begin
  234.   CoCreateInstance(
  235.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  236.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  237.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  238.   Device.Activate(
  239.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  240.   EndpointVolume.GetMute(bRes);
  241.  
  242.   Result:= TMyBOOL(bRes);
  243.  End;
  244.  
  245.  
  246. Procedure TwndGUI.SetMasterMute(Value: TMyBOOL);
  247.   Var
  248.    EndpointVolume  : IAudioEndpointVolume;
  249.    DeviceEnumerator: IMMDeviceEnumerator;
  250.    Device          : IMMDevice;
  251.  Begin
  252.   CoCreateInstance(
  253.    CLASS_IMMDeviceEnumerator, Nil, CLSCTX_INPROC_SERVER,
  254.    IID_IMMDeviceEnumerator, DeviceEnumerator);
  255.   DeviceEnumerator.GetDefaultAudioEndpoint($00000000, $00000000, Device);
  256.   Device.Activate(
  257.    IID_IAudioEndpointVolume, CLSCTX_INPROC_SERVER, Nil, EndpointVolume);
  258.   EndpointVolume.SetMute(BOOL(Value), Nil);
  259.  End;
  260.  
  261. Initialization
  262.  coInitialize(Nil);
  263. Finalization
  264.  coUnInitialize;
  265.  
  266. End.
  267.  

I was a little bit confused, because if I don't use COMOBJ and don't use coInitialize and coUninitialize then it's still working.
Normally when I miss something, then I expect an error or any kind of exception or at least that the code is not working.
« Last Edit: March 23, 2017, 10:15:48 pm by RAW »
Windows 7 Pro (x64 Sp1) And Windows XP Pro (x86 Sp3) - LAZARUS 2.0.8 FPC 3.0.4 - TRUNK 2.1.0 FPC 3.3.1
// This is polarity (hell) and hell is reigned by lies, nothing but lies. Be careful what you believe ...
// There are 10 types of people in this world, those who understand mind control and ...

majid.ebru

  • Sr. Member
  • ****
  • Posts: 388
Hi
is it possible put your sample program , here?

 

TinyPortal © 2005-2018