|
-
June 13th, 2007, 05:32 AM
#1
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.
-
June 13th, 2007, 05:34 AM
#2
Re: Pass string to c# program from C dll
How are you calling a C# DLL ?
Regards,
Ramkrishna Pawar
-
June 13th, 2007, 05:46 AM
#3
Re: Pass string to c# program from C dll
-
June 13th, 2007, 06:03 AM
#4
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.)
-
June 13th, 2007, 07:00 AM
#5
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.
-
June 13th, 2007, 08:16 AM
#6
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)
{
}
}
}
-
June 13th, 2007, 08:49 AM
#7
Re: Pass string to c# program from C dll
 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]
-
June 13th, 2007, 09:00 AM
#8
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.
-
June 13th, 2007, 09:46 AM
#9
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.
-
June 13th, 2007, 10:27 AM
#10
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.
-
June 14th, 2007, 01:05 PM
#11
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.
-
June 14th, 2007, 01:55 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|