使用C#程序启动Dll
我有一个 C# 表单应用程序...我创建了一个 Dll...现在我想使用该程序启动该 dll。我该怎么做?
#include <windows.h>
typedef int (*function1_ptr) ();
function1_ptr function1=NULL;
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
HMODULE myDll = LoadLibrary("Dll1.dll");
if(myDll!=NULL) {
function1 = (function1_ptr) GetProcAddress(myDll,"function1");
if(function1!=NULL)
function1();
else
exit(4);
FreeLibrary(myDll);
}
else
exit(6);
GetLastError();
return 0;
}
这是用于测试我的 dll 的代码...即 Dll1.dll ..function1
是 dll1.dll 中的函数...我可以用 C# 代码做类似的事情吗???
I have a C# form application...i created a Dll...now i want to launch that dll using this program. how do i do it?
#include <windows.h>
typedef int (*function1_ptr) ();
function1_ptr function1=NULL;
int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
HMODULE myDll = LoadLibrary("Dll1.dll");
if(myDll!=NULL) {
function1 = (function1_ptr) GetProcAddress(myDll,"function1");
if(function1!=NULL)
function1();
else
exit(4);
FreeLibrary(myDll);
}
else
exit(6);
GetLastError();
return 0;
}
This was the code used to test my dll...i.e Dll1.dll..function1
was the function within dll1.dll.....can i do something similar with the C# code???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
要执行代码示例的操作,请使用以下 C# 代码:
上面的示例是调用非基于 .NET 的 DLL 中的函数的 C# 程序。
下面的示例是一个 C# 程序,它调用基于 .NET 的 DLL 中的函数。
这两个例子中的任何一个是你想要的吗?
或者您是否尝试从示例代码中调用 DLL 中的 C# 函数?
To do what your code example does, use the following C# code:
The above example is a C# program that calls a function in non .NET based DLL.
The example below is a C# program that calls a function in a .NET based DLL.
Is any of the the 2 examples what you want?
Or are you trying to call a C# function in a DLL from your example code?
我猜你想使用DLL的功能?如果是这样,请创建对该 DLL 的引用并在 C# 表单应用程序中使用它。换句话说,为 DLL 中包含的应用程序逻辑创建“用户”界面。如果这没有意义,您应该了解如何添加对项目的引用。
I assume you want to use the functionality of the DLL? If so, create a reference to the DLL and consume it in your C# forms application. In other words, create a "user" interface for application logic contained in a DLL. If this does not make sense, you should look up how to add a reference to a project.
使您的 dll 可执行,然后使用诊断中的 Process 类:
http ://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
Make your dll executable and after that use the Process class from diagnostics:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
术语“启动”和“DLL”是有些不兼容的概念。操作系统启动的程序是具有定义的入口点的二进制文件:main 方法。 DLL 最好被视为具有多个 API 形式入口点的二进制文件。在这种情况下启动需要操作系统在这些多个入口点之间进行选择。
您是否尝试使用 DLL 中的特定对象?如果是这样,请尝试以下
现在您将能够使用 DLL 中的类型你的项目。
The terms launching and DLL are somewhat incompatible concepts. The operating system launches programs which are binaries that have a defined entry point: the main method. DLLs are better viewed as binaries which have multiple entry points in the form of APIs. Launching in this case would require the operating system to pick between these many entry points.
Were you trying to use a particular object from a DLL? If so then try the following
Now you will be able to use the types from the DLL within your project.
您可以对 exe 执行此操作:
如果您想将 dll 加载到进程中然后使用它,也可以使用 AppDomain 对象。
最后,您可以使用
Each 服务其自己的目的,我建议初学者在 msdn 上阅读所有这些内容。
You could do this for an exe:
You could also use the AppDomain object if you want to load a dll into your process and then consume it.
And finally you can use the
Each serves its own purpose and I would suggest to read up on all of them on msdn for starters.
添加 DLL 作为表单应用程序的引用。然后,您将能够从应用程序代码访问其中的名称空间和类。
Add the DLL as a reference to your form application. Then you'll be able to access the namespaces and classes in it from the application code.
在您的 C# 应用程序中,添加对您创建的程序集(DLL)的引用。您可以通过解决方案资源管理器窗口执行此操作 - 右键单击引用,然后说“添加引用...”并选择您的 DLL。
此时,您可以在 C# 表单类的顶部添加“
using YourDllNamespace;
”,并根据需要使用 DLL 中定义的类型。In your C# application, add a reference to the assembly you created (the DLL). You can do this through the solution explorer window - right click on references, and say "Add Reference..." and choose your DLL.
At that point, you can add "
using YourDllNamespace;
" at the top of your C# form's class, and use the types defined within the DLL as needed.您可以使用不同的方法,一种
是使用 DllImport 属性:
You can use different methods, one is
another is using a DllImport attribute:
您可以将该 dll 的引用添加到您的项目中。
要添加参考,请使用以下步骤:
You can
add reference
to that dll into your project.To add reference use following STEPS: