紧急求教!p/invoke调用C/C++的DLL,C#的函数怎么声明,谢谢!

songcan 2010-03-25 10:46:26
c/c++
extern int __cdecl fx_setparm(int dev, unsigned long parm, const void * valuep);
struct DeviceStruct
{
int voxhandle;
int faxhandle;
int dtihandle;
int dtidevice;
char RcvFile[MAX_PATH+1];
char SendFile[MAX_PATH+1];
char DialString[MAXSTRL+1];
char LocalID[MAXSTRL+1];
char UsrHeader[MAXUSRHDR];
BOOL ecm;
BOOL handler;
int txcoding;
int Speed;
long type;
int stcoding;
WORD nFileOffset;

};
typedef struct DeviceStruct PORT;
PORT Chdev;

/* init parameters */
localidp = Chdev.LocalID;
if (fx_setparm(Chdev.faxhandle,FC_LOCALID,(void *)localidp)!=0) {
disperror(Chdev.faxhandle,"Setting local id");


c#包装

[DllImport("libfaxmt.dll", EntryPoint = "fx_setparm")]
public static extern int fx_setparm(int dev, ulong parm, IntPtr valuep);
/* init parameters */
localidp = Chdev.LocalID;
IntPtr ptr = Marshal.StringToHGlobalAnsi(localidp);
if (clsAPI.fx_setparm(Chdev.faxhandle, (ulong)FC_LOCALID, ptr) != 0)
{

}
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DeviceStruct
{

/// int
public int voxhandle;
/// int
public int faxhandle;
/// int
public int dtihandle;
/// int
public int dtidevice;
/// char[261]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 261)]
public string RcvFile;
/// char[261]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 261)]
public string SendFile;
/// char[]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 21)]
public string DialString;
/// char[]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 21)]
public string LocalID;
/// char[]
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 32)]
public string UserHeader;
/// BOOL->int
public bool ecm;
/// BOOL->int
public bool handler;
/// int
public int txcoding;
/// int
public int Speed;
/// int
public int type;
/// int
public int stcoding;
/// WORD->unsigned short
public ushort nFileOffset;
}
这样转换后,运行出现系统报尝试读取或写入受保护的内存。这通常指示其他内存已损坏的错误,请哪位大侠教下我,谢谢!
...全文
71 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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