2016-06-21 50 views
0

我试图通过凭证提供程序中的命名管道与Windows服务进行通信,但我不太确定将命名管道代码放在COM接口结构中的何处。我使用SampleHardwareEventCredentialProvider(微软)作为测试平台,和我创建内CSampleCredential.cpp下面的代码:凭据提供程序和命名管道

// Initializes one credential with the field information passed in. 
// Set the value of the SFI_USERNAME field to pwzUsername. 

HRESULT CSampleCredential::Initialize(
CREDENTIAL_PROVIDER_USAGE_SCENARIO cpus, 
const CREDENTIAL_PROVIDER_FIELD_DESCRIPTOR* rgcpfd, 
const FIELD_STATE_PAIR* rgfsp 
) 
{ 
HRESULT hr = S_OK; 

_cpus = cpus; 

// Copy the field descriptors for each field. This is useful if you want to vary the field 
// descriptors based on what Usage scenario the credential was created for. 
for (DWORD i = 0; SUCCEEDED(hr) && i < ARRAYSIZE(_rgCredProvFieldDescriptors); i++) 
{ 
    _rgFieldStatePairs[i] = rgfsp[i]; 
    hr = FieldDescriptorCopy(rgcpfd[i], &_rgCredProvFieldDescriptors[i]); 
} 

// Initialize named pipe 
if (SUCCEEDED(hr)) { 
    HANDLE pipe = CreateNamedPipe("\\\\.\\pipe\\PipeData", PIPE_ACCESS_INBOUND | PIPE_ACCESS_OUTBOUND, PIPE_WAIT, 1, 1024, 1024, 120 * 1000, NULL); 
    if (pipe == INVALID_HANDLE_VALUE) 
    { 
     cout << "Error: " << GetLastError(); 
    } 

    char data[1024]; 
    DWORD numRead; 


    ConnectNamedPipe(pipe, NULL); 

    ReadFile(pipe, data, 1024, &numRead, NULL); 
} 

这显然是不行的,除非我把它放在错误的地方或者不初始化CP以侦听来自Windows服务的传入消息?我将如何做到这一点?

回答

1

在你的windows服务中创建管道。由于您指的是SampleHardwareEventCredentialProvider示例,因此有CommandWindow.h及其cpp文件。

您需要使用CCommandWindow :: _ InitInstance方法中的CreateFile函数来打开创建的管道。

然后,您可以使用WriteFile和ReadFile函数轻松编写和读取管道。

随意询问是否有疑问

相关问题