dllimport参数问题

lhprince 2011-05-07 01:29:47

C#中结构的定义
public struct AccountMsg
{

[MarshalAs(UnmanagedType.ByValTStr,SizeConst=21)]
public string Name; /*姓名四个汉字*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public string SexNo; /*性别*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=19)]
public string DeptCode; /*部门代码*/

public UInt32 CardNo; /*卡号*/

public Int32 AccountNo; /*帐号*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string StudentCode; /*学号*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string IDCard; /*身份证号*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 3)]
public string PID; /*身份代码*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 13)]
public string IDNo; /*身份序号*/
public Int32 Balance; /*现余额*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string Password; /*消费密码*/
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 7)]
public string ExpireDate; /*账户截止日期*/
public UInt16 SubSeq; /*补助戳*/
public char IsOpenInSys; /*是否在本系统内开通*/
public Int16 TerminalNo; /*终端号码*/
public Int16 RetCode; /*后台处理返回值*/
}



C++中头文件的定义

typedef struct
{
char Name[21]; /*姓名四个汉字*/
char SexNo[2]; /*性别*/
char DeptCode[19]; /*部门代码*/
unsigned int CardNo; /*卡号*/
unsigned int AccountNo; /*帐号*/
char StudentCode[21]; /*学号*/
char IDCard[21]; /*身份证号*/
char PID[3]; /*身份代码*/
char IDNo[13]; /*身份序号*/
int Balance; /*现余额*/
char Password[7]; /*消费密码*/
char ExpireDate[7]; /*账户截止日期*/
unsigned short SubSeq; /*补助戳*/
char IsOpenInSys; /*是否在本系统内开通*/
short TerminalNo; /*终端号码*/
short RetCode; /*后台处理返回值*/
} AccountMsg;


方法的声明
public class CardStuff
{

[DllImport("C:\\windows\\system32\\AIO_API.dll",CallingConvention=CallingConvention.Cdecl)]
public static extern int TA_ReadCardSimple( ref AccountMsg pAccMsg);

}



调用方法之后传回的结构体中部分参数值不正确,比如cardno和accountno高手们帮看看到底哪里错了,谢了
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhprince 2011-05-07
  • 打赏
  • 举报
回复
自己顶
lhprince 2011-05-07
  • 打赏
  • 举报
回复
有人知道吗?
lhprince 2011-05-07
  • 打赏
  • 举报
回复
int _stdcall TA_ReadCardSimple(AccountMsg * pAccMsg);
源代码我没有
bdmh 2011-05-07
  • 打赏
  • 举报
回复
c++ 原型是啥,是Cdecl调用还是stdcall调用
lhprince 2011-05-07
  • 打赏
  • 举报
回复
AccountNo是UInit32也不正确
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功能呢?答案是肯定的,大家可以通过C#中的DllImport直接调用这些功能。 DllImport所在的名字空间 using System.Runtime.InteropServices; MSDN中对DllImportAttribute的解释是这样的:可将该属性应用于方法。DllImportAttribute 属性提供对从非托管 DLL 导出的函数进行调用所必需的信息。作为最低要求,必须提供包含入口点的 DLL 的名称。 DllImport 属性定义如下: namespace System.Runtime.InteropServices {   [AttributeUsage(AttributeTargets.Method)]   public class DllImportAttribute: System.Attribute   {    public DllImportAttribute(string dllName) {...}    public CallingConvention CallingConvention;    public CharSet CharSet;    public string EntryPoint;    public bool ExactSpelling;    public bool PreserveSig;    public bool SetLastError;    public string Value { get {...} }   } }   说明:   1、DllImport只能放置在方法声明上。   2、DllImport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。   3、DllImport具有五个命名参数:    a、CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。    b、CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。    c、EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。    d、ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。    e、PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。    f、SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。   4、它是一次性属性类。   5、此外,用 DllImport 属性修饰的方法必须具有 extern 修饰符。
#region 导入API函数 [DllImport("avicap32.dll")]//包含了执行视频捕获的函数,它给AVI文件I/O和视频、音频设备驱动程序提供一个高级接口 public static extern IntPtr capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hwndParent, int nID); /************参数说明************* * * 函数:capCreateCaptureWindow * * lpszWindowName:标识窗口的名称 * dwStyle:标识窗口风格 * x、y:标识窗口的左上角坐标 * nWidth、nHeight:标识窗口的宽度和高度 * hWnd:标识父窗口句柄 * nID:标识窗口ID * * 返回值:视频捕捉窗口句柄。 * ********************************/ [DllImport("AVICAP32.dll", CharSet = CharSet.Unicode)] public static extern bool capGetDriverDescription(int wDriverIndex, StringBuilder lpszName, int cbName, StringBuilder lpszVer, int cbVer); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPDRIVERCAPS lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPTUREPARMS lParam); [DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref CAPSTATUS lParam); [DllImport("User32.dll")] public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); [DllImport("avicap32.dll")] public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize); #endregion

110,539

社区成员

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

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

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