CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2007
    Posts
    5

    Pass string to c# program from C dll

    I have read a lot of threads about passing strings from dlls to c# programs but a many of them are examples in C++ witch use a lot of C++ features.

    I want to make a dll in C (only) and it should pass strings too C# program and vice versa.

    I would appriciate any help and code examples.

    Thank you.

  2. #2
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Pass string to c# program from C dll

    How are you calling a C# DLL ?
    Regards,
    Ramkrishna Pawar

  3. #3
    Join Date
    Jun 2007
    Posts
    5

    Re: Pass string to c# program from C dll

    With dllimport.

  4. #4
    Join Date
    Jun 2007
    Posts
    5

    Re: Pass string to c# program from C dll

    I am calling the C dll from C# program. (NOT the C# dll from C program.)

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Pass string to c# program from C dll

    I did it like this
    Code:
    [DllImport("My.dll", CharSet = CharSet.Unicode)]
    public static extern void Read([MarshalAs(UnmanagedType.LPWStr)] out String str);
    In the dll I converted the char array to unicode before returning but maybe this isn't neccessary with changed CharSet and LPWStr. This worked fine however so I didn't bother to test another way.

  6. #6
    Join Date
    Jun 2007
    Posts
    5

    Re: Pass string to c# program from C dll

    I am trying this code but it doesn't work. What i am trying to do is basicaly trying to receive a string from the dll and desplay it in a textbox:

    C part:
    Code:
       __declspec(dllexport) void  GetStringFromDLL()
    {
    char string[5];
    strcpy(string,"un");
    
    return string;
    }
    C# part:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace dll_import
    {
        public partial class Form1 : Form
       {
    
            [DllImport("dll.dll", CharSet = CharSet.Unicode)]
            public static extern void GetStringFromDLL([MarshalAs(UnmanagedType.LPWStr)] out String str);
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
                
    
            }
    
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
                
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string m;
               GetStringFromDLL(out m);
               textBox3.Text = m;
    
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
        }
    
    }

  7. #7
    Join Date
    Mar 2006
    Location
    Craiova, Romania
    Posts
    439

    Re: Pass string to c# program from C dll

    Quote Originally Posted by Cyril B.
    I am trying this code but it doesn't work. What i am trying to do is basicaly trying to receive a string from the dll and desplay it in a textbox:

    C part:
    Code:
     __declspec(dllexport) void GetStringFromDLL()
    {
    char string[5];
    strcpy(string,"un");
     
    return string;
    }
    Does the C code compile ? I see that you return something but the method signature says something else ...and try with StringBuilder.
    Bogdan

    If someone helped you then please Rate his post and mark the thread as Resolved
    Please improve your messages appearance by using tags [ code] Place your code here [ /code]

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Pass string to c# program from C dll

    If you use my example the dll shall be as:
    Code:
    __declspec(dllexport) void Read(_TCHAR* sz)
    {
      static _TCHAR string[100];
      _tcscpy(sz,_T("un"));
    }
    I think this works if dll is built as unicode if not string has to be converted to unicode before copying to buf.

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Pass string to c# program from C dll

    I hate to say this but I believe using the

    Code:
    __declspec(dllexport)
    will cause a DECORATED version of the method's name to be exported. Therefore DllImport won't be able to find it. I.e. it's got a lot of gunk surrounding the actual name.

    Personally I don't do it this way : I use a .def file with __stdcall on all the exported methods instead to ensure that their names aren't decorated.

    Of course I may be wrong...

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  10. #10
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Pass string to c# program from C dll

    I don't think that's neccessary darwen.

    I checked my project again. I used __declspec( dllexport ) in code, nothing fancy in header a def file to be on the safe side but then without __stdcall.

    Nevertheless, if things go wrong the OP now has another option as well.

  11. #11
    Join Date
    Jun 2007
    Posts
    5

    Re: Pass string to c# program from C dll

    I use Dev C++ compiler for the dll.

    When i try to compile the code S_M_A wrote i get various errors.

    I don't think _TCHAR is a C language type. Or _tcscpy a C language function.

    (Please remember i use C language not C++)

    Thank you for your replies so far.

  12. #12
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Pass string to c# program from C dll

    Sorry, _TCHAR & _tcscpy are MS way of making it possible to build both unicode and not from a single source.

    Build your DLL the DevC++ way but remember that C# expects a unicode wide string so if the DLL not produce that you have to convert using before copying to return pointer. MultiByteToWideChar() should be helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured