Invoke in C#

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.
 
B

Ben Voigt [C++ MVP]

C2J said:
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# ?

You need to declare a different overload of the function for every possible
parameter combination, like this:

extern public static int StrToParameters(
[MarshalAs(UnmanagedType.LPStr)] ref StringBuilder ppCmde,
[MarshalAs(UnmanagedType.LPStr)] StringBuilder pFormat,
ref int first, ref int second);

I'm somewhat doubtful that the first argument is going to work the way you
intend, but I don't know what it is supposed to do either... leave a pointer
to the character after the last match found?

A much better idea would be to use a regular expression instead, .NET
includes a regular expression class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top