BAOS .net SDK  18.2.0
kdriveManagedInterop.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2002-2018 WEINZIERL ENGINEERING GmbH
3 // All rights reserved.
4 //
5 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
8 // SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
9 // WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
11 //
12 
13 #pragma once
14 
15 #include <windows.h>
16 #include <Poco/Exception.h>
17 
18 namespace unmanaged
19 {
20 
22 {
23  void operator()(void const*) const
24  {
25  }
26 };
27 
28 } // end namespace unmanaged
29 
30 namespace kdrive
31 {
32 namespace interop
33 {
34 
35 /*********************************
36 ** String Conversions
37 **********************************/
38 
40 {
41 public:
42 
43  StringConvA(System::String^ s)
44  : szAnsi_(static_cast<char*>(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer()))
45  {
46  }
47 
49  {
50  System::Runtime::InteropServices::Marshal::FreeHGlobal(System::IntPtr(szAnsi_));
51  }
52 
53  operator LPCSTR() const
54  {
55  return szAnsi_;
56  }
57 
58 private:
59  char* szAnsi_;
60 };
61 
63 {
64 public:
65 
66  StringConvW(System::String^ s)
67  : szUnicode_(static_cast<wchar_t*>(System::Runtime::InteropServices::Marshal::StringToHGlobalUni(s).ToPointer()))
68  {
69  }
70 
72  {
73  System::Runtime::InteropServices::Marshal::FreeHGlobal(System::IntPtr(szUnicode_));
74  }
75 
76  operator LPCWSTR() const
77  {
78  return szUnicode_;
79  }
80 
81 private:
82  wchar_t* szUnicode_;
83 };
84 
85 #ifdef _UNICODE
86 #define StringConvT StringConvW
87 #else
88 #define StringConvT StringConvA
89 #endif
90 
91 /*********************************
92 ** Buffer Conversions
93 **********************************/
94 
96 {
97  static array<unsigned char>^ Copy(const unsigned char* buffer, int bufferSize)
98  {
99  array<unsigned char>^ a = gcnew array<unsigned char>(bufferSize);
100  for (int i = 0; i < bufferSize; ++i)
101  {
102  a[i] = buffer[i];
103  }
104  return a;
105  }
106 };
107 
108 } // end namespace interop
109 
110 /*********************************
111 ** Exception Conversions
112 **********************************/
113 
114 public ref class KdriveException : public System::ApplicationException
115 {
116 public:
118 
119  : ApplicationException()
120  {
121  }
122 
123  KdriveException(System::String^ message)
124 
125  : ApplicationException(message)
126  {
127  }
128 
129 
130  KdriveException(System::String^ message, System::Exception^ innerException)
131 
132  : ApplicationException(message, innerException)
133  {
134  }
135 
136 protected:
137  KdriveException(System::Runtime::Serialization::SerializationInfo^ info, System::Runtime::Serialization::StreamingContext context)
138 
139  : ApplicationException(info, context)
140  {
141  }
142 
143 };
144 
146 {
147  static void Rethrow(std::exception& exception)
148  {
149  Poco::Exception* e = dynamic_cast<Poco::Exception*>(&exception);
150  const std::string strError = e ? e->displayText() : exception.what();
151  const std::wstring wstrError(strError.begin(), strError.end());
152  System::String^ string = gcnew System::String(wstrError.c_str());
153  throw gcnew KdriveException(string);
154  }
155 };
156 
157 } // end namespace kdrive
158 
159 // The default exception handler to convert a std::exception / Poco::Exception
160 // into a managed C++ kdrive::rpc::netnnode::bindings::Exception
161 #define WZ_KDRIVE_CLR_DEFAULT_CATCH \
162  catch (std::exception& exception) { kdrive::ExceptionHandler::Rethrow(exception); } \
163  catch (...) { throw; }
164 
165 // use this macro if you are returning a value from the function
166 // i.e. once this handler is invoked you never come out again as
167 // it throws an exception regardless
168 // this is to get rid of the compiler warning that not all return paths are covered
169 // i.e. either the return is successful, or we catch the exception and rethrow
170 #define WZ_KDRIVE_CLR_CATCH_BLACK_HOLE \
171  catch (std::exception& exception) { kdrive::ExceptionHandler::Rethrow(exception); } \
172  catch (...) { throw; } \
173  throw gcnew kdrive::KdriveException("Should Never Reach This Exception!");
Definition: kdriveManagedInterop.h:62
Definition: kdriveManagedInterop.h:39
StringConvW(System::String^s)
Definition: kdriveManagedInterop.h:66
StringConvA(System::String^s)
Definition: kdriveManagedInterop.h:43
Definition: kdriveManagedInterop.h:95
KdriveException(System::String^message, System::Exception^innerException)
Definition: kdriveManagedInterop.h:130
KdriveException()
Definition: kdriveManagedInterop.h:117
KdriveException(System::Runtime::Serialization::SerializationInfo^info, System::Runtime::Serialization::StreamingContext context)
Definition: kdriveManagedInterop.h:137
Definition: BaosConnection.h:31
static array< unsigned char > Copy(const unsigned char *buffer, int bufferSize)
Definition: kdriveManagedInterop.h:97
~StringConvA()
Definition: kdriveManagedInterop.h:48
Definition: kdriveManagedInterop.h:21
KdriveException(System::String^message)
Definition: kdriveManagedInterop.h:123
Definition: kdriveManagedInterop.h:114
Definition: BaosConnection.h:22
void operator()(void const *) const
Definition: kdriveManagedInterop.h:23
static void Rethrow(std::exception &exception)
Definition: kdriveManagedInterop.h:147
Definition: kdriveManagedInterop.h:145
~StringConvW()
Definition: kdriveManagedInterop.h:71