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.
Re: Pass string to c# program from C dll
How are you calling a C# DLL ?
Re: Pass string to c# program from C dll
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.)
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.
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)
{
}
}
}
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.
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.
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.
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. :)
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.
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.