Asked by:
PInvokeStackImbalance calling unmanaged function
General discussion

Hi all!This is the unmanaged function
MODEL lad (const int *list, DATASET *dset);
This is the MODEL structure returned by the functiontypedef struct { int ID; /* ID number for model */ int refcount; /* for saving/deleting */ int ci; /* "command index"  estimation method */ gretlopt opt; /* record of options */ int t1, t2, nobs; /* starting observation, ending observation, and number of obs */ char *submask; /* keep track of subsample in force when model was estimated */ char *missmask; /* missing observations mask */ SAMPLE smpl; /* numeric start and end of current sample when model was estimated */ int full_n; /* full length of dataset on estimation */ int ncoeff, dfn, dfd; /* number of coefficents; degrees of freedom in numerator and denominator */ int *list; /* list of variables by ID number */ int ifc; /* = 1 if the equation includes a constant, else = 0 */ int nwt; /* ID number of the weight variable (WLS) */ int aux; /* code representing the sort of auxiliary regression this is (or not) */ double *coeff; /* array of coefficient estimates */ double *sderr; /* array of estimated std. errors */ double *uhat; /* regression residuals */ double *yhat; /* fitted values from regression */ double *xpx; /* X'X matrix, in packed form */ double *vcv; /* VCV matrix for coefficient estimates */ double ess, tss; /* Error and Total Sums of Squares */ double sigma; /* Standard error of regression */ double rsq, adjrsq; /* Unadjusted and adjusted R^2 */ double fstt; /* overall Fstatistic */ double chisq; /* overall chisquare statistic */ double lnL; /* loglikelihood */ double ybar, sdy; /* mean and std. dev. of dependent var. */ double criterion[3]; /* array of model selection statistics */ double dw, rho; /* DurbinWatson stat. and estimated 1st order autocorrelation coefficient */ ARINFO *arinfo; /* pointer to struct to hold special info for autoregressive model */ int errcode; /* Error code in case of failure */ char *name; /* for use in GUI */ char *depvar; /* name of dependent var in special cases */ int nparams; /* number of named model parameters */ char **params; /* for named model parameters */ int ntests; /* number of attached test results */ ModelTest *tests; /* attached hypothesis test results */ DATASET *dataset; /* for handling models estimated on a subsampled portion of the dataset */ int n_data_items; /* number of extra data items */ model_data_item **data_items; /* pointer to additional data */ } MODEL;
This is the C# method declaration[DllImport(@"C:\Program Files (x86)\gretl\libgretl.dll", CallingConvention = CallingConvention.Cdecl)] internal static extern MODEL lad(IntPtr list, IntPtr dset);
This is the C# structure declaration[StructLayout(LayoutKind.Sequential)] struct SAMPLE { int t1; int t2; }
[StructLayout(LayoutKind.Sequential)] struct MODEL { int ID; /* ID number for model */ int refcount; /* for saving/deleting */ int ci; /* "command index"  estimation method */ int opt; /* record of options */ int t1, t2, nobs; /* starting observation, ending observation, and number of obs */ IntPtr submask; /* keep track of subsample in force when model was estimated */ IntPtr missmask; /* missing observations mask */ SAMPLE smpl; /* numeric start and end of current sample when model was estimated */ int full_n; /* full length of dataset on estimation */ int ncoeff, dfn, dfd; /* number of coefficents; degrees of freedom in numerator and denominator */ IntPtr list; /* list of variables by ID number */ int ifc; /* = 1 if the equation includes a constant, else = 0 */ int nwt; /* ID number of the weight variable (WLS) */ int aux; /* code representing the sort of auxiliary regression this is (or not) */ IntPtr coeff; /* array of coefficient estimates */ IntPtr sderr; /* array of estimated std. errors */ IntPtr uhat; /* regression residuals */ IntPtr yhat; /* fitted values from regression */ IntPtr xpx; /* X'X matrix, in packed form */ IntPtr vcv; /* VCV matrix for coefficient estimates */ double ess, tss; /* Error and Total Sums of Squares */ double sigma; /* Standard error of regression */ double rsq, adjrsq; /* Unadjusted and adjusted R^2 */ double fstt; /* overall Fstatistic */ double chisq; /* overall chisquare statistic */ double lnL; /* loglikelihood */ double ybar, sdy; /* mean and std. dev. of dependent var. */ //[MarshalAs (UnmanagedType.ByValArray, SizeConst=3)] double criterion_1, criterion_2, criterion_3; /* array of model selection statistics */ double dw, rho; /* DurbinWatson stat. and estimated 1st order autocorrelation coefficient */ IntPtr arinfo; /* pointer to struct to hold special info for autoregressive model */ int errcode; /* Error code in case of failure */ IntPtr name; /* for use in GUI */ IntPtr depvar; /* name of dependent var in special cases */ int nparams; /* number of named model parameters */ IntPtr pparams; /* for named model parameters */ int ntests; /* number of attached test results */ IntPtr tests; /* attached hypothesis test results */ IntPtr dataset; /* for handling models estimated on a subsampled portion of the dataset */ int n_data_items; /* number of extra data items */ IntPtr data_items; /* pointer to additional data */ }
calling C# lad function produces PInvokeStackImbalance.I checked so many times my structure .... unsuccessfully: I know, I'm wrong but it seems ok :)Thanks for any help!Filippo Edited by Filippo Bonsignori Tuesday, January 10, 2012 10:45 AM
All replies

Hey Flippo
I'm currently also doing Gretl .Net "interface". And have the same problems. After several days of research found out you cannot. You have to use C++/CLI wrapper. But I cannot give you more specific solution couse I am still working on it...
see next links:
http://social.msdn.microsoft.com/Forums/enUS/clr/thread/6a66cdefb1f44bec9a9f4686eef3474a
http://social.msdn.microsoft.com/Forums/enUS/clr/thread/d730658ce74d4cb9a1937f5f8d32da81
And finaly:
http://social.msdn.microsoft.com/Forums/enUS/vcgeneral/thread/93fd5a1a00954c4893fab0de343a532fYou can mail me, if you need anything. Else if you found some solution using PInvoke I would REALY appreciated if you share.
Have a nice day
Saymon Edited by Saymon Rupar Tuesday, January 10, 2012 1:23 PM Links correction
