Answered by:
map DLL functions in C to C#

Question
-
Hello,
Here are declerations of DLL functions with defines in C:
#define PV_PTR_DECL *
#define PV_DECL __declspec(dllexport) __stdcall
typedef unsigned short rs_bool;
typedef char PV_PTR_DECL char_ptr;
typedef short int16, PV_PTR_DECL int 16_ptr;
typedef void PV_PTR_DECL void_ptr;
typedef void_ptr PV_PTR_DECL void_ptr_ptr;
typedef unsigned long uns32 PV_PTR_DECL uns32_ptr;
typedef
struct{ uns16 s1;
/* First pixel in the serial register */uns16 s2;
/* Last pixel in the serial register */}rgn_type, PV_PTR_DECL rgn_ptr;
typedef
const rgn_type PV_PTR_DECL rgn_const_ptr;/* DLL functions */
rs_bool PV_DECL pl_cam_get_name (int16 cam_num, char_ptr camera_name,void_ptr_ptr frame);
rs_bool PV_DECL pl_get_param ( uns32 param_id, int16_ptr status, void_ptr param_value,
rgn_const_ptr rgn_array,uns32_ptr exp_bytes);
How can I declare these two DLL functions in C#?
Thanks a lot,
Tammy
Saturday, March 3, 2007 5:35 PM
Answers
-
This probably is not 100% correct but here is a first pass.
[DllImport("")]
private static extern ushort pl_cam_get_name ( short cam_num, StringBuilder camera_name, IntPtr frame );
[DllImport("")]
private static extern ushort pl_get_param ( uint param_id, ref short status, IntPtr param_value, ref rgn_type rgn_array, uint exp_bytes )
[StructLayout(LayoutKind.Sequential)]
struct rgn_type
{
public ushort s1;
public ushort s2;
}Now the problems. Firstly the pl_cam_get_name appears to return a string but the length of the string buffer is never specified. Unless the buffer size is fixed this is a problem. If the buffer is fixed then you can use a marshaling attribute to specify the initial size. You might also need to have to pre-allocate the buffer in StringBuilder. I can't remember.
Next is the rgn_array in the second function. This appears to be an array of structures but again the length is not specified. If you know the length then you can again use an attribute to specify it. If exp_bytes is the total size of the array then you should probably pre-allocate a fixed # of structures, use the attribute to specify the length and then set exp_bytes accordingly.
Michael Taylor - 3/4/07
http://p3net.mvps.orgMonday, March 5, 2007 3:16 AM
All replies
-
This probably is not 100% correct but here is a first pass.
[DllImport("")]
private static extern ushort pl_cam_get_name ( short cam_num, StringBuilder camera_name, IntPtr frame );
[DllImport("")]
private static extern ushort pl_get_param ( uint param_id, ref short status, IntPtr param_value, ref rgn_type rgn_array, uint exp_bytes )
[StructLayout(LayoutKind.Sequential)]
struct rgn_type
{
public ushort s1;
public ushort s2;
}Now the problems. Firstly the pl_cam_get_name appears to return a string but the length of the string buffer is never specified. Unless the buffer size is fixed this is a problem. If the buffer is fixed then you can use a marshaling attribute to specify the initial size. You might also need to have to pre-allocate the buffer in StringBuilder. I can't remember.
Next is the rgn_array in the second function. This appears to be an array of structures but again the length is not specified. If you know the length then you can again use an attribute to specify it. If exp_bytes is the total size of the array then you should probably pre-allocate a fixed # of structures, use the attribute to specify the length and then set exp_bytes accordingly.
Michael Taylor - 3/4/07
http://p3net.mvps.orgMonday, March 5, 2007 3:16 AM -
Thanks Mike! That's help a lot.
(I have been sick last couple days so I didn't have time to try it out until this afternoon). Again, thanks a lot.
Tammy
Wednesday, March 7, 2007 2:30 AM