[求助]c#调用c++非托管dll
附件里面是我的dll的源文件下面是我的主程序里面调用的
[DllImport("C:\\Dev-Cpp\\新建文件夹\\libProject1.dll", EntryPoint = "_ZN4Trie6insertEPc", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Auto, SetLastError = true)]
public static extern void insert(IntPtr ths,string word);
[DllImport("C:\\Dev-Cpp\\新建文件夹\\libProject1.dll", EntryPoint = "_ZN4Trie9wordFoundEPc", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool wordFound(IntPtr ths,string word);
[DllImport("C:\\Dev-Cpp\\新建文件夹\\libProject1.dll" )]
public static extern IntPtr CreateTrie(string word);
[DllImport("C:\\Dev-Cpp\\新建文件夹\\libProject1.dll")]
public static extern void DeleteTrie(IntPtr instance);
private unsafe void Form1_Load(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("C:\\1.txt");
string sLine = "";
ArrayList arrText = new ArrayList();
string s = "a";
IntPtr instancePtr = CreateTrie(s);
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
{
string[] split = null;
int count = 0;
split = sLine.Split(new char[] { ' '},StringSplitOptions.RemoveEmptyEntries);
foreach (string word in split)
{
count++;
}
for (int i = 0; i < count;i++ )
{
string ww = split[i];
MessageBox.Show(ww);
// string n_size = instancePtr.ToString();
// MessageBox.Show(n_size);
try
{
insert(instancePtr, ww);
// if (wordFound(instancePtr,ref ww))
// MessageBox.Show("founded");
}
catch (Exception es)
{
MessageBox.Show(es.ToString());
}
DeleteTrie(instancePtr);
}
}
}
objReader.Close();
}
老是出现这样的错误信息阿。"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
我的dll中的源函数是这样定义的。
[/code]
#ifndef _DLL_H_
#define _DLL_H_
# define DLLIMPORT __declspec (dllexport)
#include <iomanip.h>
class Trie;
class TrieNonLeafNode {
public:
TrieNonLeafNode( ){};
TrieNonLeafNode(char ch)
{
ptrs = new TrieNonLeafNode*;
letters = new char[2];
if (ptrs == 0 || letters == 0) {
return ;
}
leaf = false;
endOfWord = false;
*ptrs = 0;
*letters = ch;
*(letters+1) = '\0';
};
private:
bool leaf, endOfWord;
char *letters;
TrieNonLeafNode **ptrs;
friend class Trie;
};
class TrieLeafNode {
public:
TrieLeafNode( ){};
TrieLeafNode(char* suffix){
leaf = true;
word = new char[strlen(suffix)+1];
if (word == 0) {
// cerr << "Out of memory2.\n";
// exit(-1);
return ;
}
strcpy(word,suffix);
};
private:
bool leaf;
char *word;
friend class Trie;
};
class Trie
{
public:
Trie(char *);
// void DLLIMPORT printTrie();
void DLLIMPORT insert(char*);
bool DLLIMPORT wordFound(char*);
private:
TrieNonLeafNode *root;
int notFound;
char prefix[80];
int position(TrieNonLeafNode*,char);
void addCell(char,TrieNonLeafNode*,int);
void createLeaf(char,char*,TrieNonLeafNode*);
// void printTrie(int,TrieNonLeafNode*,char*);
};
extern "C" DLLIMPORT Trie * CreateTrie(char * word);
extern "C" DLLIMPORT void DeleteTrie( Trie* instance );
#endif /* _DLL_H_ */
[/code]
希望有人能帮帮我,我已经在这个问题上搞了三天了,就快没有时间了。
各位高手请帮帮忙。