From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

NI TestStand

cancel
Showing results for 
Search instead for 
Did you mean: 

pass .net SequenceContext type as an argument to external dll function which recives CAObjHandle *

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);
}

0 Kudos
Message 1 of 2
(3,807 Views)

@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

0 Kudos
Message 2 of 2
(3,802 Views)