| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1258 人关注过本帖
标题:[求助]c#调用c++非托管dll
只看楼主 加入收藏
pink_girl
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-4
收藏
 问题点数:0 回复次数:3 
[求助]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]

希望有人能帮帮我,我已经在这个问题上搞了三天了,就快没有时间了。
各位高手请帮帮忙。
搜索更多相关主题的帖子: dll CharSet 托管 DllImport 
2007-06-05 11:00
pink_girl
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-4
收藏
得分:0 
有没有人帮我啊?
在线等啊!
2007-06-05 13:32
slcfhr
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2006-6-16
收藏
得分:0 
执行在那个位置出现的此错

有肚量去容忍那些无法改变的事情 有勇气去改变那些可以改变的事情 有智慧去区别上述二类事
2007-06-05 18:03
slcfhr
Rank: 1
等 级:新手上路
帖 子:125
专家分:0
注 册:2006-6-16
收藏
得分:0 
public static extern void insert(char[] word);

public static extern bool wordFound(ref char[] word);

没有看明白,,可能出现在上面这两个地方吧

有肚量去容忍那些无法改变的事情 有勇气去改变那些可以改变的事情 有智慧去区别上述二类事
2007-06-05 18:18
快速回复:[求助]c#调用c++非托管dll
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016905 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved