c#调用c++ api函数之参数类型问题-急(高人教教我)

jackyped 2007-11-09 10:35:50
c#调用c++ api函数之参数类型问题

c++函数定义如下
extern "C"
{

struct colorstruct
{
double dbValueInterval;
CString str;
IColorPtr ipColor;
colorstruct()
{
str = _T(" ");
dbValueInterval = 0.0;
ipColor.CreateInstance(CLSID_RgbColor);
}


};

__declspec(dllexport) void CressmantestforSK (int a,int irad,float * rads,int isiz,int jsiz,float dbCellSize,double lnmin,double lnmax,double ltmin,double ltmax,long stnnum,float *stnlon,float *stnlat,float *stnrpt,float *grid,colorstruct *colorArray,int iLineNum,float NoDefineVale);


}

C#中extern 如下
[DllImport("CressmanforSk24.dll")]
public extern static void CressmantestforSK( //__declspec(dllexport) void CressmantestforSK (
int a, // int a,
int irad, // int irad,
Single[] rads, // Single * rads,
int isiz, // int isiz,
int jsiz, // int jsiz,
Single dbCellSize, // Single dbCellSize,
double lnmin, // double lnmin,
double lnmax, // double lnmax,
double ltmin, // double ltmin,
double ltmax, // double ltmax,
long stnnum, // long stnnum,
Single[] stnlon, // Single *stnlon,
Single[] stnlat, // Single *stnlat,
Single[] stnrpt, // Single *stnrpt,
out Single[] grid, // Single *grid,
colorstruct[] colorArray, // colorstruct *colorArray,
int iLineNum, // int iLineNum,
Single NoDefineVale // Single NoDefineVale
); // );


public struct colorstruct
{
/// <summary>
/// 数值
/// </summary>
[MarshalAs(UnmanagedType.R8)]
public double dbValueInterval;// = 0;
/// <summary>
/// 名称
/// </summary>
[MarshalAs(UnmanagedType.LPStr)]
public string str;// = " "
/// <summary>
/// 颜色
/// </summary>
[MarshalAs(UnmanagedType.Interface)]
public IColor ipColor;// = new RgbColorClass()


//void colorstruct()
//{
// str = _T(" ");
// dbValueInterval = 0.0;
// ipColor.CreateInstance(CLSID_RgbColor);
//}
};

报错 :尝试读取或写入受保护的内存,内存已损坏


初步确定是参数类型对应的问题,谁能帮帮我啊
msn:jpu_jacky@eyou.com
...全文
164 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyped 2007-11-09
  • 打赏
  • 举报
回复
我要上笨办法了
一个个类型来调

等我搞明白了告诉大家
Love_My 2007-11-09
  • 打赏
  • 举报
回复
帮顶
王集鹄 2007-11-09
  • 打赏
  • 举报
回复
不好翻译的类型(指针)先用IntPtr代替,先把接口调试通过再想办法
struct colorstruct
{
public double dbValueInterval;
public IntPtr str;
public IntPtr ipColor;
}
monkeyfu 2007-11-09
  • 打赏
  • 举报
回复
考虑一下是否是字节边界对齐问题.尝试一下调整Layout的对齐大小
王集鹄 2007-11-09
  • 打赏
  • 举报
回复
估计是这样
void CressmantestforSK(int a, int irad, ref float rads,
int isiz, int jsiz, float dbCellSize,
double lnmin, double lnmax, double ltmin, double ltmax, long stnnum,
ref float stnlon, ref float stnlat, ref float stnrpt,
ref float grid, IntPtr colorArray, int iLineNum, float NoDefineVale);


colorArray参数通过
Marshal.UnsafeAddrOfPinnedArrayElement();

得到初始地址
soaringbird 2007-11-09
  • 打赏
  • 举报
回复
那有个CString啊
jackyped 2007-11-09
  • 打赏
  • 举报
回复
两星都只能帮顶啊?

谁精通的给我们菜鸟上一课吧~
lizhizhe2000 2007-11-09
  • 打赏
  • 举报
回复
帮顶
wsj1983920 2007-11-09
  • 打赏
  • 举报
回复
up
jackyped 2007-11-09
  • 打赏
  • 举报
回复
有精通的朋友加我msn,做arcgis二次开发的更好,先谢谢大家了
马哲 2007-11-09
  • 打赏
  • 举报
回复


[StructLayout(LayoutKind.Sequential)]
public struct colorstruct
{
...
}

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧