Passing and retrieving a string calling a c++ function from c#

Discussion in 'Microsoft C# .NET' started by roben, Mar 29, 2004.

  1. roben

    roben Guest

    I want to call a function in C++ (external dll) from C#.
    However the string is still empty after returning from the function.

    -------------------
    C#
    -------------------
    [DllImport("ccw.dll", CharSet=CharSet.Auto)] public static extern int
    HelloWorld(StringBuilder abc);

    StringBuilder mytext= new StringBuilder(1000);
    AuthResult = HelloWorld(mytext);

    //Here the string is still empty

    -------------------
    C++
    -------------------
    int HelloWorld(char *mytext)
    {
    mytext = "Hi..Test";
    return 0;
    }


    Hope you can help!
    Any suggestions?
     
    roben, Mar 29, 2004
    #1
    1. Advertisements

  2. roben,

    The reason it is still empty is because the pointer that you are passing
    in is not able to be modified. You are trying to reassign the pointer to
    the character array, which can't be done. If you want to perform a
    reassignment of the pointer, then you have to pass a pointer to a pointer.

    What you should be doing is calling strcpy on the pointer being passed
    in, and passing the other string you want to copy it to. Then, it should
    work.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    -

    "roben" <> wrote in message
    news:...
    > I want to call a function in C++ (external dll) from C#.
    > However the string is still empty after returning from the function.
    >
    > -------------------
    > C#
    > -------------------
    > [DllImport("ccw.dll", CharSet=CharSet.Auto)] public static extern int
    > HelloWorld(StringBuilder abc);
    >
    > StringBuilder mytext= new StringBuilder(1000);
    > AuthResult = HelloWorld(mytext);
    >
    > //Here the string is still empty
    >
    > -------------------
    > C++
    > -------------------
    > int HelloWorld(char *mytext)
    > {
    > mytext = "Hi..Test";
    > return 0;
    > }
    >
    >
    > Hope you can help!
    > Any suggestions?
     
    Nicholas Paldino [.NET/C# MVP], Mar 29, 2004
    #2
    1. Advertisements

  3. roben

    roben Guest

    hi,

    perfect... You're right...Thanks!

    I just needed to change it to strcpy(mytext, "Hello...");


    "Nicholas Paldino [.NET/C# MVP]" <> skrev i
    en meddelelse news:...
    > roben,
    >
    > The reason it is still empty is because the pointer that you are

    passing
    > in is not able to be modified. You are trying to reassign the pointer to
    > the character array, which can't be done. If you want to perform a
    > reassignment of the pointer, then you have to pass a pointer to a pointer.
    >
    > What you should be doing is calling strcpy on the pointer being passed
    > in, and passing the other string you want to copy it to. Then, it should
    > work.
    >
    > Hope this helps.
    >
    >
    > --
    > - Nicholas Paldino [.NET/C# MVP]
    > -
    >
    > "roben" <> wrote in message
    > news:...
    > > I want to call a function in C++ (external dll) from C#.
    > > However the string is still empty after returning from the function.
    > >
    > > -------------------
    > > C#
    > > -------------------
    > > [DllImport("ccw.dll", CharSet=CharSet.Auto)] public static extern int
    > > HelloWorld(StringBuilder abc);
    > >
    > > StringBuilder mytext= new StringBuilder(1000);
    > > AuthResult = HelloWorld(mytext);
    > >
    > > //Here the string is still empty
    > >
    > > -------------------
    > > C++
    > > -------------------
    > > int HelloWorld(char *mytext)
    > > {
    > > mytext = "Hi..Test";
    > > return 0;
    > > }
    > >
    > >
    > > Hope you can help!
    > > Any suggestions?

    >
    >
     
    roben, Mar 29, 2004
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. Tony Liu
    Replies:
    2
    Views:
    719
    Peter Huang [MSFT]
    Jul 10, 2003
  2. Hessam
    Replies:
    0
    Views:
    1,259
    Hessam
    Aug 8, 2003
  3. Jeremy Chapman

    Passing a function and parameter list to another function

    Jeremy Chapman, Mar 8, 2004, in forum: Microsoft C# .NET
    Replies:
    3
    Views:
    616
    Jon Skeet [C# MVP]
    Mar 9, 2004
  4. Guest
    Replies:
    3
    Views:
    320
    Stephen Ahn
    Jul 22, 2004
  5. Guest
    Replies:
    1
    Views:
    324
    Bjorn Abelli
    Nov 9, 2005
  6. Hema
    Replies:
    1
    Views:
    249
    Ignacio Machin \( .NET/ C# MVP \)
    Apr 18, 2006
  7. greatbarrier86

    String.Contains then a String.Replace or just String.Replace

    greatbarrier86, Mar 13, 2009, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    928
    Tom Overton
    Mar 13, 2009
  8. shapper

    Parse String to Dictionary<String, String>

    shapper, Oct 10, 2010, in forum: Microsoft C# .NET
    Replies:
    10
    Views:
    1,647
    shapper
    Oct 14, 2010
Loading...