C++中的函数的源代码如下:
struct DYNAMICDLL_API cdp_tree1
{
int count;
CString DeviceID;
CString Addresses;
CString PortID;
CString platform;
} ;
int libmyethereal::testsum(int a,int b,int *outnum, cdp_tree1 *cdptree1)
{ int c=0;
c=a+b;
*outnum=356;
cdptree1[0].count=356;
cdptree1[0].DeviceID="abc";
cdptree1[0].Addresses="dfg";
cdptree1[0].platform=123;
cdptree1[0].PortID="aaa";
cdptree1[1].count=444;
cdptree1[1].DeviceID="fgh";
cdptree1[1].Addresses="dfg";
cdptree1[1].platform=123;
cdptree1[1].PortID="aaa";
..... return c;}
c#中调用时的代码如下, public struct mycdptree
{
public int count;
public String DeviceID;
public String Addresses;
public String PortID;
public String platform;
}; [DllImport("libmyeth.dll", EntryPoint = "?testsum@libmyethereal@@QAEHHHPAHPAUcdp_tree1@@@Z", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int testsum(int a, int b, ref int outnum ,out mycdptree[] cdptree1);
private void button1_Click(object sender, System.EventArgs e)
{
.....
int c=-1;
mycdptree[] cdptree2= new mycdptree[3];
for (int i = 0; i < 3; i++)
{
cdptree2[i].count = 3;
cdptree2[i].DeviceID = "12";
cdptree2[i].platform = "11";
cdptree2[i].PortID = "13";
cdptree2[i].Addresses = "13";
} cdptree[] cdptree1=new cdptree[1];
int num=0 ;
// for(int i=0;i<3;i++)
c = testsum(3, 300,ref num, ref cdptree2[0]);问题:怎么才能把cdptree2数组中的三个对象的值都改变了,急求!!
struct DYNAMICDLL_API cdp_tree1
{
int count;
CString DeviceID;
CString Addresses;
CString PortID;
CString platform;
} ;
int libmyethereal::testsum(int a,int b,int *outnum, cdp_tree1 *cdptree1)
{ int c=0;
c=a+b;
*outnum=356;
cdptree1[0].count=356;
cdptree1[0].DeviceID="abc";
cdptree1[0].Addresses="dfg";
cdptree1[0].platform=123;
cdptree1[0].PortID="aaa";
cdptree1[1].count=444;
cdptree1[1].DeviceID="fgh";
cdptree1[1].Addresses="dfg";
cdptree1[1].platform=123;
cdptree1[1].PortID="aaa";
..... return c;}
c#中调用时的代码如下, public struct mycdptree
{
public int count;
public String DeviceID;
public String Addresses;
public String PortID;
public String platform;
}; [DllImport("libmyeth.dll", EntryPoint = "?testsum@libmyethereal@@QAEHHHPAHPAUcdp_tree1@@@Z", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int testsum(int a, int b, ref int outnum ,out mycdptree[] cdptree1);
private void button1_Click(object sender, System.EventArgs e)
{
.....
int c=-1;
mycdptree[] cdptree2= new mycdptree[3];
for (int i = 0; i < 3; i++)
{
cdptree2[i].count = 3;
cdptree2[i].DeviceID = "12";
cdptree2[i].platform = "11";
cdptree2[i].PortID = "13";
cdptree2[i].Addresses = "13";
} cdptree[] cdptree1=new cdptree[1];
int num=0 ;
// for(int i=0;i<3;i++)
c = testsum(3, 300,ref num, ref cdptree2[0]);问题:怎么才能把cdptree2数组中的三个对象的值都改变了,急求!!
c++的类函数,在编译后第一个参数是指向this的指针,与你声明的函数不一致
改为static函数试试
C++中的函数的源代码如下:
struct cdp_tree1
{
int count;
CString DeviceID;
CString Addresses;
CString PortID;
CString platform;
} ;
int testsum(int a,int b,int *outnum, cdp_tree1 *cdptree1)
{ int c=0;
c=a+b;
*outnum=356;
cdptree1[0].count=356;
cdptree1[0].DeviceID="abc";
cdptree1[0].Addresses="dfg";
cdptree1[0].platform=123;
cdptree1[0].PortID="aaa"; cdptree1[1].count=444;
cdptree1[1].DeviceID="fgh";
cdptree1[1].Addresses="dfg";
cdptree1[1].platform=123;
cdptree1[1].PortID="aaa";
..... return c; }
c#中调用时的代码如下, public struct mycdptree
{
public int count;
public String DeviceID;
public String Addresses;
public String PortID;
public String platform;
}; [DllImport("libmyeth.dll", EntryPoint = "?testsum@libmyethereal@@QAEHHHPAHPAUcdp_tree1@@@Z", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int testsum(int a, int b, ref int outnum ,out mycdptree[] cdptree1);
private void button1_Click(object sender, System.EventArgs e)
{
.....
int c=-1;
mycdptree[] cdptree2= new mycdptree[3];
c = testsum(3, 300,ref num, ref cdptree2[0]);
有谁能给我一个类似的例子,定重谢!!!!!!!!!!!
2、但C#并不知道CString的具体实现,不知道如何把CString转换为C# string,也不会析构CString。
3、即使通过一些偏移量硬从CString得到内部的string,也很容易造成内存的泄漏。
4、其实就是都用C++,不用版本的CString也会造成麻烦。