求助。。。托管C++ char* 指针怎么传递??大神请进
我用C#写了个操作数据库的类库,用托管C++来调用,但传递的char*指针不能正确得到结果,不知道怎么回事
我才刚接触托管C++很多不懂,请大家多多帮忙。
代码如下
C#部分不涉及char*就不写了
int SelectDataFromDB( int week,int day,int wItch,char *b,char *c)
{
opearDB ^DB=gcnew opearDB();
String ^sql="select 课程,教室 from CurriculumName where 星期="+day.ToString()+" and 第几节="+wItch.ToString()+" and 从第X周开始<="+week.ToString()+" and 到第X周结束>="+week.ToString();
DataSet ^ds = gcnew DataSet();
ds=DB->select(sql);
if(ds->Tables[0]->Rows->Count>0)
{
b=(char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ds->Tables[0]->Rows[0]["课程"]->ToString());
c=(char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(ds->Tables[0]->Rows[0]["教室"]->ToString());
}
return 0;
}
int main()
{
char a[40]={0},b[40]={0};
if (SelectDataFromDB(8,3,4,a,b))
{
printf("查询失败\n");
system("pause");
return 1;
}
return 0;
}
#include <stdio.h>
#include <string.h>
void func(char *p) {
char *ch = "world";
strcpy(p, ch);
}
int main() {
char a[16] = "hello";
func(a);
printf("%s", a);
return 0;
}