C# string to C++ dll char*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johnsto

    #1

    C# string to C++ dll char*

    I'm really stuck - can someone help me!

    I've got a basic setup consisting of two things:
    1. A C# web service
    2. An unmanaged C++ DLL

    The WS is intended to call some functions in the DLL. The DLL has some
    legacy code which requires char* in its method parameters.

    I also have a new file in the DLL which is basically an intermediary -
    the WS can call the exported functions from this file, which then call
    the legacy code. The problem is converting from the native C# strings in
    the WS to the char*'s in the C++ code.

    So, the WS calls method(string) in the DLL, and the DLL then needs to
    call method(char*) in its C++ code, where string==char*.

    At the moment I have the DLL exporting method(String*) 's, but of course
    in C# I can't get the address of a string to pass to the method. If I
    change the DLL methods to method(String) (ie. no pointer), the compiler
    complains that they HAVE to be pointers. I can't use method(char*)
    either because then I'm back to the original problem of C# not wanting
    to call a method with a char* in it.

    Help!



  • Shakir Hussain

    #2
    Re: C# string to C++ dll char*

    For char*, you have to do this

    byte [] str = new byte [255];

    Pass str to the function

    --
    Shak
    (Houston)


    "johnsto" <anyone@anywher e.com> wrote in message
    news:cddlmm$fac $1@titan.btinte rnet.com...[color=blue]
    > I'm really stuck - can someone help me!
    >
    > I've got a basic setup consisting of two things:
    > 1. A C# web service
    > 2. An unmanaged C++ DLL
    >
    > The WS is intended to call some functions in the DLL. The DLL has some
    > legacy code which requires char* in its method parameters.
    >
    > I also have a new file in the DLL which is basically an intermediary -
    > the WS can call the exported functions from this file, which then call
    > the legacy code. The problem is converting from the native C# strings in
    > the WS to the char*'s in the C++ code.
    >
    > So, the WS calls method(string) in the DLL, and the DLL then needs to
    > call method(char*) in its C++ code, where string==char*.
    >
    > At the moment I have the DLL exporting method(String*) 's, but of course
    > in C# I can't get the address of a string to pass to the method. If I
    > change the DLL methods to method(String) (ie. no pointer), the compiler
    > complains that they HAVE to be pointers. I can't use method(char*)
    > either because then I'm back to the original problem of C# not wanting
    > to call a method with a char* in it.
    >
    > Help!
    >
    >
    >[/color]


    Comment

    • Chris R. Timmons

      #3
      Re: C# string to C++ dll char*

      johnsto <anyone@anywher e.com> wrote in
      news:cddlmm$fac $1@titan.btinte rnet.com:
      [color=blue]
      > I'm really stuck - can someone help me!
      >
      > I've got a basic setup consisting of two things:
      > 1. A C# web service
      > 2. An unmanaged C++ DLL
      >
      > The WS is intended to call some functions in the DLL. The DLL
      > has some legacy code which requires char* in its method
      > parameters.
      >
      > I also have a new file in the DLL which is basically an
      > intermediary - the WS can call the exported functions from this
      > file, which then call the legacy code. The problem is converting
      > from the native C# strings in the WS to the char*'s in the C++
      > code.
      >
      > So, the WS calls method(string) in the DLL, and the DLL then
      > needs to call method(char*) in its C++ code, where
      > string==char*.
      >
      > At the moment I have the DLL exporting method(String*) 's, but of
      > course in C# I can't get the address of a string to pass to the
      > method. If I change the DLL methods to method(String) (ie. no
      > pointer), the compiler complains that they HAVE to be pointers.
      > I can't use method(char*) either because then I'm back to the
      > original problem of C# not wanting to call a method with a char*
      > in it.[/color]

      You can use the either the System.String or System.Text.Str ingBuilder
      class. The underlying P/Invoke marshalling code in the .Net
      framework will do all of the messy character pointer conversions for
      you. If the string parameter is used as an output or input/output
      buffer, then use StringBuilder. Otherwise if the string parameter is
      input-only you can use the String class.

      For example, the Win32 API function GetPrivateProfi leString has both
      input-only string parameters, and a string output parameter. Here's
      how it can be accessed in C# using the String and StringBuilder
      classes as parameter types:

      /* Original C Method Signature:

      DWORD GetPrivateProfi leString(
      LPCTSTR lpAppName,
      LPCTSTR lpKeyName,
      LPCTSTR lpDefault,
      LPTSTR lpReturnedStrin g,
      DWORD nSize,
      LPCTSTR lpFileName
      );

      */

      [DllImport("kern el32", EntryPoint="Get PrivateProfileS tring",
      SetLastError = false, CharSet = CharSet.Auto)]
      private static extern long GetPrivateProfi leString(
      string lpApplicationNa me, // in
      string lpKeyName, // in
      string lpDefault, // in
      StringBuilder lpReturnedStrin g, // out
      int nSize, // in
      string lpFileName); // in


      public string GetIniValue(
      string iniFileName,
      string sectionName,
      string keyName,
      string defaultValue)
      {
      const int maxlen = 255;

      StringBuilder sBuffer = new StringBuilder(m axlen);

      // The P/Invoke marshaller takes care of converting the
      // String and StringBuilder parameters to/from
      // character pointers.

      GetPrivateProfi leString(sectio nName, keyName, defaultValue,
      sBuffer, maxlen, iniFileName);

      return sBuffer.ToStrin g();
      }

      --
      Hope this helps.

      Chris.
      -------------
      C.R. Timmons Consulting, Inc.

      Comment

      • johnsto

        #4
        Re: C# string to C++ dll char*

        Chris R. Timmons wrote:[color=blue]
        > You can use the either the System.String or System.Text.Str ingBuilder
        > class. The underlying P/Invoke marshalling code in the .Net
        > framework will do all of the messy character pointer conversions for
        > you. If the string parameter is used as an output or input/output
        > buffer, then use StringBuilder. Otherwise if the string parameter is
        > input-only you can use the String class.[/color]

        It is input-only... so you're saying if I pass it a System::String, it
        will convert to a char* automatically?

        I mean, the very basic version of the problem is converting the
        System::String to char*, and working out where to do the conversion. The
        C++ requires char*, the C# WS needs String (it makes life complicated
        and complains about char*), and the DLL intermediary is there really
        just to forward method calls and handle any parameter conversion.

        Part of the problem is actually convincing the C# bit to let me use
        String* as a pointer - which I can then pass to the DLL... That's the
        last hurdle that got me before I gave up for the weekend...

        Argh!!!

        Comment

        • BMermuys

          #5
          Re: C# string to C++ dll char*

          Hi,

          "johnsto" <anyone@anywher e.com> wrote in message
          news:cddlmm$fac $1@titan.btinte rnet.com...[color=blue]
          > I'm really stuck - can someone help me!
          >
          > I've got a basic setup consisting of two things:
          > 1. A C# web service
          > 2. An unmanaged C++ DLL
          >
          > The WS is intended to call some functions in the DLL. The DLL has some
          > legacy code which requires char* in its method parameters.
          >
          > I also have a new file in the DLL which is basically an intermediary -
          > the WS can call the exported functions from this file, which then call
          > the legacy code. The problem is converting from the native C# strings in
          > the WS to the char*'s in the C++ code.
          >
          > So, the WS calls method(string) in the DLL, and the DLL then needs to
          > call method(char*) in its C++ code, where string==char*.[/color]

          The dll (intermediary) that WS suppose to call, is it a managed c dll or a
          plain old c dll ? Because that's still not very clear here.


          HTH,
          greetings

          [color=blue]
          >
          > At the moment I have the DLL exporting method(String*) 's, but of course
          > in C# I can't get the address of a string to pass to the method. If I
          > change the DLL methods to method(String) (ie. no pointer), the compiler
          > complains that they HAVE to be pointers. I can't use method(char*)
          > either because then I'm back to the original problem of C# not wanting
          > to call a method with a char* in it.
          >
          > Help!
          >
          >
          >[/color]


          Comment

          • Chris R. Timmons

            #6
            Re: C# string to C++ dll char*

            johnsto <anyone@anywher e.com> wrote in
            news:cdeotj$1rq $1@titan.btinte rnet.com:
            [color=blue]
            > Chris R. Timmons wrote:[color=green]
            >> You can use the either the System.String or
            >> System.Text.Str ingBuilder class. The underlying P/Invoke
            >> marshalling code in the .Net framework will do all of the messy
            >> character pointer conversions for you. If the string parameter
            >> is used as an output or input/output buffer, then use
            >> StringBuilder. Otherwise if the string parameter is input-only
            >> you can use the String class.[/color]
            >
            > It is input-only... so you're saying if I pass it a
            > System::String, it will convert to a char* automatically?[/color]

            Yes.
            [color=blue]
            > I mean, the very basic version of the problem is converting the
            > System::String to char*, and working out where to do the
            > conversion. The C++ requires char*, the C# WS needs String (it
            > makes life complicated and complains about char*), and the DLL
            > intermediary is there really just to forward method calls and
            > handle any parameter conversion.
            >
            > Part of the problem is actually convincing the C# bit to let me
            > use String* as a pointer - which I can then pass to the DLL...
            > That's the last hurdle that got me before I gave up for the
            > weekend...[/color]

            Are you trying to use a "string *" in the C# code? Just try a plain
            old System.String. If that doesn't work, try
            System.Text.Str ingBuilder. I don't think there's any need to use
            pointers (*) in the C# code.

            To see why, look at the example code I posted. The C "LPSTR"
            parameter type means "Long Pointer to STRing", which is a typedef for
            "char *". And LPCSTR means "Long Pointer to a Const STRing", which
            is a typedef for "const char *" (see WTypes.h for the
            actual typedefs). The P/Invoker marshaller correctly translates from
            System.String to LPCSTR, and from System.Text.Str ingBuilder to LPSTR.

            --
            Hope this helps.

            Chris.
            -------------
            C.R. Timmons Consulting, Inc.

            Comment

            • johnsto

              #7
              Re: C# string to C++ dll char*

              BMermuys wrote:[color=blue]
              > Hi,
              > "johnsto" <anyone@anywher e.com> wrote in message[color=green]
              >>I've got a basic setup consisting of two things:
              >>1. A C# web service
              >>2. An unmanaged C++ DLL[/color]
              > The dll (intermediary) that WS suppose to call, is it a managed c dll or a
              > plain old c dll ? Because that's still not very clear here.[/color]

              It's unmanaged - I think it was throwing up errors as a managed DLL (due
              to the legacy stuff). I guess that makes it 'plain old c' although I'm
              building it with VS2003.

              Comment

              • BMermuys

                #8
                Re: C# string to C++ dll char*


                "johnsto" <anyone@anywher e.com> wrote in message
                news:cdfsgq$lre $1@sparta.btint ernet.com...[color=blue]
                > BMermuys wrote:[color=green]
                > > Hi,
                > > "johnsto" <anyone@anywher e.com> wrote in message[color=darkred]
                > >>I've got a basic setup consisting of two things:
                > >>1. A C# web service
                > >>2. An unmanaged C++ DLL[/color]
                > > The dll (intermediary) that WS suppose to call, is it a managed c dll or[/color][/color]
                a[color=blue][color=green]
                > > plain old c dll ? Because that's still not very clear here.[/color]
                >
                > It's unmanaged - I think it was throwing up errors as a managed DLL (due
                > to the legacy stuff). I guess that makes it 'plain old c' although I'm
                > building it with VS2003.[/color]

                ok, no problem, then you have to follow Chris' replies.

                Greetings


                Comment

                • johnsto

                  #9
                  Re: C# string to C++ dll char*

                  Chris R. Timmons wrote:[color=blue]
                  > To see why, look at the example code I posted. The C "LPSTR"
                  > parameter type means "Long Pointer to STRing", which is a typedef for
                  > "char *". And LPCSTR means "Long Pointer to a Const STRing", which
                  > is a typedef for "const char *" (see WTypes.h for the
                  > actual typedefs). The P/Invoker marshaller correctly translates from
                  > System.String to LPCSTR, and from System.Text.Str ingBuilder to LPSTR.[/color]

                  Damn, .NET is so smart :)

                  Comment

                  Working...