c# - Passing a struct by reference from managed to unmanaged code? -
my struct looks 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# 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 calling 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 calling this:
p_request_struct = new _request_struct(); // fill p_request_struct // response p_response_struct = new _response_struct(); // initialize unmanaged memory 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 getting null query! in c code query==null
true
, returns null. populating request struct?
do have allocate memory well?
Comments
Post a Comment