该死的C#调用C++dll问题
我现在用C++写了一个dll,里面有几个函数//DLL1_API void _stdcall outputchar(char *a,int b);输出a数组中前b个元素
//DLL1_API char * _stdcall outputcharx(char *a,char b,int t);//
//用字符b替换t位置的字符
//DLL1_API unsigned char _stdcall putuc(unsigned char * a,int b);//输出a数组中第b个元素
现在我用C#来调用这个dll代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace 引用
{
#region Struct
#endregion
public sealed class Program2
{
#region decode
[DllImport("dll1.dll", EntryPoint = "addAraay")]
//LL1_API int _stdcall addAraay(int *a,int b)
public static extern int addAraay( int[] a, int b);
[DllImport("dll1.dll")]
//DLL1_API void _stdcall outputchar(char *a,int b);输出前b个
public static extern void outputchar( char[] a, int b);//输出前b个
[DllImport("dll1.dll")]
//DLL1_API char * _stdcall outputcharx(char *a,char b,int t);//插入在t位置
public static extern char outputcharx( char[] a, char b, int t);//用字符b替换t位置的字符
[DllImport("dll1.dll")]
//DLL1_API unsigned char _stdcall putuc(unsigned char * a,int b);//输出第b个
public static extern char putuc( char[] a, int b);//输出第b个
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
namespace 引用
{
class Program
{
static void Main(string[] args)
{
int[] qwe = new int[12]{1,2,3,4,5,6,7,8,9,10,11,12};
int m = 5;
int q = 0;
char[] Str ={ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'n' };
char x = 'v';
char d='x';
q=Program2.addAraay(qwe,m);
Console.WriteLine("ok");
Console.WriteLine( q);
Program2.outputchar( Str, 3);
Console.WriteLine(Str[m]);
Program2.outputcharx( Str,d, 0);
Console.WriteLine( Str[m]);
x= Program2.putuc( Str,7);
Console.WriteLine(x);
}
}
}
现在问题来了,调用第一个和第三个没问题,调用char * _stdcall outputcharx(char *a,char b,int i)//用字符b替换t位置的字
时老出问题。
我试过用ref来调用数组Str了,不能调用成功。我的问题核心是我想调用一个函数来改变这个数组的值再返回来时就有问题了,求高人解决一下
--------------------编程问答-------------------- //DLL1_API char * _stdcall outputcharx(char *a,char b,int t);//插入在t位置
public static extern char outputcharx( char[] a, char b, int t);//用字符b替换t位置的字符 char * 对应 C#的 string 或 stringbuilder吧? --------------------编程问答--------------------
我也在搞这玩意 头大 --------------------编程问答-------------------- 还有我按照网上的说法修改数据类型的时候就不对了,比如char *改成string,int*改成ref int,都错的一塌糊涂。到底怎么回事 --------------------编程问答-------------------- http://blog.csdn.net/sjzlxd/archive/2009/02/27/3940246.aspx
ref是需要返回值才用的,指针才可以返回
char * 尝试用StringBuilder
int * 要看需要不需要返回值,不需要的话直接就是int --------------------编程问答-------------------- stringbuffer --------------------编程问答-------------------- 用
ref stringbuffer --------------------编程问答-------------------- 是哪个参数要返回值?
你得先弄清楚outputcharx这个函数的实际意义
然后再去对应的替换
--------------------编程问答-------------------- char * _stdcall outputcharx(char *a,char b,int i)就是用字符b替换t位置的字符,其他不变。我调用这个函数就是要他改变现在已有的一个字符数组,然后把改变的数组传递回来。关键是现在调用这个函数没有反应
补充:.NET技术 , C#