pointers - passing a HANDLE variable to an unmanaged .dll in C++/CLI -


i trying wrap unmanaged c++ dll talks video capture card in c++/cli can reference functions c# project have. having trouble getting 1st wrapped call work new c++/cli syntax. here have.

here function declataion trying wrap.

__declspec(dllimport) bool az_devicecreate(handle& hliveevent, dword* hencoderevent, dword* pdwencoderaddress, handle& haudioevent, dword& dwaudioaddress); 

here c++/cli .h file

namespace capturelibrary  {     public ref class capturecard     {     public:         handle m_hliveevent;         dword *m_hencoderevent;         handle m_haudioevent;      public:         capturecard();         bool createdevice();         void disposedevice();     }; } 

and .cpp

namespace capturelibrary {     capturecard::capturecard()     {         m_hliveevent = invalid_handle_value;          m_hencoderevent = new dword[max_video_channel];         (byte i=0;i<max_video_channel;i++)         {             m_hencoderevent[i] = (dword)invalid_handle_value;         }          m_haudioevent = invalid_handle_value;     }      bool capturecard::createdevice()     {         dword dwencoderbuff[max_video_channel];         dword dwacapturebuffer = 0;          if(az_devicecreate(m_hliveevent, m_hencoderevent, dwencoderbuff, m_haudioevent, dwacapturebuffer)==false)         {             return false;         }          return true;     }      void capturecard::disposedevice()     {         az_deviceclose();     } } 

when compile required headers, error:

error c2664: 'az_devicecreate' : cannot convert parameter 1 'handle' 'handle &'

can me know stupid syntax thing doing wrong.

thanks in advance.

i mean constructively: you're off on wrong foot. goal c++/cli here wrap unmanaged library in manner won't seem foreign in .net, capturecard class doesn't that.

  • don't expose fields, expose properties (i assume should get-only capturecard's members)
  • don't expose raw pointer types (e.g. handle), expose intptr
  • don't expose raw c-arrays (e.g. dword*), expose array<t>^, readonlycollection<t>^, or ienumerable<t>^ (but don't expose array<t>^s intended read-only via properties, via methods + array::copy)
  • don't expose disposedevice method, make class implement idisposable device can closed using statement rather forcing use of try..finally
  • as class controls unmanaged resources, needs finalizer

.h:

namespace capturelibrary  {     public ref class capturecard sealed     {     public:         capturecard();         ~capturecard();         !capturecard();          property intptr liveevent { intptr get(); }         property ienumerable<dword>^ encoderevent { ienumerable<dword>^ get(); }         property intptr audioevent { intptr get(); }          bool createdevice();         void disposedevice();      private:         bool m_bopened;         intptr m_hliveevent;         array<dword>^ m_hencoderevent;         intptr m_haudioevent;     }; } 

.cpp:

namespace capturelibrary {     capturecard::capturecard()       : m_hliveevent(invalid_handle_value),         m_hencoderevent(gcnew array<dword>(max_video_channel)),         m_haudioevent(invalid_handle_value)     {         (int = 0, i_max = m_hencoderevent->length; != i_max; ++i)             m_hencoderevent[i] = reinterpret_cast<dword>(invalid_handle_value);     }      capturecard::~capturecard()     {         this->!capturecard();     }      capturecard::!capturecard()     {         disposedevice();     }      intptr capturecard::liveevent::get()     {         return m_hliveevent;     }      ienumerable<dword>^ capturecard::encoderevent::get()     {         return m_hencoderevent;     }      intptr capturecard::audioevent::get()     {         return m_haudioevent;     }      bool capturecard::createdevice()     {         disposedevice();          dword dwaudioaddress = 0u;         dword dwencoderaddress[max_video_channel];          handle hliveevent = m_hliveevent.topointer();         handle haudioevent = m_haudioevent.topointer();         {             pin_ptr<dword> hencoderevent = &m_hencoderevent[0];             m_bopened = az_devicecreate(hliveevent, hencoderevent, dwencoderaddress, haudioevent, dwaudioaddress) == true;         }         m_hliveevent = intptr(hliveevent);         m_haudioevent = intptr(haudioevent);          return m_bopened;     }      void capturecard::disposedevice()     {         if (m_bopened)         {             az_deviceclose();             m_bopened = false;         }     } } 

suggestions further improvement:

  • get rid of createdevice , disposedevice altogether. code has c-ish mentality; .net users expect constructed object have meaningful value without calling separate initialization function, assuming az_devicecreate not expected fail regularly createdevice's logic should go straight in class' constructor , exception should thrown upon failure
  • if calling az_deviceclose multiple times harmless rid of m_bopened altogether

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -