关于VB.net dll [英] In regards to VB.net dlls

查看:115
本文介绍了关于VB.net dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个普遍的问题,我想知道我是否要用另一种语言创建一个dll(例如c / c ++,就像那些我熟悉的那样),它运行一个无限循环并调用它从一个vb.net程序我将如何结束无限循环。为了澄清,我会按下我的vb程序上的一个按钮来运行这个c dll,并且在c dll中会调用一个函数来运行无限循环,而(1)让我们说,我的问题是如何vb程序停止c程序?





总的来说,最重要的是,我的问题是我可以在vb中创建一个按钮/代码来打开和关闭dll是按下 - 而不是让dll继续使用?





编辑:此外,如果有任何资源如何这样做,教程,演练等。我非常感谢他们的链接。我找到了一些东西,但是阅读时有点神秘,在很多情况下没有帮助。





相关信息:操作系统:Windows 7(64位),使用mingw编译器编写代码:: blocks for c / c ++,以及Visual Studio 2008中的vb.net





为了进一步阐述我的问题,我想要的是无限期地从文件中读取的ac程序(文件不断填充)和用于启动和停止进程的vb程序。此外,数据应该由数组从c dll发送到vb应用程序,以便vb应用程序也可以存储和使用数据。

I had a general question, I'm wondering if I were to create a dll in another language (such as c/c++ for example, as those are what I'm familiar with) that ran an infinite loop and call it from a vb.net program how would I go about ending that infinite loop. To clarify, I would press a button on my vb program that would run this c dll, and in the c dll a function would be called that ran off of an infinite loop, while(1) let's say, my question is how would the vb program stop the c program?


Overall, and most significantly, my question is can I make a button/code in vb that will open and close a dll when it is pressed--rather than having the dll stay in use?


Also, if there are any resources on how to do this, tutorials, walkthroughs, etc. I would highly appreciate a link to them. I've found some stuff, but it's a bit cryptic to read through and in many cases not helpful.


Relevant info: OS: Windows 7 (64 bit), programming in code::blocks with mingw compiler for c/c++, and vb.net in visual studio 2008


To further elaborate on my question, what I want is a c program to indefinitely read from a file (the file is constantly filling) and a vb program to start and stop the process. Also, the data is supposed to be sent by an array from the c dll to the vb application so that the vb application can store and use the data as well.

推荐答案

我知道如何操作的唯一方法是在单独的线程上运行该函数,然后在完成后终止该线程。



查找线程和在谷歌上互操作。在大多数情况下,.NET可以为您生成互操作程序集。



Interop In .NET [ ^ ]
The only way I know how to do it is to run the function on a separate thread, and then terminate the thread when you are done.

Look up threading and interop on google. .NET can generate interop assemblies for you in most cases.

Interop In .NET[^]


应该更详细地解释带有线程的场景:你必须使用像 System.Threading这样的非平凡工具。 Thread.Abort

http://msdn.microsoft.com/en-us/library/system.threading.thread.abort.aspx [ ^ ]。



与一些常见的人认为,它不会终止线程。它执行诸如异常播种之类的非平凡操作到目标线程中。例外是 System.Threading.ThreadAbortException ,你可以处理它来执行一些最后的清理操作,线程的最后遗嘱:

http://msdn.microsoft.com/en-us/library/system。 threading.threadabortexception.aspx [ ^ ]。



一般情况下,如果可能是危险的。只有当你非常了解自己在做什么时,才应该使用这些东西。但是:如果代码挂起,可能意味着它已经足够糟糕了,所以在这种情况下你只是没有更好的补救措施。



现在,关于装卸。这是一个复杂的问题,我建议不要进入,除非你对技术有非常好的理解。这种DLL有很大的不同。



如果这是一个本机(非托管)DLL,你通常通过P / Invoke使用它:

http://en.wikipedia.org/wiki/P/Invoke [ ^ ],

http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp [< a href =http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp\"target =_ blanktitle =New Window> ^ ]。



此CodeProject文章也可能有所帮助: http://www.codeproject .com / csharp / EssentialPInvoke.asp [ ^ ]。



如果在DLL中调用某些方法,则无法加载或卸载它。 (同样,你没有解释任何理由,所以我会警告你反对它。)如果你真的想加载和卸载它,你宁愿调用Windows API方法并使用它们:

http://msdn.microsoft.com/en -us / library / windows / desktop / ms684175%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop /ms683212%28v=vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683152%28v=vs.85%29.aspx [ ^ ]。



现在,关于.NET程序集。通常,您只需参考并使用它。但你可以加载或卸载它。惊喜:你不能从内存中卸载.NET程序集,因为它不安全,对内存管理系统来说是不可接受的 - 如果某些代码可以访问已经卸载的某些方法/属性会怎么样?代码?



只有在某个单独的Application Domain 中加载.NET程序集时,才能卸载它。在这种情况下,您可以做的是卸载整个Application Domain。但如果它在一个单独的应用程序域中,如何使用该程序集? 这很棘手。



如果您想了解更多细节,请参阅我过去对此主题的回答:

创建使用可重新加载插件的WPF应用程序...... [ ^ ],

AppDomain拒绝加载程序集 [ ^ ],

使用CodeDom生成代码 [ ^ ]。



-SA
The scenario with the thread should be explained in more detail: you have to use such an non-trivial tool as System.Threading.Thread.Abort:
http://msdn.microsoft.com/en-us/library/system.threading.thread.abort.aspx[^].

In contrast to some common believe, it does not terminate thread. It performs such a non-trivial operation as exception seeding into the target thread. The exception is System.Threading.ThreadAbortException, you can handle it to perform some last clean-up action, the "last will" of the thread:
http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx[^].

In general case, if can be dangerous. You should use such things only if you understand very well what you are doing. But: if the code is hanging, it may means that it is already bad enough, so you simply don't have a better remedy in this situation.

Now, about loading and unloading. This is a complex matter which I would advise not to get into, unless you have extremely good understanding of technology. The kind of DLL makes dramatic difference.

If this is a native (unmanaged) DLL, you normally use it via P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article could also be helpful: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

If you invoke some methods in you DLL, you won't be able to load or unload it. (Again, you did not explain any reasons for doing it, so I would warn you against it.) If you really want to load and unload it, you would rather invoke Windows API methods and use them:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684175%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684179%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683212%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683152%28v=vs.85%29.aspx[^].

Now, about a .NET assembly. Normally, you just reference and use it. But can you load or unload it. Surprise: you are not allowed to unload a .NET assembly from memory, as it would be unsafe, not acceptable for memory-managed system — what if some code keeps access to some methods/properties of already unloaded code?

You can only unload the .NET assembly if it was loaded in some separate Application Domain. In this case, what you could do is to unload the whole Application Domain. But how to use the assembly if it is in a separate Application Domain? This is tricky.

If you want further detail, please see my past answers on this topic:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^].

—SA


首先,你没有两个不同的程序。 .DLL代码加载到VB.NET应用程序的地址空间中。它成为你代码的一部分。



如果你在应用程序的启动线程上启动无限循环,那么循环将完全控制你的VB.NET代码根本没有任何东西可以运行,所以你无法停止循环。



解决这个问题的方法是在VB.NET中使用任何可用的线程方法,这样在线程中运行C方法或使用BackgroundWorker组件。这样做的问题是你必须修改C代码以寻找你的VB.NET代码设置的某种o bailout标志,以告诉它优雅地结束自己。不要使用Thread.Abort。这是杀死一个线程的非常混乱的方式,而不是优雅地关闭它。



谷歌用于VB.NET BackgroundWorker的大量例子。
First, you don't have two different programs. The .DLL code is loaded into the address space of your VB.NET app. It becomes parts of your code.

If you start the inifinite loop on the startup thread of the app, then the loop will have complete control and your VB.NET code will not get anychance at all to run, so you can't stop the loop.

The way around this is to use any available threading methods in VB.NET, such a running the C method in a Thread or using a BackgroundWorker component. The problem with this is that you would have to modify the C code to look for some kind o bailout flag your VB.NET code sets to tell it to end itself gracefully. Don't use Thread.Abort. That's the very messy way to kill a thread, not shut it down gracefully.

Google for "VB.NET BackgroundWorker" for lots of examples.


这篇关于关于VB.net dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆