I tried to use a C++ dll by fii,one of the function declared like this:
QCAP_CREATE( CHAR * pszDevName /*IN*/, UINT iDevNum /*IN*/, HWND hAttachedWindow /*IN*/, PVOID * ppDevice /*OUT*/, BOOL bThumbDraw = FALSE /*IN*/, BOOL bMaintainAspectRatio = FALSE /*IN*/ );
I tried to pass a null pointer to the third parameter "hAttachedWindow " by ref like this:
const ffi = require('ffi');
const ref = require('ref');
const path = require('path');
let dllPath = path.join(__dirname, '/QCAP.X64');
let intPtr = ref.refType('int');
let QCAP = ffi.Library(dllPath, {
'QCAP_CREATE': [ref.types.ulong, [ref.types.CString, ref.types.uint,
intPtr, intPtr, ref.types.bool, ref.types.bool]]
});
let ppdevice = ref.alloc('int');
let initResult = QCAP.QCAP_CREATE('CY3014 USB', 0, ref.NULL, ppdevice, true, false);
but the function keep throw an error said the third parameter is valid,I have imported this function in C# successfully like this,
[DllImport(@"D:\01work\01code\video\code\VideoDemo\CPlusPlus\obj\Debug\QCAP.X64.DLL", EntryPoint = "QCAP_CREATE")]
public static extern ulong QCAP_CREATE(string deviceName, UInt32 iDevNum, IntPtr hAttachedWindow, out IntPtr ppDevice, bool bThumbDraw, bool bMaintainAspectRatio);
static void Main(string[] args)
{
try
{
public IntPtr ppDevice = new IntPtr(0x00000000);
var result = QCAP_CREATE("CY3014 USB", 0, IntPtr.Zero, out ppDevice, true, false);
}
I have checked the source code of ref ,it said that ref.NULL is
/** A Buffer that references the C NULL pointer. */
so I think ref.NULL equals IntPtr.Zero in C#,but it provides me wrong. I have googled for this problem quite a long time but nothing helped,can anyone give some suggestion to me? Many thanks!
Part of the confusion you have is that the C# type IntPtr
does not represent a "pointer to int", but instead a "pointer interpreted as int".
Another part is that after peeling back the aliases to the types involved, the third and fourth parameters are different pointer types, void *
and void **
. The C# binding hides this, because of the out
qualifier passes the address of the IntPtr
you allocate, allowing the library function to modify it.
Try this
const ffi = require('ffi');
const ref = require('ref');
const path = require('path');
let dllPath = path.join(__dirname, '/QCAP.X64');
let voidPtr = ref.refType(ref.types.void);
let voidPtrPtr = ref.refType(voidPtr);
let QCAP = ffi.Library(dllPath, {
'QCAP_CREATE': [ref.types.ulong, [ref.types.CString, ref.types.uint,
voidPtr, voidPtrPtr, ref.types.bool, ref.types.bool]]
});
let ppdevice = ref.alloc(voidPtrPtr);
let initResult = QCAP.QCAP_CREATE('CY3014 USB', 0, ref.NULL, ppdevice, true, false);
let pdevice = ppdevice.deref();