次のコードを使用して、すべてのOutputDebugStringメッセージ (サービスからのものを含む) をキャッチしようとしています。Windows 7 に移行するまでは問題なく動作していました。
問題は、Windows Vista のサービスが低レベルのセッション #0 で実行されているため、キャッチできないという人もいれば、キャッチできないという人もいるということです。どう思いますか?
セッション #0 からOutputDebugStringメッセージを受信できるようにいくつかの権限を増やして、次のコードを変更することは可能ですか? 言い換えると; セッション #0 の DBWIN_BUFFER をセッション #1 と共有することはできますか?
たとえば DebugView がそれを行うことができ、セッション #0 から GUI が実行されているセッション #1 にそれらのメッセージを (たとえば名前付きパイプを介して) 送信するサービスヘルパーが表示されないため、可能であると言えます。
問題は、セキュリティ設定の IMO です。誰かがそれらを変更する方法を提案できますか?
type
TODSThread = class(TThread)
protected
procedure Execute; override;
end;
...
procedure TODSThread.Execute;
var SharedMem: Pointer;
SharedFile: THandle;
WaitingResult: DWORD;
SharedMessage: string;
DataReadyEvent: THandle;
BufferReadyEvent: THandle;
SecurityAttributes: SECURITY_ATTRIBUTES;
SecurityDescriptor: SECURITY_DESCRIPTOR;
begin
SecurityAttributes.nLength := SizeOf(SECURITY_ATTRIBUTES);
SecurityAttributes.bInheritHandle := True;
SecurityAttributes.lpSecurityDescriptor := @SecurityDescriptor;
if not InitializeSecurityDescriptor(@SecurityDescriptor, SECURITY_DESCRIPTOR_REVISION) then
Exit;
if not SetSecurityDescriptorDacl(@SecurityDescriptor, True, nil, False) then
Exit;
BufferReadyEvent := CreateEvent(@SecurityAttributes, False, True, 'DBWIN_BUFFER_READY');
if BufferReadyEvent = 0 then
Exit;
DataReadyEvent := CreateEvent(@SecurityAttributes, False, False, 'DBWIN_DATA_READY');
if DataReadyEvent = 0 then
Exit;
SharedFile := CreateFileMapping(THandle(-1), @SecurityAttributes, PAGE_READWRITE, 0, 4096, 'DBWIN_BUFFER');
if SharedFile = 0 then
Exit;
SharedMem := MapViewOfFile(SharedFile, FILE_MAP_READ, 0, 0, 512);
if not Assigned(SharedMem) then
Exit;
while (not Terminated) and (not Application.Terminated) do
begin
SetEvent(BufferReadyEvent);
WaitingResult := WaitForSingleObject(DataReadyEvent, INFINITE);
case WaitingResult of
WAIT_TIMEOUT: Continue;
WAIT_OBJECT_0:
begin
SharedMessage := String(PAnsiChar(SharedMem) + SizeOf(DWORD));
// here I have what I need and process it in the main thread
end;
WAIT_FAILED: Continue;
end;
end;
UnmapViewOfFile(SharedMem);
CloseHandle(SharedFile);
end;
セキュリティ属性は Windows API 全体に共通であり、C# には多くのフォロワーがいるため、コードが Delphi にある場合でも C# タグを追加しました :)