convert string to char*

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mbh1
    New Member
    • Mar 2009
    • 8

    convert string to char*

    i need to send a string from a text box to a certain
    function that receives char* type
    how can i do the convertion?
    thanks
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    What have you tried so far?
    What did MSDN have to say on this?
    What about a search right here on Bytes?

    Thread 250052

    Comment

    • mbh1
      New Member
      • Mar 2009
      • 8

      #3
      well , im not sure i understood what was the solution in that thread, but
      this is what i did :

      the C function header (in the C dll called genclient.dll):

      int genLogin(char * name);

      and this is the C# code:
      Code:
      [DllImport("genClient.dll", EntryPoint = "genLogin",SetLastError = false, CharSet = CharSet.Auto)]
      public static extern int genLogin(string name);
      
      //and this is the call to the function:
      
      int reply;
      string s;
      reply = genLogin(s);
      but now, c# cant find the enrty point genLogin...
      what did i do wrong?
      thanks
      Last edited by tlhintoq; Mar 12 '09, 12:51 PM. Reason: [CODE] ... [/CODE] tags added

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        It can't find the entry point... Or it doesn't like you trying to call it with an argument of 's' which has not been initialized yet? (s has no value when you try to use it in line 8)

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          Check this link for some examples on P/Invoke: http://msdn.microsoft.com/en-us/magazine/cc164193.aspx. There is also a download link for a "PInvoke Interop Assistant" tool, which helps you create the correct DllImport signature (if you did a mistake).

          You can get detailed info on string marshalling on this link: http://msdn.microsoft.com/en-us/library/s9ts558h.aspx.

          Comment

          • mbh1
            New Member
            • Mar 2009
            • 8

            #6
            now ive tried something else...

            the C function header is this:

            __declspec(dlle xport) int genLogin(char * name);

            and my decleration is this:

            [DllImport("genC lient.dll", EntryPoint = "genLogin", ExactSpelling = false, SetLastError = true)]
            public static extern int genLogin([MarshalAs(Unman agedType.LPArra y)] byte[] name);


            the call to the function is done in this way:

            byte[] str = new byte[100];
            textBox1.Text = Convert.ToStrin g((genLogin(str )));

            but still i get the following error:

            Unable to find an entry point named 'genLogin' in DLL 'genClient.dll' .


            any more suggestions?

            thanks!

            Comment

            • vekipeki
              Recognized Expert New Member
              • Nov 2007
              • 229

              #7
              Use the dumpbin.exe tool to make sure that you have the correct entry point name in C++.

              Comment

              Working...