最匹配的重载方法具有一些无效参数 / 类型不匹配
用VC建立DLL 在C#做的界面下使用 使用指针传递问题:
错误 1 与“a.Form1.cCdll.strA(char[], char[])”最匹配的重载方法具有一些无效参数
错误 2 参数“1”: 无法从“string”转换为“char[]”
错误 3 参数“2”: 无法从“string”转换为“char[]”
希望高手给些指点:
C#代码如下:
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 a
{
public partial class Form1 : Form
{
public class cCdll
{
[DllImport("test.dll", EntryPoint = "strA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static unsafe extern char *strA(char[] a, char[] b);
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = cCdll.strA(Convert.ToString(textBox1.Text),Convert.ToString(textBox2.Text)).ToString(); /*错误出现在此行*/
}
}
} --------------------编程问答-------------------- string s = textBox1.Text;
byte[] b = UnicodeEncoding.Default.GetBytes(s);
char[] c = UnicodeEncoding.Default.GetChars(b, 0, b.Length);
string s1 = textBox2.Text;
byte[] b1 = UnicodeEncoding.Default.GetBytes(s);
char[] c1 = UnicodeEncoding.Default.GetChars(b, 0, b.Length);
textBox3.Text = cCdll.strA(C,C1);
--------------------编程问答-------------------- [DllImport("test.dll", EntryPoint = "strA", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr strA(IntPtr a, IntPtr b);
改这样吧,C#的char貌似不是单字节的
IntPtr a=Marshal.StringToBSTR(textBox1.Text);
IntPtr b=Marshal.StringToBSTR(textBox2.Text);
IntPtr d=strA(a,b);
TextBox3.Text=Marshal.PtrToStringAuto(d);
试试看 --------------------编程问答-------------------- textBox3.Text = cCdll.strA(textBox1.Text.ToCharArray(),textBox2.Text.ToCharArray()).ToString();
补充:.NET技术 , C#