读书人

C#中生成dll的有关问题

发布时间: 2012-02-03 22:02:47 作者: rapoo

C#中生成dll的问题
有一个多线程写文件的函数,想生成DLL,生成TransClass.dll后却报找不到入口,两个函数如下,请教一下哪里错了,谢谢:
//TransClass.cs
using System;
using System.Threading;
using System.Data;
using System.IO;
namespace DataServer
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class TransClass
{
private int[] EachFileSize;
private int[] FilePosition;
public TransClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public bool FileCopy(int ThreadCount,string FilePath,string NewFilePath)
{
int eachFileSize = (int)(File.OpenRead(FilePath).Length) / ThreadCount;
int LastFileSize = eachFileSize + (int)(File.OpenRead(FilePath).Length) % ThreadCount;
EachFileSize = new int[ThreadCount];
FilePosition = new int[ThreadCount];
for (int m = 0; m < ThreadCount - 1; m++)
{
EachFileSize[m] = eachFileSize;
}
EachFileSize[ThreadCount - 1] = LastFileSize;
for (int n = 0; n < ThreadCount; n++)
{
FilePosition[n] = n * eachFileSize;
}

Thread[] ThrArray = new Thread[ThreadCount];
for (int j = 0; j < ThreadCount; j++)
{
ThreadCopy threadcopy = new ThreadCopy();
threadcopy.FilePath = FilePath;
threadcopy.NewFilePath = NewFilePath;
threadcopy.ThreadIndex = j;
threadcopy.EachFileSize = EachFileSize;
threadcopy.FilePosition = FilePosition;
ThrArray[j] = new Thread(new ThreadStart(threadcopy.MutilThreadCopy));
ThrArray[j].Start();
}
return true;
}
}
}

//ThreadCopy.cs
using System;
using System.Data;
using System.IO;
namespace DataServer
{
/// <summary>
/// ThreadCopy 的摘要说明。
/// </summary>
public class ThreadCopy
{
public string FilePath,NewFilePath;
public int ThreadIndex;
public int[] EachFileSize;
public int[] FilePosition;
public ThreadCopy()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void MutilThreadCopy()
{
try
{
FileStream fs = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
FileStream nfs = new FileStream(NewFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
BinaryWriter bw = new BinaryWriter(nfs);

int ReadedLength;
byte[] buffer = new byte[EachFileSize[ThreadIndex]];
fs.Seek(FilePosition[ThreadIndex], SeekOrigin.Begin);
ReadedLength = br.Read(buffer, 0, EachFileSize[ThreadIndex]);
nfs.Seek(FilePosition[ThreadIndex], SeekOrigin.Begin);


bw.Write(buffer, 0, ReadedLength);
bw.Flush();
fs.Close();
nfs.Close();
br.Close();
bw.Close();
}
catch (Exception)
{
}
}
}
}

[解决办法]
只要是.net, 不管是用vb, c#, c++写的托管DLL, 在.net的其他project中调用, 都是添加引用, 就可以搞定。
你用的IDE环境VS2003或者VS2005中, 在project下, 都有一个Add reference这样一个类似文件夹的东西,你看里面, 都是直接引用.netframework的DLL, 你直接把你硬盘上的DLL添加近来就可以了。
不过如果你想用非托管的代码来调用托管代码写成的DLL, 我就爱莫能助。据我所知, 好像不行。
如果看不懂托管和非托管, 去看看.net基础。
[解决办法]
在非托管的语言里不能直接调用 ,C#写的dll和传统的dll有区别地!
[解决办法]
都说什么呢?

你是要C#/VB.NET的代码调用C#写的DLL,用不着DLLImport、COM。Add Reference (添加引用)到你的DLL,然后创建类事例,调用方法:

TransClass a = new TransClass();
a.FileCopy();

C#/.NET写的DLL不是Windows Native DLL格式的,不能用depends来看。
要看内容的话用ildasm。

如果你要用C++,需要用managed C++来掉.NET DLL函数。

读书人网 >C#

热点推荐