Поделиться через


ptr::operator!

Operator to determine if the owned COM object is invalid.

bool operator!();

Возвращаемое значение

true if the owned COM object is invalid; false otherwise.

Заметки

The owned COM object is valid if it is not nullptr.

Пример

This example implements a CLR class that uses a com::ptr to wrap its private member IXMLDOMDocument object. The CreateInstance member function uses operator! to determine if a document object is already owned, and only creates a new instance if the object is invalid.

// comptr_op_not.cpp
// compile with: /clr /link msxml2.lib
#include <msxml2.h>
#include <msclr\com\ptr.h>

#import <msxml3.dll> raw_interfaces_only

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr;

// a ref class that uses a com::ptr to contain an 
// IXMLDOMDocument object
ref class XmlDocument {
public:
   void CreateInstance(String^ progid) {
      if (!m_ptrDoc) {
         m_ptrDoc.CreateInstance(progid);   
         if (m_ptrDoc) {
            Console::WriteLine("DOM Document created.");
         }
      }
   }

   // note that the destructor will call the com::ptr destructor
   // and automatically release the reference to the COM object

private:
   com::ptr<IXMLDOMDocument> m_ptrDoc;
};

// use the ref class to handle an XML DOM Document object
int main() {
   try {
      XmlDocument doc;
      // create the instance from a progid string
      doc.CreateInstance("Msxml2.DOMDocument.3.0");
   }
   catch (Exception^ e) {
      Console::WriteLine(e);   
   }
}

DOM Document created.

Требования

Header file <msclr\com\ptr.h>

Namespace msclr::com

См. также

Основные понятия

ptr Members

ptr::operator bool