DllImport引入的函数经常异常,怎么能使DLL卸载后重新引入这个函数继续运行呢?

liangzha 2008-07-17 11:57:35
我引入一个DLL后,需要循环调用它的一个函数 这个函数很不稳定,经常抛出异常,再次调用这个就会死机,必须关闭程序才正常

请问捕捉到异常后怎么处理,能使这个dll重新载入使函数能再次使用

[DllImport("AdvOcr.dll", EntryPoint = "OCR_C", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]

protected static extern string OCR_C(string type, string fileName);


while(true){
try {
string result = OCR_C(OCR_type, filePath);

}
catch {

MessageBox.Show("出错了!");


}
}
...全文
354 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
西门豆豆 2008-10-22
  • 打赏
  • 举报
回复
原来这里已经解决:http://topic.csdn.net/t/20050609/15/4070992.html
西门豆豆 2008-10-22
  • 打赏
  • 举报
回复
定一个,遇到同样的问题,搜到此贴,顶!!!!!!!!
liangzha 2008-07-18
  • 打赏
  • 举报
回复
我现在有个办法 就是捕捉到错误自动关闭自身
然后再运行自身程序

然后如何触发按钮事件然程序自己运行呢?

请问有谁能指导下相关代码如何关闭自身,再运行自身程序,再触发按钮事件?

liangzha 2008-07-17
  • 打赏
  • 举报
回复
释放也是dll本身的接口函数
本身dll已经死了,释放也没有用了

我就是想找到一种能类似程序关闭又打开的方法
atlasroben 2008-07-17
  • 打赏
  • 举报
回复
看来楼主用的ocr呢
有没有释放的函数呢?应该有的吧,我调用汉王的ocr都有函数释放的
gomoku 2008-07-17
  • 打赏
  • 举报
回复
请问捕捉到异常后怎么处理,能使这个dll重新载入使函数能再次使用
No, you can't.
gomoku 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gomoku 的回复:]
引用 3 楼 liangzha 的回复:
释放也是dll本身的接口函数
本身dll已经死了,释放也没有用了

我就是想找到一种能类似程序关闭又打开的方法

If you have to, try creating an AppDomain and then run the P/Invoke within that domain.
You have to unload the whole domain to free loaded dlls.
[/Quote]

上述的尝试并不能成功 - 刚才的试验表明本地dll并没有随着AppDomain卸载而卸载。
我也不建议显式用LoadLibrary,GetProcAddress和FreeLibrary。

回过头来,你的问题在Dll方面,掩盖问题会使以后的开发和维护变得更难。
要在项目开始时选择好的第三方模块,而不要在开发和维护过程中不断擦别人的屁股。

liangzha 2008-07-17
  • 打赏
  • 举报
回复
有谁能指点下把这段代码转换成appDomain的形式呢?

[DllImport("AdvOcr.dll", EntryPoint = "OCR_C", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]

protected static extern string OCR_C(string type, string fileName);


while(true){
try {
string result = OCR_C(OCR_type, filePath);

}
catch {

MessageBox.Show("出错了!");


}
}
liangzha 2008-07-17
  • 打赏
  • 举报
回复
i'm just a beginner and i started wrote code 3 days ago, can you tell me the way how to use AppDomain for those codes?
i've search the way in internet for long time, i'm still confusing, thanks.
gomoku 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 liangzha 的回复:]
释放也是dll本身的接口函数
本身dll已经死了,释放也没有用了

我就是想找到一种能类似程序关闭又打开的方法
[/Quote]

If you have to, try creating an AppDomain and then run the P/Invoke within that domain.
You have to unload the whole domain to free loaded dlls.

110,023

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧