public void GetFuncDesc(int index, out IntPtr ppFuncDesc) { // Fail BEFORE allocating the handle to avoid leaks. If the real COM object fails in this method // and doesn't return the handle or clean it up itself there's not much we can do to avoid the leak. _faultInjector.FailurePointThrow(MockTypeLibrariesFailurePoints.ITypeInfo_GetFuncDesc); ppFuncDesc = _memoryHelper.AllocateHandle(Marshal.SizeOf <FUNCDESC>()); _memoryHelper.EnterSubAllocationScope(ppFuncDesc); FUNCDESC funcDesc = new FUNCDESC(); funcDesc.lprgelemdescParam = _memoryHelper.AllocateHandle(_definedFunctions[index].parameters.Length * Marshal.SizeOf <ELEMDESC>()); funcDesc.cParams = (short)_definedFunctions[index].parameters.Length; for (int i = 0; i < _definedFunctions[index].parameters.Length; i++) { ELEMDESC elemDesc = new ELEMDESC(); elemDesc.tdesc = _definedFunctions[index].parameters[i].CreateTypeDesc( new IntPtr(index * s_HREF_FUNCSPARAM_OFFSET_PERFUNC + i + s_HREF_FUNCSPARAM_OFFSET), _memoryHelper); Marshal.StructureToPtr( elemDesc, new IntPtr(funcDesc.lprgelemdescParam.ToInt64() + i * Marshal.SizeOf <ELEMDESC>()), false); } funcDesc.elemdescFunc.tdesc = _definedFunctions[index].returnType.CreateTypeDesc( new IntPtr(index + s_HREF_FUNCSRET_OFFSET), _memoryHelper); _memoryHelper.ExitSubAllocationScope(); Marshal.StructureToPtr(funcDesc, ppFuncDesc, false); }
public static int LaunchDebugTargets(this IVsDebugger2 debugger, params VsDebugTargetInfo2[] targets) { IntPtr ptr = IntPtr.Zero; int marshalledCount = 0; try { ptr = Marshal.AllocHGlobal(targets.Length * Marshal.SizeOf(typeof(VsDebugTargetInfo2))); for (int i = 0; i < targets.Length; i++) { IntPtr current = new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(typeof(VsDebugTargetInfo2))); Marshal.StructureToPtr(targets[i], current, false); marshalledCount++; } return(debugger.LaunchDebugTargets2((uint)targets.Length, ptr)); } finally { if (ptr != IntPtr.Zero) { for (int i = 0; i < marshalledCount; i++) { IntPtr current = new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(typeof(VsDebugTargetInfo2))); Marshal.DestroyStructure(current, typeof(VsDebugTargetInfo2)); } Marshal.FreeHGlobal(ptr); ptr = IntPtr.Zero; } } }
public void GetLibAttr(out IntPtr ppTLibAttr) { // Fail BEFORE allocating the handle to avoid leaks. If the real COM object fails in this method // and doesn't return the handle or clean it up itself there's not much we can do to avoid the leak. _faultInjector.FailurePointThrow(MockTypeLibrariesFailurePoints.ITypeLib_GetLibAttr); ppTLibAttr = _memoryHelper.AllocateHandle(Marshal.SizeOf(typeof(TYPELIBATTR))); Marshal.StructureToPtr(this.Attributes, ppTLibAttr, false); }
public TYPEDESC CreateTypeDesc(IntPtr finalTypeHRef, MockUnmanagedMemoryHelper memoryHelper) { TYPEDESC typeDesc; typeDesc.vt = (short)VarEnum.VT_PTR; typeDesc.lpValue = memoryHelper.AllocateHandle(Marshal.SizeOf <TYPEDESC>()); Marshal.StructureToPtr(_baseElementType.CreateTypeDesc(finalTypeHRef, memoryHelper), typeDesc.lpValue, false); return(typeDesc); }
/// <summary> /// 構造体を Stream に書き込みます。動作確認済み。 /// </summary> /// <param name="structure">書き込む構造体を指定します。</param> /// <param name="strlen">構造体の長さを指定します。</param> /// <param name="copylen">書き込む長さを指定します。</param> private void WriteStructure(System.ValueType structure, int strlen, int copylen) { System.IntPtr buff = Marshal.AllocHGlobal(strlen); try{ byte[] data = new byte[copylen]; Marshal.StructureToPtr(structure, buff, false); Marshal.Copy(buff, data, 0, strlen); str.Write(data, 0, copylen); }finally{ Marshal.FreeHGlobal(buff); } }
public static int LaunchDebugTargets(this IVsDebugger2 debugger, params DebugTargetInfo[] targets) { VsDebugTargetInfo2[] vstargets = new VsDebugTargetInfo2[targets.Length]; try { for (int i = 0; i < targets.Length; i++) { vstargets[i].bstrArg = targets[i].Arguments; vstargets[i].bstrCurDir = targets[i].CurrentDirectory; vstargets[i].bstrEnv = GetEnvironmentString(targets[i].Environment); vstargets[i].bstrExe = targets[i].Executable; vstargets[i].bstrOptions = targets[i].Options; vstargets[i].bstrPortName = targets[i].PortName; vstargets[i].bstrRemoteMachine = targets[i].RemoteMachine; vstargets[i].cbSize = (uint)Marshal.SizeOf(typeof(VsDebugTargetInfo2)); vstargets[i].dlo = (uint)targets[i].LaunchOperation; vstargets[i].dwProcessId = targets[i].ProcessId; vstargets[i].dwReserved = 0; vstargets[i].fSendToOutputWindow = targets[i].SendToOutputWindow ? 1 : 0; vstargets[i].guidLaunchDebugEngine = Guid.Empty; vstargets[i].guidPortSupplier = targets[i].PortSupplier; vstargets[i].guidProcessLanguage = targets[i].ProcessLanguage; //vstargets[i].hStdError = targets[i].StdError; //vstargets[i].hStdInput = targets[i].StdInput; //vstargets[i].hStdOutput = targets[i].StdOutput; vstargets[i].LaunchFlags = (uint)targets[i].LaunchFlags; vstargets[i].pUnknown = null; vstargets[i].dwDebugEngineCount = (uint)targets[i].DebugEngines.Length; vstargets[i].pDebugEngines = Marshal.AllocHGlobal(targets[i].DebugEngines.Length * Marshal.SizeOf(typeof(Guid))); for (int j = 0; j < targets[i].DebugEngines.Length; j++) { Marshal.StructureToPtr(targets[i].DebugEngines[j], new IntPtr(vstargets[i].pDebugEngines.ToInt64() + j * Marshal.SizeOf(typeof(Guid))), false); } } return(debugger.LaunchDebugTargets(vstargets)); } finally { for (int i = 0; i < vstargets.Length; i++) { if (vstargets[i].pDebugEngines != IntPtr.Zero) { Marshal.FreeHGlobal(vstargets[i].pDebugEngines); } } } }
public void GetVarDesc(int index, out IntPtr ppVarDesc) { // Fail BEFORE allocating the handle to avoid leaks. If the real COM object fails in this method // and doesn't return the handle or clean it up itself there's not much we can do to avoid the leak. _faultInjector.FailurePointThrow(MockTypeLibrariesFailurePoints.ITypeInfo_GetVarDesc); ppVarDesc = _memoryHelper.AllocateHandle(Marshal.SizeOf <VARDESC>()); _memoryHelper.EnterSubAllocationScope(ppVarDesc); VARDESC varDesc = new VARDESC(); varDesc.elemdescVar.tdesc = _definedVariables[index].CreateTypeDesc(new IntPtr(index + s_HREF_VARS_OFFSET), _memoryHelper); _memoryHelper.ExitSubAllocationScope(); Marshal.StructureToPtr(varDesc, ppVarDesc, false); }
/// <devdoc> /// Copy the EncoderParameters data into a chunk of memory to be consumed by native GDI+ code. /// /// We need to marshal the EncoderParameters info from/to native GDI+ ourselve since the definition of the managed/unmanaged classes /// are different and the native class is a bit weird. The native EncoderParameters class is defined in GDI+ as follows: /// /// class EncoderParameters { /// UINT Count; // Number of parameters in this structure /// EncoderParameter Parameter[1]; // Parameter values /// }; /// /// We don't have the 'Count' field since the managed array contains it. In order for this structure to work with more than one /// EncoderParameter we need to preallocate memory for the extra n-1 elements, something like this: /// /// EncoderParameters* pEncoderParameters = (EncoderParameters*) malloc(sizeof(EncoderParameters) + (n-1) * sizeof(EncoderParameter)); /// /// Also, in 64-bit platforms, 'Count' is aligned in 8 bytes (4 extra padding bytes) so we use IntPtr instead of Int32 to account for /// that. /// </devdoc> internal IntPtr ConvertToMemory() { int size = Marshal.SizeOf(typeof(EncoderParameter)); int length = _param.Length; IntPtr memory = Marshal.AllocHGlobal(checked (length * size + Marshal.SizeOf(typeof(IntPtr)))); if (memory == IntPtr.Zero) { throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.OutOfMemory); } Marshal.WriteIntPtr(memory, (IntPtr)length); long arrayOffset = checked ((long)memory + Marshal.SizeOf(typeof(IntPtr))); for (int i = 0; i < length; i++) { Marshal.StructureToPtr(_param[i], (IntPtr)(arrayOffset + i * size), false); } return(memory); }
internal static DeviceInfo[] GetList() { string[] passDevices = Directory.GetFiles("/dev/", "pass*", SearchOption.TopDirectoryOnly); List <DeviceInfo> listDevices = new List <DeviceInfo>(); foreach (string passDevice in passDevices) { var deviceInfo = new DeviceInfo(); IntPtr dev = cam_open_device(passDevice, FileFlags.ReadWrite); var camDevice = (CamDevice)Marshal.PtrToStructure(dev, typeof(CamDevice)); IntPtr ccbPtr = cam_getccb(dev); if (ccbPtr.ToInt64() == 0) { continue; } var cgd = (CcbGetdev)Marshal.PtrToStructure(ccbPtr, typeof(CcbGetdev)); cgd.ccb_h.func_code = XptOpcode.XptGdevType; Marshal.StructureToPtr(cgd, ccbPtr, false); int error = cam_send_ccb(dev, ccbPtr); if (error < 0) { cam_freeccb(ccbPtr); continue; } cgd = (CcbGetdev)Marshal.PtrToStructure(ccbPtr, typeof(CcbGetdev)); cam_freeccb(ccbPtr); cam_close_device(dev); string simName = StringHandlers.CToString(camDevice.SimName); deviceInfo.Path = passDevice; byte[] serialNumber = new byte[camDevice.SerialNumLen]; Array.Copy(camDevice.SerialNum, 0, serialNumber, 0, serialNumber.Length); deviceInfo.Serial = StringHandlers.CToString(serialNumber); switch (cgd.protocol) { case CamProto.ProtoAta: case CamProto.ProtoAtapi: case CamProto.ProtoSatapm: { // Little-endian FreeBSD gives it resorted // Big-endian FreeBSD, no idea byte[] atadTneid = new byte[512]; for (int aIndex = 0; aIndex < 512; aIndex += 2) { atadTneid[aIndex] = cgd.ident_data[aIndex + 1]; atadTneid[aIndex + 1] = cgd.ident_data[aIndex]; } Identify.IdentifyDevice?idt = Identify.Decode(atadTneid); if (idt.HasValue) { string[] separated = idt.Value.Model.Split(' '); if (separated.Length == 1) { deviceInfo.Vendor = "ATA"; deviceInfo.Model = separated[0]; } else { deviceInfo.Vendor = separated[0]; deviceInfo.Model = separated[^ 1];