我正在尝试使用以下代码捕获所有OutputDebugString消息(包括来自服务的消息)。在我迁移到 Windows 7 之前它运行良好。
问题在于,由于 Windows Vista 服务在低级别会话 #0 中运行,因此有些人说不可能捕获它们,而有些人则认为它是。你怎么看?
是否可以通过增加一些权限来修改以下代码,以便能够从 Session #0接收OutputDebugString消息?换句话说; 是否可以在会话 #0 中与会话 #1 共享 DBWIN_BUFFER?
我会说这应该是可能的,因为例如 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;
即使代码在 Delphi 中,我也添加了 C# 标记,因为安全属性对于整个 Windows API 是通用的,并且 C# 有很多追随者 :)