gpt4 book ai didi

c# - 如何从服务接收 OutputDebugString?

转载 作者:可可西里 更新时间:2023-11-01 08:25:12 26 4
gpt4 key购买 nike

我正在 try catch 所有 OutputDebugString使用以下代码的消息(包括来自服务的消息)。在我迁移到 Windows 7 之前它运行良好。

问题在于,由于 Windows Vista 服务在低级 session #0 中运行,有人说 it's impossible catch 他们和一些它是。你怎么看?

是否可以通过增加一些权限来修改以下代码,以便能够接收OutputDebugString来自 Session #0 的消息?换一种说法;是否可以在 session #0 中与 session #1 共享 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 forums 中谈到了同样的问题.他们的解决方案是 add "Global\" to the named objects .

所以使用下面的

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');

关于c# - 如何从服务接收 OutputDebugString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6384785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com