C
C2J
I have a problem to understand and use invoke in C#.
I have a function in C. I need to use it in my new software (C#).
This is the function :
int StrToParameters( char **pCmde, char *pFormat, ... ) ;
This is how I call it in C or C++:
char[] Cmde = "X=150 Y=200";
char[] Format = "X=%i Y=%i";
int VarX=0;
int VarY=0;
StrToParameters(&Cmde, Format, &VarX, &VarY);
After function VarX=150, VarY=200, Cmde=""
I invoke it in C# :
[DllImport(@"MyDLL.dll", CharSet=CharSet.Auto,
CallingConvention=CallingConvention.Cdecl)]
extern public static int StrToParameters(
[MarshalAs(UnmanagedType.LPStr)] ref StringBuilder ppCmde,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pFormat,
params IntPtr[] args);
And I try to use it:
StringBuilder Cmde = new StringBuilder("X=150 Y=200");
StringBuilder Format = new StringBuilder("X=%i Y=%i");
int VarX = 0;
int VarY = 0;
int Ok=StrToParameters(ref Cmde,Format, VarX, VarY);
I'm unable to give adress of VarX and VarY. I try to use ref but I have a
compiler error.
If I don't use ref all is ok with Cmde and Format but VarX and VarY are zéro.
How to pass a list of pointers to my function ?
How to get the adress of a variable in c# ?
I don't want use unsafe code.
Thanks for your help.
I have a function in C. I need to use it in my new software (C#).
This is the function :
int StrToParameters( char **pCmde, char *pFormat, ... ) ;
This is how I call it in C or C++:
char[] Cmde = "X=150 Y=200";
char[] Format = "X=%i Y=%i";
int VarX=0;
int VarY=0;
StrToParameters(&Cmde, Format, &VarX, &VarY);
After function VarX=150, VarY=200, Cmde=""
I invoke it in C# :
[DllImport(@"MyDLL.dll", CharSet=CharSet.Auto,
CallingConvention=CallingConvention.Cdecl)]
extern public static int StrToParameters(
[MarshalAs(UnmanagedType.LPStr)] ref StringBuilder ppCmde,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pFormat,
params IntPtr[] args);
And I try to use it:
StringBuilder Cmde = new StringBuilder("X=150 Y=200");
StringBuilder Format = new StringBuilder("X=%i Y=%i");
int VarX = 0;
int VarY = 0;
int Ok=StrToParameters(ref Cmde,Format, VarX, VarY);
I'm unable to give adress of VarX and VarY. I try to use ref but I have a
compiler error.
If I don't use ref all is ok with Cmde and Format but VarX and VarY are zéro.
How to pass a list of pointers to my function ?
How to get the adress of a variable in c# ?
I don't want use unsafe code.
Thanks for your help.