日期:2014-05-18  浏览次数:20807 次

请教:wince+C#中如何调用一个这样的C++ DLL,非常感谢
向高手请教:
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

我觉得这里应该是参数类型问题,试了好多其他办法,也没出来结果,请大家帮忙看看,非常感谢!

------解决方案--------------------
你做的是无线射频呀!我以臆以是做这个的!有些怀念!!

[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);