Passing strings to C++ DLL

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

    Passing strings to C++ DLL

    I am attempting to write a .NET wrapper for a C++ DLL file, but am having
    problems with passing strings as parameters. How should I be writing my C#
    function call when the C header file is definined as taking a char * as an
    argument? For example the C++ header says
    SDCERR GetCurrentConfi g(DWORD *num, char *name);
    I am using Uint for the *num parameter, which returns the correct value
    but for *name, I always get back a string of 6 squares. I have tried using
    string, StringBuilder and char arrays, and passing by ref and not, but
    always get the same result.
    Connected to this I am also having problems with passing structures for
    C++ functions - the structures contain strings as well. Whenever I call a
    function that requires a structure as a parameter, I always get a
    NotSupportedExc eption, does this just mean that I have defined my structure
    incorrectly? Do I have to initailise the structure beforehand (have tried
    this but still didn't work)?
    I am using Visual Studio 2005, and if it makes a difference it is for a
    Compact Framework device. Thanks in advance for any help.

    Andy Baker



  • puggid@googlemail.com

    #2
    Re: Passing strings to C++ DLL

    Have you tried passing a StringBuilder?

    Comment

    • Andy Baker

      #3
      Re: Passing strings to C++ DLL

      Yes, I tried StringBuilder. From what I read on the net, that's what I
      thought I should be doing. The code I am using is as follows:-
      uint Number = 0;
      StringBuilder Name = new StringBuilder(C ONFIG_NAME_SZ);
      SDCERR Result = GetCurrentConfi g(ref Number, Name);
      Result contains a valid value, as does Number, but Name just contains
      rubbish. If I pass Name with the ref keyword I get a NotSupportedExc eption.
      To convert the StringBuilder to a string, I am just using ToString - is this
      correct?

      Andy Baker

      <puggid@googlem ail.comwrote in message
      news:5b2bd884-532d-456a-9852-c4bf3bae2387@f6 3g2000hsf.googl egroups.com...
      Have you tried passing a StringBuilder?

      Comment

      • Pavel Minaev

        #4
        Re: Passing strings to C++ DLL

        On Jul 2, 3:17 pm, "Andy Baker" <aba...@NOSPAMv anputer.comwrot e:
        I am attempting to write a .NET wrapper for a C++ DLL file, but am having
        problems with passing strings as parameters. How should I be writing my C#
        function call when the C header file is definined as taking a char * as an
        argument? For example the C++ header says
            SDCERR GetCurrentConfi g(DWORD *num, char *name);
            I am using Uint for the *num parameter, which returns the correct value
        but for *name, I always get back a string of 6 squares. I have tried using
        string, StringBuilder and char arrays, and passing by ref and not, but
        always get the same result.
            Connected to this I am also having problems with passing structures for
        C++ functions - the structures contain strings as well. Whenever I call a
        function that requires a structure as a parameter, I always get a
        NotSupportedExc eption, does this just mean that I have defined my structure
        incorrectly? Do I have to initailise the structure beforehand (have tried
        this but still didn't work)?
            I am using Visual Studio 2005, and if it makes a difference it is for a
        Compact Framework device. Thanks in advance for any help.
        Can you please show your P/Invoke declaration for this function?
        Perhaps you've forgotten CharSet?
        Try this also:

        [DllImport(CharS et = CharSet.Ansi)]
        SDCERR GetCurrentConfi g(ref uint num, StringBuilder name);

        Comment

        • =?Utf-8?B?S0g=?=

          #5
          RE: Passing strings to C++ DLL

          Andy, a couple options:

          1) Look at the MarshalAsAttrib ute which tells .NET how to translate its
          unicode string between it and unmanaged code; the attribute is applied to the
          argument, here's the example from MSDN:

          void MyMethod( [MarshalAs(LPStr )] string s);

          2) The other option is to write the wrapper in managed C++; if you go this
          route your method calls will look something like this:

          void MyMethod(String ^ s)
          {
          IntPtr ptr = Marshal::String ToHGlobalAnsi(s tr);;

          try
          {
          SDCERR err = GetCurrentConfi g( (char*)ptr.ToPo inter() );
          }
          finally
          {
          if (ptr.ToPointer( )) Marshal::FreeHG lobal(ptr);
          }
          }

          HTH - KH



          "Andy Baker" wrote:
          I am attempting to write a .NET wrapper for a C++ DLL file, but am having
          problems with passing strings as parameters. How should I be writing my C#
          function call when the C header file is definined as taking a char * as an
          argument? For example the C++ header says
          SDCERR GetCurrentConfi g(DWORD *num, char *name);
          I am using Uint for the *num parameter, which returns the correct value
          but for *name, I always get back a string of 6 squares. I have tried using
          string, StringBuilder and char arrays, and passing by ref and not, but
          always get the same result.
          Connected to this I am also having problems with passing structures for
          C++ functions - the structures contain strings as well. Whenever I call a
          function that requires a structure as a parameter, I always get a
          NotSupportedExc eption, does this just mean that I have defined my structure
          incorrectly? Do I have to initailise the structure beforehand (have tried
          this but still didn't work)?
          I am using Visual Studio 2005, and if it makes a difference it is for a
          Compact Framework device. Thanks in advance for any help.
          >
          Andy Baker
          >
          >
          >
          >

          Comment

          • Andy Baker

            #6
            Re: Passing strings to C++ DLL

            My P/Invoke declaration is as follows:
            [DllImport("VPSu mmit.dll", CharSet = CharSet.Unicode "]
            public static extern SDCERR GetCurrentConfi g(ref unit num, StringBuilder
            name);
            I have also tried CharSet.Auto - CharSet.Ansi is not a option for the
            Compact Framework (I think)

            "Pavel Minaev" <int19h@gmail.c omwrote in message
            news:1f2323cd-7b69-47a4-96ac-1f64b32c8003@w7 g2000hsa.google groups.com...
            On Jul 2, 3:17 pm, "Andy Baker" <aba...@NOSPAMv anputer.comwrot e:
            I am attempting to write a .NET wrapper for a C++ DLL file, but am having
            problems with passing strings as parameters. How should I be writing my C#
            function call when the C header file is definined as taking a char * as an
            argument? For example the C++ header says
            SDCERR GetCurrentConfi g(DWORD *num, char *name);
            I am using Uint for the *num parameter, which returns the correct value
            but for *name, I always get back a string of 6 squares. I have tried using
            string, StringBuilder and char arrays, and passing by ref and not, but
            always get the same result.
            Connected to this I am also having problems with passing structures for
            C++ functions - the structures contain strings as well. Whenever I call a
            function that requires a structure as a parameter, I always get a
            NotSupportedExc eption, does this just mean that I have defined my
            structure
            incorrectly? Do I have to initailise the structure beforehand (have tried
            this but still didn't work)?
            I am using Visual Studio 2005, and if it makes a difference it is for a
            Compact Framework device. Thanks in advance for any help.
            Can you please show your P/Invoke declaration for this function?
            Perhaps you've forgotten CharSet?
            Try this also:

            [DllImport(CharS et = CharSet.Ansi)]
            SDCERR GetCurrentConfi g(ref uint num, StringBuilder name);


            Comment

            • Andy Baker

              #7
              Re: Passing strings to C++ DLL

              Thanks for the advice. I have been looking at the MarshalAs attribute in
              relation to passing structures, but hadn't tried it in the DllImport
              declaration. However, I am obviously doing something wrong as every time I
              try it I am getting a NotSupportedExc eption.
              As I understand even less about C++ that I do about C# (which isn't
              alot!) I don't think the managed C++ route is the one to go down. I think I
              need someone more experienced to do this job for me! Thanks again.

              Andy Baker

              "KH" <KH@discussions .microsoft.comw rote in message
              news:9C5B61AE-FAD6-496B-94B0-8D104F6C24D0@mi crosoft.com...
              Andy, a couple options:
              >
              1) Look at the MarshalAsAttrib ute which tells .NET how to translate its
              unicode string between it and unmanaged code; the attribute is applied to
              the
              argument, here's the example from MSDN:
              >
              void MyMethod( [MarshalAs(LPStr )] string s);
              >
              2) The other option is to write the wrapper in managed C++; if you go this
              route your method calls will look something like this:
              >
              void MyMethod(String ^ s)
              {
              IntPtr ptr = Marshal::String ToHGlobalAnsi(s tr);;
              >
              try
              {
              SDCERR err = GetCurrentConfi g( (char*)ptr.ToPo inter() );
              }
              finally
              {
              if (ptr.ToPointer( )) Marshal::FreeHG lobal(ptr);
              }
              }
              >
              HTH - KH
              >
              >
              >
              "Andy Baker" wrote:
              >
              >I am attempting to write a .NET wrapper for a C++ DLL file, but am having
              >problems with passing strings as parameters. How should I be writing my
              >C#
              >function call when the C header file is definined as taking a char * as
              >an
              >argument? For example the C++ header says
              > SDCERR GetCurrentConfi g(DWORD *num, char *name);
              > I am using Uint for the *num parameter, which returns the correct
              >value
              >but for *name, I always get back a string of 6 squares. I have tried
              >using
              >string, StringBuilder and char arrays, and passing by ref and not, but
              >always get the same result.
              > Connected to this I am also having problems with passing structures
              >for
              >C++ functions - the structures contain strings as well. Whenever I call a
              >function that requires a structure as a parameter, I always get a
              >NotSupportedEx ception, does this just mean that I have defined my
              >structure
              >incorrectly? Do I have to initailise the structure beforehand (have tried
              >this but still didn't work)?
              > I am using Visual Studio 2005, and if it makes a difference it is for
              >a
              >Compact Framework device. Thanks in advance for any help.
              >>
              >Andy Baker
              >>
              >>
              >>
              >>

              Comment

              Working...