songcan 2010年03月25日
紧急求教!p/invoke调用C/C++的DLL,C#的函数怎么声明,谢谢!
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;
}
这样转换后,运行出现系统报尝试读取或写入受保护的内存。这通常指示其他内存已损坏的错误,请哪位大侠教下我,谢谢!
...全文
33 点赞 收藏 回复
写回复
回复

还没有回复,快来抢沙发~

发动态
发帖子
C语言
创建于2007-09-28

3.2w+

社区成员

24.0w+

社区内容

C语言相关问题讨论
社区公告
暂无公告