VC MFC DLL中定义:
struct FieldElement
{
BSTR Name;
BSTR Value;
bool Sequence;
bool Mandatory;
};long WINAPI ArrayTest(FieldElement *pMySet,long ElementCount)
{
CString _Name;
for (long i=0;i<ElementCount;i++)
{
_Name = (LPCWSTR)(pMySet+i)->Name;
//_Name.Format("%s",(pMySet+i)->Name);
AfxMessageBox(_Name,MB_OK | MB_ICONINFORMATION);
}
return 0;
}
C#调用:
public struct DLLType
{
public string Name;
public string Value;
public bool Sequence;
public bool Mandatory;
}[DllImport("BSTR.dll", EntryPoint = "ArrayTest",CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int ArrayTest(ref DLLType Key,int Count);private void button1_Click(object sender, EventArgs e)
{
DLLType []x= new DLLType[6]; x[0] = new DLLType();
x[0].Name = "sdfasdfa";
x[1] = new DLLType();
x[1].Name = "阿斯顿发送的";
x[2] = new DLLType();
x[2].Name = "萨法四大阿斯顿发送大苏打"; ArrayTest(ref x[0],3);
}显示的都是乱码,但是在VB中调用显示正常。CharSet 属性更改为 CharSet.Ansi 或 CharSet.Unicode都不能正常显示。请问各位高手该怎么解决?
struct FieldElement
{
BSTR Name;
BSTR Value;
bool Sequence;
bool Mandatory;
};long WINAPI ArrayTest(FieldElement *pMySet,long ElementCount)
{
CString _Name;
for (long i=0;i<ElementCount;i++)
{
_Name = (LPCWSTR)(pMySet+i)->Name;
//_Name.Format("%s",(pMySet+i)->Name);
AfxMessageBox(_Name,MB_OK | MB_ICONINFORMATION);
}
return 0;
}
C#调用:
public struct DLLType
{
public string Name;
public string Value;
public bool Sequence;
public bool Mandatory;
}[DllImport("BSTR.dll", EntryPoint = "ArrayTest",CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int ArrayTest(ref DLLType Key,int Count);private void button1_Click(object sender, EventArgs e)
{
DLLType []x= new DLLType[6]; x[0] = new DLLType();
x[0].Name = "sdfasdfa";
x[1] = new DLLType();
x[1].Name = "阿斯顿发送的";
x[2] = new DLLType();
x[2].Name = "萨法四大阿斯顿发送大苏打"; ArrayTest(ref x[0],3);
}显示的都是乱码,但是在VB中调用显示正常。CharSet 属性更改为 CharSet.Ansi 或 CharSet.Unicode都不能正常显示。请问各位高手该怎么解决?
解决方案 »
- System.InvalidCastException: 指定的转换无效
- 去除空格
- 获取返回值问题
- 刚接触C#, 委托是什么东西?可以 public event事件名 as 委托名 这样吗?
- dgv控件里的单元数据在不同form中的txtbox中显示
- DatagridView 显示Excel数据,有些数值型数据为什么显示不正确
- 数据集的Fill(DataSet dataSet,string srcTable)方法后面的srcTable是哪里的表啊?
- 微博通 如何获取 APPKey
- 关于多重继承和接口的问题。
- 数据库解答
- .net Remoting 可以传递泛型吗?
- 请问怎样为Word文档添加页码
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 WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} public struct DLLType
{
public IntPtr Name;
public IntPtr Value;
public bool Sequence;
public bool Mandatory;
} [DllImport("BSTR.dll", EntryPoint = "ArrayTest", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int ArrayTest(ref DLLType Key, int Count); DLLType[] x = new DLLType[3];
private void Form1_Load(object sender, EventArgs e)
{
x[0] = new DLLType();
x[0].Name = Marshal.StringToBSTR("sdfasdfa");
x[1] = new DLLType();
x[1].Name = Marshal.StringToBSTR("阿斯顿发送的");
x[2] = new DLLType();
x[2].Name = Marshal.StringToBSTR("萨法四大阿斯顿发送大苏打");
ArrayTest(ref x[0], 3);
string s1 = Marshal.PtrToStringBSTR(x[0].Value);
string s2 = Marshal.PtrToStringBSTR(x[1].Value);
string s3 = Marshal.PtrToStringBSTR(x[2].Value);
}
}
}