使用C#程序启动Dll

发布于 2024-10-15 08:58:51 字数 668 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

就此别过 2024-10-22 08:58:51

要执行代码示例的操作,请使用以下 C# 代码:

public static class DllHelper
{
    [System.Runtime.InteropServices.DllImport("Dll1.dll")]
    public static extern int function1();
}

private void buttonStart_Click(object sender, EventArgs e)
{
    try
    {
        DllHelper.function1();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}      

上面的示例是调用非基于 .NET 的 DLL 中的函数的 C# 程序。
下面的示例是一个 C# 程序,它调用基于 .NET 的 DLL 中的函数。

try
{
    System.Reflection.Assembly dll1 = System.Reflection.Assembly.LoadFile("Dll1.dll");
    if (dll1 != null)
    {
        object obj = dll1.CreateInstance("Function1Class");
        if (obj != null)
        {
            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("function1");
            mi.Invoke(obj, new object[0]);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

这两个例子中的任何一个是你想要的吗?
或者您是否尝试从示例代码中调用 DLL 中的 C# 函数?

To do what your code example does, use the following C# code:

public static class DllHelper
{
    [System.Runtime.InteropServices.DllImport("Dll1.dll")]
    public static extern int function1();
}

private void buttonStart_Click(object sender, EventArgs e)
{
    try
    {
        DllHelper.function1();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}      

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.

try
{
    System.Reflection.Assembly dll1 = System.Reflection.Assembly.LoadFile("Dll1.dll");
    if (dll1 != null)
    {
        object obj = dll1.CreateInstance("Function1Class");
        if (obj != null)
        {
            System.Reflection.MethodInfo mi = obj.GetType().GetMethod("function1");
            mi.Invoke(obj, new object[0]);
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

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?

或十年 2024-10-22 08:58:51

我猜你想使用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.

烧了回忆取暖 2024-10-22 08:58:51

使您的 dll 可执行,然后使用诊断中的 Process 类:

http ://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

Make your dll executable and after that use the Process class from diagnostics:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                // You can start any process, HelloWorld is a do-nothing example.
                myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.Start();
                // This code assumes the process you are starting will terminate itself. 
                // Given that is is started without a window so you cannot terminate it 
                // on the desktop, it must terminate itself or you can do it programmatically
                // from this application using the Kill method.
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
囚我心虐我身 2024-10-22 08:58:51

术语“启动”和“DLL”是有些不兼容的概念。操作系统启动的程序是具有定义的入口点的二进制文件:main 方法。 DLL 最好被视为具有多个 API 形式入口点的二进制文件。在这种情况下启动需要操作系统在这些多个入口点之间进行选择。

您是否尝试使用 DLL 中的特定对象?如果是这样,请尝试以下

  • 操作 右键单击​​“解决方案资源管理器”中的项目,然后选择“添加引用”
  • 选择“浏览”选项卡
  • 导航到有问题的 DLL 并单击“确定”

现在您将能够使用 DLL 中的类型你的项目。

MyOtherDLLNamespace.TheType local = ...

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

  • Right click on the project in "Solution Explorer" and select "Add Reference"
  • Choose the "Browse" Tab
  • Navigate to the DLL in question and hit OK

Now you will be able to use the types from the DLL within your project.

MyOtherDLLNamespace.TheType local = ...
花开雨落又逢春i 2024-10-22 08:58:51

您可以对 exe 执行此操作:

   Process.Start("yourProcess");

如果您想将 dll 加载到进程中然后使用它,也可以使用 AppDomain 对象。

最后,您可以使用

  Assembly.Load(...) 

Each 服务其自己的目的,我建议初学者在 msdn 上阅读所有这些内容。

You could do this for an exe:

   Process.Start("yourProcess");

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

  Assembly.Load(...) 

Each serves its own purpose and I would suggest to read up on all of them on msdn for starters.

时间你老了 2024-10-22 08:58:51

添加 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.

葬花如无物 2024-10-22 08:58:51

在您的 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.

音盲 2024-10-22 08:58:51

您可以使用不同的方法,一种

Assembly.Load 

是使用 DllImport 属性:

[DllImport("mylib.dll)]

You can use different methods, one is

Assembly.Load 

another is using a DllImport attribute:

[DllImport("mylib.dll)]
淑女气质 2024-10-22 08:58:51

您可以将该 dll 的引用添加到您的项目中。

要添加参考,请使用以下步骤:

1.转到项目菜单解决方案资源管理器
2.添加参考
3.浏览你的dll
4. 好的

You can add reference to that dll into your project.

To add reference use following STEPS:

1.Go to Project Menu or Solution Explorer
2. add reference
3. browse your dll
4. OK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文