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.

Signature
C2J electronique
Peter Duniho - 25 Mar 2008 17:28 GMT
> I have a problem to understand and use invoke in C#.
Unfortunately, you don't. You are looking for help with "p/invoke", not
"invoke".
I know it seems like a subtle difference, but .NET uses both terms in very
specific, and relatively different ways. Someone would have to read
practically all the way to the end of your relatively lengthy post to
learn that you're actually talking about the former, even though the
subject and early parts of your message claim to be talking about the
latter.
I mention this because it's _possible_ that the few people who can help
you with "p/invoke" will overlook your post because it says "invoke"
instead and not realize you need their help. Hopefully that won't happen,
but in the event you don't get a useful answer in the next 24 hours, it
may be because of that. If so, as much as I hate suggesting it, you may
want to consider reposting your question with a subject that more accurate
describes your question.
You may also want to consider posting your question in the newsgroup
specifically intended for questions like this:
microsoft.public.dotnet.framework.interop
Sorry I don't actually have an answer to your question. But I hope that I
can at least help you get an answer to your question. :)
Pete
Ben Voigt [C++ MVP] - 25 Mar 2008 23:12 GMT
> I have a problem to understand and use invoke in C#.
>
[quoted text clipped - 45 lines]
> 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.
> I don't want use unsafe code.
>
> Thanks for your help.