nickdu
2009-04-27 20:28:10 UTC
I've written the following methods:
[StructLayout(LayoutKind.Sequential)]
public struct TRACE_GUID_REGISTRATION
{
public IntPtr Guid;
public IntPtr RegHandle;
}
[DllImport("advapi32.dll")]
public static extern uint RegisterTraceGuids(WMIDPREQUEST request, IntPtr
context,
ref Guid controlGuid, uint guidCount, IntPtr traceGuidReg, string
mofImagePath,
string mofResourceName, out ulong session);
public static uint RegisterTraceGuids(WMIDPREQUEST request, IntPtr context,
ref Guid controlGuid, ref TRACE_GUID_REGISTRATION[] traceGuidReg, string
mofImagePath,
string mofResourceName, out ulong session)
{
if (traceGuidReg == null)
throw(new ArgumentNullException("traceGuidReg"));
if (traceGuidReg.Length == 0)
throw(new ArgumentException("traceGuidReg.Length cannot be zero."));
uint size = (uint) Marshal.SizeOf(typeof(TRACE_GUID_REGISTRATION));
IntPtr buffer = Marshal.AllocHGlobal((int) (size * traceGuidReg.Length));
try
{
for (int i = 0; i < traceGuidReg.Length; ++i)
{
Marshal.StructureToPtr(traceGuidReg[i], (IntPtr) ((uint) buffer + (i *
size)), false);
}
uint result = RegisterTraceGuids(request, context, ref controlGuid, (uint)
traceGuidReg.Length,
buffer, mofImagePath, mofResourceName, out session);
if (result != 0)
return result;
for (int i = 0; i < traceGuidReg.Length; ++i)
{
Marshal.PtrToStructure((IntPtr) ((uint) buffer + (i * size)),
traceGuidReg[i]);
}
return 0;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
It appears calling my helper RegisterTraceGuids() works up until the point I
try to marshal the array back, from IntPtr to the managed array. Is there a
reason why I can't copy back into the array?
[StructLayout(LayoutKind.Sequential)]
public struct TRACE_GUID_REGISTRATION
{
public IntPtr Guid;
public IntPtr RegHandle;
}
[DllImport("advapi32.dll")]
public static extern uint RegisterTraceGuids(WMIDPREQUEST request, IntPtr
context,
ref Guid controlGuid, uint guidCount, IntPtr traceGuidReg, string
mofImagePath,
string mofResourceName, out ulong session);
public static uint RegisterTraceGuids(WMIDPREQUEST request, IntPtr context,
ref Guid controlGuid, ref TRACE_GUID_REGISTRATION[] traceGuidReg, string
mofImagePath,
string mofResourceName, out ulong session)
{
if (traceGuidReg == null)
throw(new ArgumentNullException("traceGuidReg"));
if (traceGuidReg.Length == 0)
throw(new ArgumentException("traceGuidReg.Length cannot be zero."));
uint size = (uint) Marshal.SizeOf(typeof(TRACE_GUID_REGISTRATION));
IntPtr buffer = Marshal.AllocHGlobal((int) (size * traceGuidReg.Length));
try
{
for (int i = 0; i < traceGuidReg.Length; ++i)
{
Marshal.StructureToPtr(traceGuidReg[i], (IntPtr) ((uint) buffer + (i *
size)), false);
}
uint result = RegisterTraceGuids(request, context, ref controlGuid, (uint)
traceGuidReg.Length,
buffer, mofImagePath, mofResourceName, out session);
if (result != 0)
return result;
for (int i = 0; i < traceGuidReg.Length; ++i)
{
Marshal.PtrToStructure((IntPtr) ((uint) buffer + (i * size)),
traceGuidReg[i]);
}
return 0;
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
It appears calling my helper RegisterTraceGuids() works up until the point I
try to marshal the array back, from IntPtr to the managed array. Is there a
reason why I can't copy back into the array?
--
Thanks,
Nick
***@community.nospam
remove "nospam" change community. to msn.com
Thanks,
Nick
***@community.nospam
remove "nospam" change community. to msn.com