posts - 38, comments - 18, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

最新评论

Re:C#程序实现动态调用DLL的研究(转载) ilovemeyou2000 2012-05-08 11:55  
你好 我碰上一个问题,我如何取得DLL指定的类的一个实例呢? DLD dld = new DLD(); dld.LoadDll(PluginDLLPath); string typeName="MyApp.MyClass"; MyClass mc =dld... //这边传入typeName,返回对应的实例 要怎么写呢?
问题好像是解决了,如下;(但是原因没有搞清楚,还是菜鸟) object[] Parameters = new object[]{a}; // 实参 Type[] ParameterTypes = new Type[]{typeof(int).MakeByRefType()}; // 实参类型为引用类型 ModePass[] themode = new ModePass[] { ModePass.ByValue }; // 传送方式为传值 Type Type_Return = typeof(int); // 返回类型 int ret = (int)(myfun.Invoke(Parameters,ParameterTypes,themode,Type_Return)); int newa = (int)Parameters[0]; //引用的Parameters[0]被赋值了。
[quote]小堆爸: [quote]小光123456:试过了,其它都没问题,但引用类型传进去并不能被赋值,可否解释一下。[/quote] 对啊,在动态调用托管DLL时,例如函数是 int fun(ref int a);这时候a的值就不能被赋值,要怎么传参数呢?[/quote] 将传送方式设为传引用了,还是不能被赋值。跟踪代码执行没有发现什么问题,怎么回事?
[quote]小光123456:试过了,其它都没问题,但引用类型传进去并不能被赋值,可否解释一下。[/quote] 对啊,在动态调用托管DLL时,例如函数是 int fun(ref int a);这时候a的值就不能被赋值,要怎么传参数呢?
re: Use Case编写建议(转载) 左轮 2009-05-02 00:50  
很不错。这样专研、实践的少见。很专业。
re: C#程序实现动态调用DLL的研究(转载) 小光123456 2008-06-06 10:37  
试过了,其它都没问题,但引用类型传进去并不能被赋值,可否解释一下。
re: C#程序实现动态调用DLL的研究(转载) 求知无傲 2008-06-04 15:40  
kankan
re: vb和vc混合编程实例(ocx和dll) (转载) 依然冬虫草 2008-05-29 10:10  
请问vb如何调用VC写的ocx啊?
只打SP3的补丁就可以可吗,而且是必须的是吗,那SP4还用打吗?
re: C#程序实现动态调用DLL的研究(转载) 菜鸟一只 2007-06-06 13:52  
代码中,object 0 是在if语句块中定义的局部变量,在最后return (object)0时已经超出了其作用域。该代码是否实现过?
public object Invoke(string lpFileName,string Namespace,string ClassName,string lpProcName,object[] ObjArray_Parameter)

{

try

{// 判断 MyAssembly 是否为空或 MyAssembly 的命名空间不等于要调用方法的命名空间,如果条件为真,就用 Assembly.Load 加载所需 DLL 作为程序集

if(MyAssembly==null||MyAssembly.GetName().Name!=Namespace)

MyAssembly=Assembly.Load(LoadDll(lpFileName));

Type[] type=MyAssembly.GetTypes();

foreach(Type t in type)

{

if(t.Namespace==Namespace&&t.Name==ClassName)

{

MethodInfo m=t.GetMethod(lpProcName);

if(m!=null)

{// 调用并返回

object o=Activator.CreateInstance(t);

return m.Invoke(o,ObjArray_Parameter);

}

else

System.Windows.Forms.MessageBox.Show(" 装载出错 !");

}

}

}

catch(System.NullReferenceException e)

{

System.Windows.Forms.MessageBox.Show(e.Message);

}

return (object)0;

}
"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。"
我总是出现这个错误,能否帮忙看看……
在.net中的声明和调用如下
[DllImport("C:\\Dev-Cpp\\新建文件夹\\libProject1.dll", EntryPoint = "_ZN4Trie6insertEPc", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Auto, SetLastError = true)]
public static extern void insert(IntPtr ths,ref String word);


[DllImport("C:\\Dev-Cpp\\新建文件夹\\libProject1.dll", EntryPoint = "_ZN4Trie9wordFoundEPc", CallingConvention = CallingConvention.ThisCall, CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool wordFound(IntPtr ths,ref 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 void button4_Click(object sender, EventArgs e)
{
}
private 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[] { ' '});
foreach (string word in split)
{
count++;

}
for (int i = 0; i < count;i++ )
{
try
{
// string n_size = instancePtr.ToString();
// MessageBox.Show(n_size);
insert(instancePtr, ref split[i]);
DeleteTrie(instancePtr);

string ww = split[i];
MessageBox.Show(ww);

if (wordFound(instancePtr, ref ww))

MessageBox.Show("founded");
}
catch (Exception es)
{
MessageBox.Show(es.ToString());
}
}
}
}
objReader.Close();

}
我的dll源程序的头文件是这样的
#ifndef _DLL_H_
#define _DLL_H_


# define DLLIMPORT __declspec (dllexport)

#include <iomanip.h>
class Trie;
class TrieNonLeafNode {

public:
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(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_ */

急切盼望你的回复,小妹刚出道阿,希望你能帮帮我。
谢谢 果然高手 帮忙解决了大问题 再接再厉
真的只是打补丁的问题吗?
很受益,关注中
2006-08-09 21:41 by wuChang
win2003 + sqlserver2000时 sql2000一定得打sp3其他机才能连上,直接打sp4都不行的!

谢谢大大了,我试试
win2003 + sqlserver2000时 sql2000一定得打sp3其他机才能连上,直接打sp4都不行的!
re: Jar文件揭密(转载) 婕舞飞扬 2005-08-23 15:47  
golenocean