OciEnvironmentHandle.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // OciEnvironmentHandle.cs
  3. //
  4. // Part of managed C#/.NET library System.Data.OracleClient.dll
  5. //
  6. // Part of the Mono class libraries at
  7. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
  8. //
  9. // Assembly: System.Data.OracleClient.dll
  10. // Namespace: System.Data.OracleClient.Oci
  11. //
  12. // Author:
  13. // Tim Coleman <[email protected]>
  14. //
  15. // Copyright (C) Tim Coleman, 2003
  16. //
  17. using System;
  18. using System.Runtime.InteropServices;
  19. namespace System.Data.OracleClient.Oci {
  20. internal class OciEnvironmentHandle : IOciHandle, IDisposable
  21. {
  22. #region Fields
  23. IntPtr handle;
  24. #endregion // Fields
  25. #region Constructors
  26. public OciEnvironmentHandle ()
  27. : this (OciEnvironmentMode.Default)
  28. {
  29. }
  30. public OciEnvironmentHandle (OciEnvironmentMode mode)
  31. {
  32. int status = 0;
  33. this.handle = IntPtr.Zero;
  34. status = OCIEnvCreate (out handle, mode,
  35. IntPtr.Zero,
  36. IntPtr.Zero,
  37. IntPtr.Zero,
  38. IntPtr.Zero,
  39. 0,
  40. IntPtr.Zero);
  41. }
  42. #endregion // Constructors
  43. #region Properties
  44. public IntPtr Handle {
  45. get { return handle; }
  46. set { handle = value; }
  47. }
  48. public OciHandleType HandleType {
  49. get { return OciHandleType.Environment; }
  50. }
  51. #endregion // Properties
  52. #region Methods
  53. [DllImport ("oci")]
  54. public static extern int OCIEnvCreate (out IntPtr envhpp,
  55. [MarshalAs (UnmanagedType.U4)] OciEnvironmentMode mode,
  56. IntPtr ctxp,
  57. IntPtr malocfp,
  58. IntPtr ralocfp,
  59. IntPtr mfreep,
  60. int xtramem_sz,
  61. IntPtr usrmempp);
  62. [DllImport ("oci")]
  63. public static extern int OCIHandleAlloc (IntPtr parenth,
  64. out IntPtr hndlpp,
  65. [MarshalAs (UnmanagedType.U4)] OciHandleType type,
  66. int xtramem_sz,
  67. IntPtr usrmempp);
  68. [DllImport ("oci")]
  69. public static extern int OCIHandleFree (IntPtr hndlp,
  70. [MarshalAs (UnmanagedType.U4)] OciHandleType type);
  71. public IOciHandle Allocate (OciHandleType type)
  72. {
  73. IntPtr newHandle = IntPtr.Zero;
  74. int status = 0;
  75. status = OCIHandleAlloc (Handle,
  76. out newHandle,
  77. type,
  78. 0,
  79. IntPtr.Zero);
  80. if (status != 0 && status != 1)
  81. return null;
  82. switch (type) {
  83. case OciHandleType.Service:
  84. return new OciServiceHandle (this, newHandle);
  85. case OciHandleType.Error:
  86. return new OciErrorHandle (this, newHandle);
  87. case OciHandleType.Server:
  88. return new OciServerHandle (this, newHandle);
  89. case OciHandleType.Session:
  90. return new OciSessionHandle (this, newHandle);
  91. case OciHandleType.Statement:
  92. return new OciStatementHandle (this, newHandle);
  93. case OciHandleType.Transaction:
  94. return new OciTransactionHandle (this, newHandle);
  95. default:
  96. OCIHandleFree (newHandle, type);
  97. newHandle = IntPtr.Zero;
  98. throw new ArgumentException ("Invalid handle type.");
  99. }
  100. }
  101. public void Dispose ()
  102. {
  103. FreeHandle (this);
  104. }
  105. public void FreeHandle (IOciHandle toBeDisposed)
  106. {
  107. OCIHandleFree (toBeDisposed.Handle, toBeDisposed.HandleType);
  108. toBeDisposed.Handle = IntPtr.Zero;
  109. }
  110. public OciErrorInfo HandleError ()
  111. {
  112. int errbufSize = 512;
  113. IntPtr errbuf = Marshal.AllocHGlobal (errbufSize);
  114. OciErrorInfo info;
  115. info.ErrorCode = 0;
  116. info.ErrorMessage = String.Empty;
  117. OciGlue.OCIErrorGet (handle,
  118. 1,
  119. IntPtr.Zero,
  120. out info.ErrorCode,
  121. errbuf,
  122. (uint) errbufSize,
  123. OciHandleType.Environment);
  124. object err = Marshal.PtrToStringAnsi (errbuf);
  125. if (err != null) {
  126. string errmsg = (string) err;
  127. info.ErrorMessage = String.Copy (errmsg);
  128. Marshal.FreeHGlobal (errbuf);
  129. }
  130. return info;
  131. }
  132. #endregion // Methods
  133. }
  134. }