03-26-2014 09:07 AM
Hi,
can I pass .net SequenceContext type as an argument to external dll function which recives CAObjHandle * ?
is CAObjHandle * points to a 'SequenceContext 'object ??
I tried without success :
public static void PassFailTest(SequenceContext seqContext, out bool errorOccurred, out int errorCode, out String errorMsg)
{
errorOccurred = false;
errorCode = 0;
errorMsg = String.Empty;
try
{
GCHandle objHandle = GCHandle.Alloc(seqContext, GCHandleType.Pinned);
IntPtr pointer = objHandle.AddrOfPinnedObject();
Lambda_PSU._PowerSupply_Init_session(pointer, 1, 1, "ZUP");
}
...
}
public class Lambda_PSU
{
[DllImport(@"C:\Production\FEM\Lambda Pwr Supply.dll")]
public static extern int _PowerSupply_Init_session(IntPtr seqContextCVI, int PS_Addr, UInt32 com_port, String PowerSupplyType);
}
03-26-2014 10:19 AM - edited 03-26-2014 10:20 AM
@dkjsf77 wrote:
Hi,
can I pass .net SequenceContext type as an argument to external dll function which recives CAObjHandle * ?
is CAObjHandle * points to a 'SequenceContext 'object ??
I tried without success :
public static void PassFailTest(SequenceContext seqContext, out bool errorOccurred, out int errorCode, out String errorMsg)
{
errorOccurred = false;
errorCode = 0;
errorMsg = String.Empty;try
{GCHandle objHandle = GCHandle.Alloc(seqContext, GCHandleType.Pinned);
IntPtr pointer = objHandle.AddrOfPinnedObject();
Lambda_PSU._PowerSupply_Init_session(pointer, 1, 1, "ZUP");}
...
}
public class Lambda_PSU
{
[DllImport(@"C:\Production\FEM\Lambda Pwr Supply.dll")]public static extern int _PowerSupply_Init_session(IntPtr seqContextCVI, int PS_Addr, UInt32 com_port, String PowerSupplyType);
}
Creating a CAObjHandle from .NET code is doable, but will be a bit tricky.
1) First, does your function take a CAObjHandle or a CAObjHandle *? It would be odd for a function to take a CAObjHandle * as an input parameter, so I suspect it really takes a CAObjHandle (check the prototype in your .c/.h code).
2) Next you will need to get the IUnknown pointer that corresponds to the SequenceContext object as follows:
IntPtr seqContextAsIUnknown = Marshal.GetIUnknownForObject(seqContext);
3) Finally you will need to call an API in the CVI libraries to convert the IUnknown pointer to a CAObjHandle. Since the CVI library is a C dll you'll have to create a dllimport for this. The CVI library function is called CA_CreateObjHandleFromInterface(). You're probably better off writing a wrapper function around this in your own dll that takes just the IUnknown pointer and returns the CAObjHandle.
Be careful to not create new reference leaks. The IUnknown pointer you get from Marshal.GetIUnknownForObject() returns a reference you own, and thus need to release when you are done with it.
Hope this helps,
-Doug