2011-02-02 82 views
2

我有一个C#表单应用程序...我创建了一个dll ...现在我想用该程序启动该dll。我该怎么做?使用C#程序启动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#代码类似的东西???

+4

你叫什么“启动”一个DLL?一个DLL是类的容器。在dll中没有入口点。 – 2011-02-02 16:43:08

+0

使用此程序调用DLL ....为了查看DLL是否工作...我曾尝试调用DLL的代码...这帮助我找出DLL工作 – 2011-02-02 16:48:41

回答

3

做你的代码示例确实,使用下面的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); 
    } 
}  

上面的例子是一个C#程序,在非基于.NET的DLL调用一个函数。 下面的示例是一个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); 
} 

是你想要的2个例子中的任何一个? 或者你是否试图在示例代码中调用DLL中的C#函数?

0

你可以为一个EXE做到这一点:

Process.Start("yourProcess"); 

你也可以,如果你想要一个dll加载到进程,然后使用它使用AppDomain对象。

最后,你可以使用

Assembly.Load(...) 

每个提供它自己的目的,我会建议在MSDN上所有的人都读了对于初学者。

1

我假设你想使用DLL的功能?如果是这样,请创建一个对DLL的引用并在C#表单应用程序中使用它。换句话说,为包含在DLL中的应用程序逻辑创建一个“用户”界面。如果这没有意义,那么您应该查看如何添加对项目的引用。

0

将DLL添加为您的表单应用程序的引用。然后,您将能够从应用程序代码中访问其中的命名空间和类。

0

在您的C#应用​​程序中,添加对您创建的程序集(DLL)的引用。您可以通过解决方案资源管理器窗口执行此操作 - 右键单击​​引用,然后说“添加引用...”并选择您的DLL。

此时,您可以在C#窗体类的顶部添加“using YourDllNamespace;”,并根据需要使用DLL中定义的类型。

0

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

Assembly.Load 

另一个是使用DllImport属性:

[DllImport("mylib.dll)] 
1

让您的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); 
      } 
0

您可以将add reference添加到您的项目中。

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

1. 进入工程菜单Solution Explorer中
2.添加引用
3.浏览您的DLL
4。确定

1

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

你是否试图使用DLL中的特定对象?如果是这样,然后尝试在“解决方案资源管理器”项目的下列

  • 点击右键,选择“添加引用”
  • 选择“浏览”选项卡
  • 导航到有问题的DLL,点击OK

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

MyOtherDLLNamespace.TheType local = ...