c# 中调用dll文件

xhmbldsh 2007-08-03 11:46:28
在c#中调用动态库,请问如何实现同一个动态库可以多次调用,且每次调用的dll都能够同时保留在内存中,且互不影响?
...全文
282 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuyiazl 2007-08-04
  • 打赏
  • 举报
回复
托管还是非托管,抑或是com?

重命名DLL???
qieyj 2007-08-04
  • 打赏
  • 举报
回复

System.Reflection.Assembly myAssembly;
myAssembly = System.Reflection.Assembly.Load("<程序集名>");

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new
System.Resources.ResourceManager("<资源的命名空间>.<资源根名>",
myAssembly);

// Retrieves String and Image resources.
System.String myString;
System.Drawing.Image myImage;
myString = myManager.GetString("<字符串资源名>");
myImage = (System.Drawing.Image)myManager.GetObject("<图像资源名>");


qieyj 2007-08-04
  • 打赏
  • 举报
回复
技术实现
  下面看看如何逐步实现动态库的加载,类型的匹配,动态链接库函数导出的定义,这个不需要多说,大家参考下面宏定义即可:

#define LIBEXPORT_API extern "C" __declspec(dllexport)
第一步,我先从简单的调用出发,定义了一个简单的函数,该函数仅仅实现一个整数加法求和:

LIBEXPORT_API int mySum(int a,int b){ return a+b;}
C# 导入定义:

public class RefComm
{
[DllImport("LibEncrypt.dll",
    EntryPoint=" mySum ",
    CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
    public static extern int mySum (int a,int b);
}
在C#中调用测试:

int iSum = RefComm.mySum(2,3);
运行查看结果iSum为5,调用正确。第一步试验完成,说明在C#中能够调用自定义的动态链接库函数。

第二步,我定义了字符串操作的函数(简单起见,还是采用前面的函数名),返回结果为字符串:

LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a); return a;}
C# 导入定义:

public class RefComm
{
[DllImport("LibEncrypt.dll",
     EntryPoint=" mySum ",
     CharSet=CharSet.Auto,
     CallingConvention=CallingConvention.StdCall)]
     public static extern string mySum (string a, string b);
}
在C#中调用测试:

string strDest="";
string strTmp= RefComm.mySum("12345", strDest);
运行查看结果 strTmp 为"12345",但是strDest为空。我修改动态链接库实现,返回结果为串b:

LIBEXPORT_API char *mySum(char *a,char *b){sprintf(b,"%s",a) return b;}
修改 C# 导入定义,将串b修改为ref方式:

public class RefComm
{
[DllImport("LibEncrypt.dll",
     EntryPoint=" mySum ",
     CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
     public static extern string mySum (string a, ref string b);
}
在C#中再调用测试:

string strDest="";
string strTmp= RefComm.mySum("12345", ref strDest);
  运行查看结果 strTmp 和 strDest 均不对,含不可见字符。再修改 C# 导入定义,将CharSet从Auto修改为Ansi:

public class RefComm
{
[DllImport("LibEncrypt.dll",
     EntryPoint=" mySum ",
     CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
     public static extern string mySum (string a, string b);
}
在C#中再调用测试:

string strDest="";
string strTmp= RefComm. mySum("12345", ref strDest);
  运行查看结果 strTmp 为"12345",但是串 strDest 没有赋值。第二步实现函数返回串,但是在函数出口参数中没能进行输出。再次修改 C# 导入定义,将串b修改为引用(ref):

public class RefComm
{
[DllImport("LibEncrypt.dll",
     EntryPoint=" mySum ",
     CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
     public static extern string mySum (string a, ref string b);
}
运行时调用失败,不能继续执行。

第三步,修改动态链接库实现,将b修改为双重指针:

LIBEXPORT_API char *mySum(char *a,char **b){sprintf((*b),"%s",a); return *b;}
C#导入定义:

public class RefComm
{
[DllImport("LibEncrypt.dll",
     EntryPoint=" mySum ",
     CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
     public static extern string mySum (string a, ref string b);
}
在C#中调用测试:

string strDest="";
string strTmp= RefComm. mySum("12345", ref strDest);
  运行查看结果 strTmp 和 strDest 均为"12345",调用正确。第三步实现了函数出口参数正确输出结果。

第四步,修改动态链接库实现,实现整数参数的输出:

LIBEXPORT_API int mySum(int a,int b,int *c){ *c=a+b; return *c;}
C#导入的定义:

public class RefComm
{
[DllImport("LibEncrypt.dll",
     EntryPoint=" mySum ",
     CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall)]
     public static extern int mySum (int a, int b,ref int c);
}
在C#中调用测试:

int c=0;
int iSum= RefComm. mySum(2,3, ref c);
运行查看结果iSum 和c均为5,调用正确。
  经过以上几个步骤的试验,基本掌握了如何定义动态库函数以及如何在 C# 定义导入,有此基础,很快我实现了变长加密函数在 C# 中的调用,至此目标实现。

三、结论
  在 C# 中调用 C++ 编写的动态链接库函数,如果需要出口参数输出,则需要使用指针,对于字符串,则需要使用双重指针,对于 C# 的导入定义,则需要使用引用(ref)定义。
  对于函数返回值,C# 导入定义和 C++ 动态库函数声明定义需要保持一致,否则会出现函数调用失败。定义导入时,一定注意 CharSet 和 CallingConvention 参数,否则导致调用失败或结果异常。运行时,动态链接库放在 C# 程序的目录下即可,我这里是一个 C# 的动态链接库,两个动态链接库就在同一个目录下运行。
Avoid 2007-08-04
  • 打赏
  • 举报
回复
使用单实例(单件)
honey52570 2007-08-04
  • 打赏
  • 举报
回复
mark
xhmbldsh 2007-08-04
  • 打赏
  • 举报
回复
就是用c#开发的用来完成某一项功能的一个dll文件,每次调用时,开启一个或多个线程运行,
Avoid 2007-08-03
  • 打赏
  • 举报
回复
什么dll?

托管还是非托管,抑或是com?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧