vs2008如何生成dll文件

http://apps.hi.baidu.com/share/detail/31840426


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> 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值