如何从服务接收OutputDebugString?

我正在尝试使用以下代码捕获所有OutputDebugString消息(包括来自服务的消息)。 它运行正常,直到我迁移到Windows 7。

问题是,由于Windows Vista服务在低级别的Session#0中运行,有些人说它不可能捕获它们而一些它是不可能的 。 你怎么看?

是否可以通过增加一些权限来修改以下代码,以便能够从Session#0接收OutputDebugString消息? 换一种说法; 是否可以在会话#0中与会话#0共享DBWIN_BUFFER?

我想它应该是可能的,因为例如DebugView可以做到这一点,并且我看不到任何服务助手会将这些消息(例如通过命名管道)从Session#0发送到GUI正在运行的Session#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; 

即使代码在Delphi中,我也添加了C#标签,因为安全属性对于整个Windows API都很常见,C#有很多粉丝:)

有人在SysInternals论坛上谈到了同样的问题。 他们的解决方案是将“Global”添加到命名对象中 。

所以使用以下内容

 CreateEvent(@SecurityAttributes, False, True, 'Global\DBWIN_BUFFER_READY'); CreateEvent(@SecurityAttributes, False, False, 'Global\DBWIN_DATA_READY'); CreateFileMapping(THandle(-1), @SecurityAttributes, PAGE_READWRITE, 0, 4096, 'Global\DBWIN_BUFFER');