I am trying to call native.dll from C # using p / invoke. I can make a call (no failure, the function returns a value), but the return code indicates "The pointer parameter does not indicate the available memory." I resorted to trial and error to solve this problem, but I have not made any progress so far.
Here is the signature of the native function that I am calling:
LONG extern WINAPI MyFunction ( LPSTR lpszLogicalName, //input HANDLE hApp, //input LPSTR lpszAppID, //input DWORD dwTraceLevel, //input DWORD dwTimeOut, //input DWORD dwSrvcVersionsRequired, //input LPWFSVERSION lpSrvcVersion, //WFSVERSION*, output LPWFSVERSION lpSPIVersion, //WFSVERSION*, output LPHSERVICE lphService //unsigned short*, output );
Here's the imported signature in C #:
[DllImport("my.dll")] public static extern int MyFunction( [MarshalAs(UnmanagedType.LPStr)] string logicalName, IntPtr hApp, [MarshalAs(UnmanagedType.LPStr)] string appID, int traceLevel, int timeout, int srvcVersionsRequired, [Out] WFSVersion srvcVersion, [Out] WFSVersion spiVersion, [Out] UInt16 hService );
Here's the definition of C WFSVERSION:
typedef struct _wfsversion { WORD wVersion; WORD wLowVersion; WORD wHighVersion; CHAR szDescription[257]; CHAR szSystemStatus[257]; } WFSVERSION, * LPWFSVERSION;
Here is the C # definition for WFSVersion:
[StructLayout(LayoutKind.Sequential)] public class WFSVersion { public Int16 wVersion; public Int16 wLowVersion; public Int16 wHighVersion; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 257)] public char[] szDescription; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 257)] public char[] szSystemStatus; }
Here's the call to MyFunction from C #:
WFSVersion srvcVersionInfo = new WFSVersion(); WFSVersion spiVersionInfo = new WFSVersion(); UInt16 hService = 0; IntPtr hApp = IntPtr.Zero; string logicalServiceName = tbServiceName.Text; int openResult = MyFunction(logicalServiceName, hApp, null, 0, XFSConstants.WFS_INDEFINITE_WAIT, 0x00000004, srvcVersionInfo, spiVersionInfo, hService);
As I said, this call returns, but the return value is an error code indicating "The pointer parameter does not indicate available memory." I should be doing something wrong with parameters 1,3,7,8 or 9. However, I made successful calls for other functions in this .dll that required WFSVERSION * as parameters, so I don't think that parameters 7 or 8 here.
I would appreciate any thoughts that might arise about this issue, or any constructive criticisms about my code. This is my first experience with P / Invoke, so I'm not sure where to start. Is there a way to narrow down the problem or trial error only for my option?