I wrote a DLL in unmanaged visual C ++, and I have a slight problem with making it work with both C # and C ++ applications. Here's what the prototype looks like in the C ++ DLL:
extern "C" __declspec(dllexport) int WINAPI ZBNConnect( UCHAR dev, LPARAM hWnd, ZBCallbackFn rfn, ZBCallbackFn nfn, int DevType, byte * DevAddr, ZBCallbackFn dfn );
My C # application can reference this function without problems, but when it tries to call the function, an exception is thrown:
catch (Exception e) { }
e.Message = "The reference to the object is not installed in the instance of the object."
Oddly enough, if I take it WINAPIout of the prototype into a DLL and recompile, the C # application calls the function without any problems. Unfortunately, it WINAPImust remain, because this is how a function is defined in a C ++ application.
This feature is currently being prototyped in a C # application as follows:
public delegate int ZBNConnectDelegate(uint dev, IntPtr hWnd, USBCallbackDelegate rfn, NotifyCallbackDelegate nfn, uint DevType, byte[] DevAddr, ZBdebugCallbackDelegate dfn);
public ZBNConnectDelegate ZBNConnect;
procName = "ZBNConnect";
fUintPtr = Kernel32.GetProcAddress(dllHandle, procName);
if (fUintPtr == UIntPtr.Zero)
{
throw new ArgumentException(procName);
}
fIntPtr = unchecked((IntPtr)(long)(ulong)fUintPtr);
ZBNConnect = (ZBNConnectDelegate)Marshal.GetDelegateForFunctionPointer(fIntPtr, typeof(ZBNConnectDelegate));
#, ? .
EDIT:
([DllImport...]) , , , DLL, . DLL API.