在c ++中用Windows打开命名管道的ATL方法是什么?

use*_*729 2 c++ winapi atl

hPipe = CreateFile( 
         lpszPipename,   // pipe name 
         GENERIC_READ |  // read and write access 
         GENERIC_WRITE, 
         0,              // no sharing 
         NULL,           // default security attributes
         OPEN_EXISTING,  // opens existing pipe 
         0,              // default attributes 
         NULL);          // no template file 
Run Code Online (Sandbox Code Playgroud)

如何将上述ATL方法转换为方式,以便在销毁COM组件时自动关闭管道?

Joh*_*ing 6

我必须猜测你指的是什么COM组件,因为你的帖子很模糊.但我猜你已经编写了一个包装或以其他方式使用命名管道的COM组件,并且当这个COM组件被销毁时,你希望它自动释放(ala RAII).

在这种情况下,您可以在组件的FinalRelease()方法中放置任何清理代码,例如:

void CoMyObject::FinalRelease()
{
  CloseHandle(my_pipe_handle_);
}
Run Code Online (Sandbox Code Playgroud)

一次性清理硬币的另一面是一次性初始化代码.如果您还想在实例化COM组件时打开此命名管道 - 您的标题建议但您的帖子没有说明 - 那么您将在对象的FinalConstruct()方法中执行此操作:

HRESULT CoMyObject::FinalConstruct()
{
  my_pipe_handle_ = CreateFile( 
         lpszPipename,   // pipe name 
         GENERIC_READ |  // read and write access 
         GENERIC_WRITE, 
         0,              // no sharing 
         NULL,           // default security attributes
         OPEN_EXISTING,  // opens existing pipe 
         0,              // default attributes 
         NULL);          // no template file }

  return S_OK;
}
Run Code Online (Sandbox Code Playgroud)