0

my struct looks like this in c code:

typedef struct _REQUEST {
    char D [_D_MAX+1];
    char M [_M_MAX+1];
    char T [_T_MAX+1];
    char ClRef [_CL_REF_MAX+1];
    char load [_LOAD_MAX];
    ULONG loadLen;
} _REQUEST, *LP_REQUEST;

in c# like this:

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct _REQUEST_STRUCT {
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string D;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string M;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string T;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    public string clRef;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1200)]
    public string Load;

    public UInt32 LoadLen;
}

the method I am calling is like this in c:

int 
AJ_STDCALL 
SimpTran (const char* svc_name, 
                   const LP_REQUEST query, 
                   LP_RESPONSE      response)
{
    //init stuff
    //Validate input parameters
    if ( query == NULL )
        return(ERR_QUERY_NULL);

    if (query->loadLen == 0)
        return (ERR_QUERY_NULL);
}

in c#:

[DllImport(LINUXLIB, CallingConvention = CallingConvention.StdCall)]
public static extern int SimpTran(string serID, ref _REQUEST_STRUCT request, out IntPtr response);

and i am calling it like this:

p_request_struct = new _REQUEST_STRUCT();
// fill p_request_struct

// response
p_response_struct = new _RESPONSE_STRUCT();

// initialize unmanaged memory to hold return struct
IntPtr ptrRet = Marshal.AllocHGlobal(Marshal.SizeOf(p_response_struct));
Marshal.StructureToPtr(p_response_struct, ptrRet, false);

iRet = SimpTran(SerID, ref p_request_struct, out ptrRet);

I am getting null query! In C code query==null is true and it returns null. But I am populating the request struct?

Do I have to allocate memory for that as well?

infinitloop
  • 2,863
  • 7
  • 38
  • 55
  • Try making the C# `_REQUEST_STRUCT` a struct instead of a class and removing the `ref` from the DllImport and from the call to it. – Matthew Watson May 13 '13 at 19:50
  • Find the guy whose initials are "AJ" and ask him what AJ_STDCALL means on Linux. – Hans Passant May 13 '13 at 19:52
  • 1
    Does it return `ERR_QUERY_NULL` because `query == NULL` or because `query->PayloadLength == 0`? What is `query->PayloadLength` -- it's not declared in the C struct? Are you sure that `_D_MAX+1` is `32` and not `32+1`, etc.? The method signature in C# should be `public static extern int SimpTran(string serID, ref _REQUEST_STRUCT request, out _RESPONSE_STRUCT response);` -- no need for `IntPtr` here. – dtb May 13 '13 at 19:58
  • Did you read my questions that are similar? http://stackoverflow.com/questions/2372061/c-sharp-struct-no-parameterless-constructor-see-what-i-need-to-accomplish and http://stackoverflow.com/questions/2344929/pointers-in-c-sharp-to-retrieve-reference-from-dllimport-function –  May 13 '13 at 20:00
  • http://stackoverflow.com/questions/2343272/dllimport-unmanaged-non-net-dll-to-net-project-representing-char-and-void –  May 13 '13 at 20:01
  • @dtb, i fixed my question. It is indeed loadLen. – infinitloop May 13 '13 at 20:05

0 Answers0