标题:C#调用API的一些基础(希望有人用的上)
只看楼主
pink_girl
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-4
得分:0 
[CODE][CODE][CODE]楼主帮帮忙,看看我这个调用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();



}
[/CODE]
老是出现这样的错误信息阿。"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
我的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]
急切盼望你的回复
2007-06-05 10:24
yuer1231
Rank: 1
等 级:新手上路
帖 子:40
专家分:0
注 册:2006-11-30
得分:0 

支持一下了


2007-06-05 12:42
jdfsl
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-7-3
得分:0 
这个东西很重要,但是确实是看不懂,顶一下。
2007-07-03 21:42
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
得分:0 
好文章要顶的!

2007-07-03 21:44
星之魂
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-12-28
得分:0 
刚接触API,想了解一下
谢谢,支持!
2008-01-24 14:15
dachun
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-8-5
得分:0 
恩 很实用!
2008-12-11 20:23
TMG
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-8-29
得分:0 
支持一下啦.
2008-12-13 22:07
TMG
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-8-29
得分:0 
支持一下啦.
2008-12-13 22:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-127497-1-1.html




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

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