zoukankan      html  css  js  c++  java
  • VS2008生成DLL文件的方法、引用dll文件以及意义

     一 VS2008生成dll文件的方法
        有两种方法:
        1:傻瓜式操作
        打开VS2008,依次点击:菜单->文件->新建项目->项目类型visual C#(这里假设为该项目所取的名字是DllBuild)->类库(注意必须是类库),即新建一个由纯.cs类库文件组成的程序集,写好代码之后(例如写了一个名为DllTest.cs的类,该类的namespace取名为DllTestNS),再依次点击:菜单->生成->生成DllBuild,这样你的DllBuild/DllBuild/bin/Debug文件夹或者DllBuild/DllBuild/obj/Debug文件夹里便会自动生成dll文件啦,该文件名称与项目名称一致,即为DllBuild.dll。
        2:使用VS命令行
        依次点击:开始->运行,输入cmd,在打开的命令行窗口中输入:cd  \,按回车,输入下面一行命令:
        cd c:\Program Files\Microsoft Visual Studio 8\SDK\v2.0>csc /target:library /out:d:\Pager.dll d:\Pager.cs
         按回车,这样,便将d:\Pager.cs 文件编译为dll文件并保存为d:\Pager.dll。
        在这里有可能会报错,原因是csc.exe文件找不到。此时只需打开资源浏览器explorer,在“我的电脑”中搜索“csc.exe”文件即可,比如我的csc.exe文件便是在:
    C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe。为了不至于每次编译dll时都要输入如此长的VS命令行路径,我们可以将该路径添加到系统环境变量中。具体的添加方法请见:http://hi.baidu.com/yuemingfeng/blog/item/f3bf3c24b86db46934a80fcc.html 。当添加完环境变量后,现在要将.cs文件编译为dll文件便十分方便:
        点击“开始”->“运行”,输入: csc  /target:library /out:d:\Pager.dll d:\Pager.cs
    这样便直接进行编译。
     
        二 dll文件的引用及动态加载
        2.1 引用dll文件
        c++文件必须有头文件和lib文件方能编译通过,在运行时还必须调用相应的dll文件;而c#则直接将头文件和lib文件都封装进dll文件中,因此,c#编程无需再引入这两个文件,但是在运行时或者编译时很多时候都需要引用dll文件。   
        在上一步,我们生成(Build)了名为DllBuild的项目,并生成了DllBuild.dll文件,现在我们重新新建一个模板类型为Console Application(控制台应用程序)的项目,名为DllInvoke,新建好项目之后,从资源浏览器中打开该项目,依次打开DllInvoke\DllInvoke\bin\Debug\,将刚才生成的DllBuild.dll文件复制到Debug目录下,同时打开DllInvoke\DllInvoke\DllInvoke.csproj文件(右击,用记事本打开),打开后内容如下:
    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProductVersion>8.0.50727</ProductVersion>
        <SchemaVersion>2.0</SchemaVersion>
        <ProjectGuid>{EE4DDE2F-AC60-4A50-A988-AB936EB00103}</ProjectGuid>
        <OutputType>Exe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>DllInvoke</RootNamespace>
        <AssemblyName>DllInvoke</AssemblyName>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\Release\</OutputPath>
        <DefineConstants>TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
      <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Data" />
        <Reference Include="System.Xml" />  </ItemGroup>
      <ItemGroup>
        <Compile Include="Program.cs" />
        <Compile Include="Properties\AssemblyInfo.cs" />
      </ItemGroup>
      <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
      <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
           Other similar extension points exist, see Microsoft.Common.targets.
      <Target Name="BeforeBuild">
      </Target>
      <Target Name="AfterBuild">
      </Target>
      -->
    </Project>
        可以看到,该文件主要描述了改项目的一些系统配置和属性,例如项目名称和根命名空间名称、调试方式等等。由于要引用dll文件,因此我们需要在该xml格式的文件中添加关于该dll文件的描述信息,添加到<ItemGroup>节点中(即上文字体颜色为绿色的地方),添加后,该处内容变为:
        <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Data" />
        <Reference Include="System.Xml" />
        <Reference Include="test, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
          <SpecificVersion>False</SpecificVersion>
          <HintPath>..\..\DllBuild.dll</HintPath>
        </Reference>  </ItemGroup> 
  • 相关阅读:
    Java 环境变量配置
    C# 怎样判断一个字符串的编码类型
    ASP.NET MVC Razor视图引擎基础语法
    Silverlight 访问外部程序
    Silverlight 换肤的实现
    Silverlight初始动画 加载动画
    Silverlight App中线程同步
    .net反射简介
    FLASH 中文显示乱码
    [原创]开源Word读写组件DocX,通过word模板,导出用户简历使用示例
  • 原文地址:https://www.cnblogs.com/pricks/p/1601829.html
Copyright © 2011-2022 走看看