向高手请教:
wince 中用c#中调用c++写的dll,老是报错
我有一个c++调用的例子,我不太会c++
代码如下:
声明时:
typedef BOOL (*FuncQueryTags) (ULONG * pData, int nSize);
FuncQueryTags fnQueryTags;
初始化时:
m_hDll = LoadLibrary(_T("rfidrboce.dll"));
if(!m_hDll)
{
AfxMessageBox(_T("无法加载dll!"));

else
{
fnQueryTags = (FuncQueryTags)GetProcAddress(m_hDll, _T("RFIDCE_QueryTags"));
if (!fnQueryTags)
{
AfxMessageBox(_T("Cannot Find QueryTags!"));
}
        }调用代码:
void CTestRfidCEDlg::OnBUTTONQueryTags() 
{
ULONG tags[255];
// call the function
int count = fnQueryTags(tags, 255);
if (count == -1)
{
AfxMessageBox(_T("QueryTags error!"));
}
else 
{
CString strX;
strX.Format(_T("Find %d Tags:\n"), count);
for(int i=0; i < count; i ++)
{
CString strTemp;
strTemp.Format(_T("%u"), tags[i]);
strX += strTemp;
strX += (i+1)%5 ? _T(",") : _T("\n") ;
}
AfxMessageBox(strX);
}}
我在c#中是这样写的
调用dll
 [DllImport("rfidrboce.dll", EntryPoint = "RFIDCE_QueryTags", SetLastError = true)]
 private static extern int RFIDCE_QueryTags([MarshalAs(UnmanagedType.LPArray)]uint[] bufptr, int nSize);
uint[] tags = new uint[255];
count = RFIDCE_QueryTags(tags, 255);//按道理结果是count是读到数据的条数 ,tags是数据汇总
可是我结果却count最多为1,大部分都是0我觉得这里应该是参数类型问题,试了好多其他办法,也没出来结果,请大家帮忙看看,非常感谢!

解决方案 »

  1.   

    你做的是无线射频呀!我以臆以是做这个的!有些怀念!![DllImport("rfidrboce.dll", EntryPoint = "RFIDCE_QueryTags", SetLastError = true)] 
    private static extern int RFIDCE_QueryTags([MarshalAs(UnmanagedType.LPArray)]uint[] bufptr, int nSize); 为什么一这样的写呢MarshalAs(UnmanagedType.LPArray)]uint[] bufptr
    直接写成int[] bufptr不行吗?[DllImport("rfidrboce.dll", EntryPoint = "RFIDCE_QueryTags", SetLastError = true)] 
    private static extern int RFIDCE_QueryTags(int[] bufptr, int nSize); 
      

  2.   

    你做的是无线射频呀!我以臆以是做这个的!有些怀念!![DllImport("rfidrboce.dll", EntryPoint = "RFIDCE_QueryTags", SetLastError = true)] 
    private static extern int RFIDCE_QueryTags([MarshalAs(UnmanagedType.LPArray)]uint[] bufptr, int nSize); 为什么一这样的写呢MarshalAs(UnmanagedType.LPArray)]uint[] bufptr
    直接写成int[] bufptr不行吗?[DllImport("rfidrboce.dll", EntryPoint = "RFIDCE_QueryTags", SetLastError = true)] 
    private static extern int RFIDCE_QueryTags(int[] bufptr, int nSize); 
      

  3.   

    回xt0055:
    不是无线射频,是读卡器
    用int[],我试过了 ,直接报错奥
    我查过资料,有人用vb这样调用
    uint[] tags = new uint[255]
    count = RFIDCE_QueryTags(tags[0], 255)
    说数值型数组在VB中其数据是连续存放的,相当于一维的,而在C/C++中数组可以等价于指向数组第1个元素的指针
    把数组的第1个元素的地址传给VC编写的DLL,就可以了
    但是c#好象不行呢
      

  4.   

    ref int[] bufptr
    再试试!
      

  5.   

    to xt0055 :
    ref int[] bufptr 也试过了,报错的
    我觉得这应该是托管和非托管之间参数转换问题
      

  6.   

    用指针解决了 
    To to xt0055:
    分全给你了