Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Invoke in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
C2J - 25 Mar 2008 10:15 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.