首页 新闻 会员 周边

C#如何调用C++Dll类中的类

0
悬赏园豆:10 [已关闭问题] 关闭于 2011-03-26 13:29
在VS2008中用C++生成DLL为Test,Test中有类BM,BM定义如下:如何在C#中调用BM中的insert和contains成员函数调用?
  class BM
  {
public:
bloom_filter(unsigned int tbl_size)
{
table_size = tbl_size;
hash_table = new unsigned char[table_size];
for(std::size_t i = 0; i < table_size; ++i) hash_table[i] = 0;
}

void insert(char* key)
{
for(std::size_t i = 0; i < hash_function.size(); i++)
{
unsigned int hash = hash_function[i](key,3) % (table_size * char_size);
hash_table[hash / char_size] |= bit_mask[hash % char_size];
}
}
/* bool contains(const std::string& key)*/
bool contains(char* key)
{
for(std::size_t i = 0; i < hash_function.size(); i++)
{
unsigned int hash = hash_function[i](key,3) % (table_size * char_size);
unsigned int bit = hash % char_size;
if ((hash_table[hash / char_size] & bit_mask[bit]) != bit_mask[bit])
{
return false;
}
}
return true;
}

private:
std::vector < hash_function > hash_function;
unsigned char* hash_table;
unsigned int table_size;
};
 
 
niesen111的主页 niesen111 | 初学一级 | 园豆:0
提问于:2011-03-17 18:42
< >
分享
所有回答(3)
0

C#直接调用不支持,你使用P/Invoke的话,只能引入DLL的引出函数,因此最好包装成标准的WIndows API风格在C#中调用;或者使用C++/CLI包装你的类,然后C#调用C++/CLI,不过调用层次有些多

2012 | 园豆:21230 (高人七级) | 2011-03-17 20:32
0

http://zhidao.baidu.com/question/204935129.html

这里有个解答,LZ可以看看。

大概是加这么一句:

using System.Runtime.InteropServices;//引入dll文件中的函数

Rusty's code | 园豆:410 (菜鸟二级) | 2011-03-18 09:15
0
shfong | 园豆:210 (菜鸟二级) | 2011-03-18 09:47
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册