Delphi-PRAXiS
Seite 1 von 10  1 23     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi Das Handle ist ungültig (https://www.delphipraxis.net/196216-das-handle-ist-ungueltig.html)

EWeiss 2. Mai 2018 05:32


Das Handle ist ungültig
 
Wie kann ich das beheben bzw. auf was muss ich achten?
Ich starte einen Stream vom Internet über den IE11 anschließend führe ich TAudioVolume aus.
Bei normalen Prozessen ermittelt über die PID gibt es kein Problem.
Aber wenn hier die Session erstellt und die Abfrage über den Prozess läuft bekomme ich oben genannte Fehlermeldung.

Was kann man machen?
Das selbe Problem habe ich auch wenn die Sidebar.exe erkannt wird.

Delphi-Quellcode:
FAudioSessionList[Index].SessionGuid := GroupGuid;
HR := SessionControl2.GetDisplayName(pDisplayName);
if Succeeded(HR) then
begin
  FAudioSessionList[Index].DisplayName := pDisplayName;
  if AudioSessionInfo(Index).DisplayName = '' then
    FAudioSessionList[Index].DisplayName := GetBaseNameFromPID(PID);

Delphi-Quellcode:
function TAudioVolume.GetBaseNameFromPID(const PID: DWORD): string;
var
  hProcess: THandle;
  path: array [0 .. MAX_PATH - 1] of WChar;
  ErrorTxt : array [0..500] of char;
begin

  hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PID);
  if hProcess <> 0 then
    try
      if GetModuleBaseName(hProcess, 0, path, MAX_PATH) = 0 then
      begin
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, ErrorTxt, 500, nil);
        ShowMessage(ErrorTxt);
        //RaiseLastOSError;
      end;
      Result := path;
    finally
      CloseHandle(hProcess)
    end
  else
    RaiseLastOSError;
end;
Siehe shot! Das wird angezeigt.
Aber ich kann den Prozess steuern also die Lautstärke desselben.
Zudem bekomme ich so gut wie nie den DisplayName und den IconPath.
Eigentlich müsste der DisplayName wie beim Pic stream.png sein.

gruss

TiGü 2. Mai 2018 09:56

AW: Das Handle ist ungültig
 
iexplore.exe besteht ja MINDESTENS aus zwei Process-Instanzen, wenn ich ihn bei mir hier starte und sonst nichts mache (laut Task-Manager).
Probiere doch mal die andere PID aus.

EWeiss 2. Mai 2018 09:58

AW: Das Handle ist ungültig
 
Zitat:

Zitat von TiGü (Beitrag 1401041)
iexplore.exe besteht ja MINDESTENS aus zwei Process-Instanzen, wenn ich ihn bei mir hier starte und sonst nichts mache (laut Task-Manager).
Probiere doch mal die andere PID aus.

Wenn ich den IE starte habe ich nur einen Prozess in der liste vom Taskmanager IExplorer.exe
Danke.

Auf der anderen seite wie soll ich den anderen ausprobieren ?
Da habe ich keinen Einfluss drauf es wird die Session initialisiert die in dem Moment Sound wieder gibt!

gruss

TiGü 2. Mai 2018 10:25

AW: Das Handle ist ungültig
 
Versuch mal anstatt GetModuleBaseName die Funktion GetProcessImageFileNameW.

Beispiel:
Delphi-Quellcode:
program Project3;

{$APPTYPE CONSOLE}

{$R *.res}


uses
  System.SysUtils,
  Winapi.Windows,
  PsAPI;

function GetProcessImageFileNameW(hProcess: THandle; lpBaseName: LPCWSTR; nSize: DWORD): DWORD; stdcall; external 'PSAPI.dll';

function GetBaseNameFromPID(const PID: DWORD): string;
var
  hProcess: THandle;
  LModule: HMODULE;
  path: array [0 .. MAX_PATH - 1] of WideChar;
  ErrorTxt: array [0 .. 500] of WideChar;
begin

  hProcess := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, PID);
  if hProcess <> 0 then
    try
      LModule := 0;
      path := '';
      // if GetModuleBaseName(hProcess, LModule, path, MAX_PATH) = 0 then
      if GetProcessImageFileNameW(hProcess, path, MAX_PATH) = 0 then
      begin
        ErrorTxt := '';
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, ErrorTxt, 500, nil);
        Writeln(ErrorTxt);
        // RaiseLastOSError;
      end;
      Result := path;
    finally
      CloseHandle(hProcess)
    end
  else
    RaiseLastOSError;
end;

var
  gPID: DWORD;
  gName: string;

begin
  try
    while True do
    begin
      Writeln('Bitte PID eingeben');
      Readln(gPID);
      gName := GetBaseNameFromPID(gPID);
      Writeln(gName);
    end;
  except
    on E: Exception do
    begin
      Writeln(E.ClassName, ': ', E.Message);
      Readln;
    end;
  end;

end.

EWeiss 2. Mai 2018 10:39

AW: Das Handle ist ungültig
 
Danke damit funktioniert es ;)
Aber!

Zitat:

Zudem bekomme ich so gut wie nie den DisplayName und den IconPath.
Das ist immer noch nicht bereinigt..
Verstehe nicht das da nie was ankommt.

gruss

TiGü 2. Mai 2018 10:54

AW: Das Handle ist ungültig
 
Wie machste das denn?
:glaskugel: Glaskugel sagt: Ohne genauen Quelltext keine Aussage! :glaskugel:

EWeiss 2. Mai 2018 11:02

AW: Das Handle ist ungültig
 
Zitat:

Zitat von TiGü (Beitrag 1401054)
Wie machste das denn?
:glaskugel: Glaskugel sagt: Ohne genauen Quelltext keine Aussage! :glaskugel:

Nun ja denke Quelltext dafür hatte ich gepostet oder?
Delphi-Quellcode:
HR := SessionControl2.GetDisplayName(pDisplayName);


pDisplayName ist PWideChar und sollte den DisplayNamen zurück liefern tut er aber nicht.
Bei keiner Anwendung.

Wenn dieser leer ist erzwinge ich ihn das hat aber zur folge des er nicht korrekt ist.


gruss

TiGü 2. Mai 2018 11:08

AW: Das Handle ist ungültig
 
Was ist SessionControl2? Wo kommt es her? Wie wird es erzeugt? Was ist der Datentyp? Ist das ein Interface? Wie ist die Definition des Interfaces?

EWeiss 2. Mai 2018 11:15

AW: Das Handle ist ungültig
 
Zitat:

Zitat von TiGü (Beitrag 1401059)
Was ist SessionControl2? Wo kommt es her? Wie wird es erzeugt? Was ist der Datentyp? Ist das ein Interface? Wie ist die Definition des Interfaces?

Delphi-Quellcode:
unit AudioSessionControl;

interface

uses
  Windows, Messages, Classes, AudioSessionEvents;

const
  IID_IAudioSessionControl: TGUID = '{F4B1A599-7266-4319-A8CA-E70ACB11E8CD}';

type
  IAudioSessionControl = interface(IUnknown)
    ['{F4B1A599-7266-4319-A8CA-E70ACB11E8CD}']
    function GetState(out pRetVal: uint): HResult; stdcall;
    function GetDisplayName(out pRetVal: LPWSTR): HResult; stdcall;
    function SetDisplayName(Value: LPCWSTR; EventContext: pGuid): HResult; stdcall;
    function GetIconPath(out pRetVal: LPWSTR): HResult; stdcall;
    function SetIconPath(Value: LPCWSTR; EventContext: pGuid): HResult; stdcall;
    function GetGroupingParam(pRetVal: pGuid): HResult; stdcall;
    function SetGroupingParam(OverrideValue, EventContext: pGuid): HResult; stdcall;
    function RegisterAudioSessionNotification(const NewNotifications: IAudioSessionEvents)
      : HResult; stdcall;
    function UnregisterAudioSessionNotification(const NewNotifications: IAudioSessionEvents)
      : HResult; stdcall;
  end;

implementation

end.
Delphi-Quellcode:
unit AudioSessionControl2;

interface

uses
  Windows, Messages, Classes, AudioSessionControl;

const
  IID_IAudioSessionControl2: TGUID = '{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}';

type
  IAudioSessionControl2 = interface(IAudioSessionControl)
    ['{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}']
    function GetSessionIdentifier(out pRetVal: LPWSTR): Hresult; stdcall;
    function GetSessionInstanceIdentifier(out pRetVal: LPWSTR): Hresult; stdcall;
    function GetProcessId(out pRetVal: DWord): Hresult; stdcall;
    //indicates whether the session is a system sounds session.
    function IsSystemSoundsSession(): Hresult; stdcall;
    //S_OK = The session is a system sounds session.
    //S_FALSE = The session is not a system sounds session.
    function SetDuckingPreference(const optOut: Boolean): Hresult; stdcall;
    //A BOOL variable that enables or disables system auto-ducking.
  end;

implementation

end.
Delphi-Quellcode:
SessionControl2: IAudioSessionControl2;
....
    HR := AudioDevice.Activate(IID_IAudioSessionManager2, CLSCTX_ALL, nil, IUnknown(SessionManager2)
      );
    if Succeeded(HR) then
    begin
      HR := SessionManager2.GetSessionEnumerator(pEnumerator);
      if Succeeded(HR) then
      begin
        HR := pEnumerator.GetSession(Index, SessionControl);
        if Succeeded(HR) then
        begin
          HR := SessionControl.QueryInterface(IAudioSessionControl2, SessionControl2);
          if Succeeded(HR) then
gruss

TiGü 2. Mai 2018 11:15

AW: Das Handle ist ungültig
 
Zusatzfrage: Welchen Wert hat HR?
Delphi-Quellcode:
HR := SessionControl2.GetDisplayName(pDisplayName);


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:54 Uhr.
Seite 1 von 10  1 23     Letzte »    

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz