c#动态调用c++编写的dll

sunmoonfly 2008-03-25 04:55:41
我用c++编写了一个动态链接库,其中一个函数有6个参数,后面3个位返回值,
我首先采用原始的方法进行声明调用,也就是
[DllImport(@"test.dll")]
public static extern int test(string ProjName,string SchemeName,string Mile,ref double ContnMile,ref bool Reliability,ref short LineKind);
能成功返回需要的答案,但是这属于静态的方式调用,后来需求变更后,需要采用动态的方式进行调用,在网上搜集了一些资料其中
http://blog.csdn.net/pansiom/archive/2006/01/01/568096.aspx
介绍一种方式,我在机器上按照博主的方法调试,博主上面给出的例子是调用是成功的,但是我使用自己的dll进行测试,总是在最后返回的时候失败,也就是return MyMethodInfo.Invoke(null, ObjArray_Parameter);
不知道大家有没有动态调用c++ dll的好方法?




...全文
423 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunmoonfly 2008-03-31
  • 打赏
  • 举报
回复
暂时准备改良下c++dll,对DLL内部需要改进的地方进行优化,避免并发调用出现那种计算出错的问题。
我们现在调用的DLL有很多不是我写的,数量有点多,改起来会花很多时间。
对于你的那个采用组件的方式进行处理,我这几天一直在调试,非常感谢你的帮忙。
shinaterry 2008-03-31
  • 打赏
  • 举报
回复
^ō^ 请问LZ最后是采用什么方式解决呢?
sunmoonfly 2008-03-31
  • 打赏
  • 举报
回复
谢谢大家的关注和帮助
问题没有得到解决,shinaterry 所说的采用组件的方式这几天一直在尝试,不是很理想
KKND2006 2008-03-28
  • 打赏
  • 举报
回复
做COM是不错的选择
mine 2008-03-28
  • 打赏
  • 举报
回复
学习下
wxg22526451 2008-03-28
  • 打赏
  • 举报
回复
友情up
wyc_xiaoben 2008-03-28
  • 打赏
  • 举报
回复
mark 学习
shinaterry 2008-03-27
  • 打赏
  • 举报
回复
^ō^ 或者你将 C++.dll 发布 com 组件, 然后 C# 中直接引用该组件..
sunmoonfly 2008-03-27
  • 打赏
  • 举报
回复
是这样的,这个dll刚开始采用DllImport的方式进行调用,但是由于这个是放在web进行调用的,当客户端软件进行访问web里面的函数时,只要客户端软件不退出,这个dll就会一直占用服务器的内存,当很多客户端同时调用这个dll种的函数,就会出问题,导致函数计算不正确
所以要动态的调用,然后dll里面的函数计算完毕后马上释放,和mfc里面动态调用dll一样
shinaterry 2008-03-26
  • 打赏
  • 举报
回复
暂没办法, 唯有使用:

[
DllImport(@"test.dll")
]
public static extern int test(string ProjName, string SchemeName, string Mile, ref double ContnMile, ref bool Reliability, ref short LineKind);
ericzhangbo1982111 2008-03-26
  • 打赏
  • 举报
回复
Assembly assem = Assembly.LoadFrom(@"\program files\IOManager\IOManager.dll");
//.CreateInstance("ClassLibrary1.Class1");

Type type = assem.GetType("com.FujitsuBSC.MobileUnity.BizMobile.IOManager.DBProcessor");


Type t1 = typeof(string);
Type t2 = typeof(string);
ConstructorInfo info = type.GetConstructor(new Type[] { t1, t2 });//构造函数
object o = info.Invoke(new object[] { "SDFTest.sdf", "123" });//调用构造函数

string sql = "insert into Test values(?,?,?)";
object[] o1 = new object[] { "4", "aaaaa", DateTime.Now.ToString() };

object r = type.GetMethod("Update").Invoke(o, new object[] { sql, o1 });
fengyecsdn 2008-03-26
  • 打赏
  • 举报
回复
MARK 已经研究一下 学习 接分
chengqscjh 2008-03-26
  • 打赏
  • 举报
回复
mark and up
sunmoonfly 2008-03-26
  • 打赏
  • 举报
回复
谢谢大家帮忙,我这用的.net 版本是1.1,这个动态库是在webservice端进行调用

shinaterry :谢谢,你的这个例子我看过
Marshal.GetDelegateForFunctionPointe,Marshal.GetDelegateForFunctionPointer这个函数是2.0新增的功能,1.1不支持。
KKND2006 2008-03-26
  • 打赏
  • 举报
回复
你不会想把这个函数放到C#类的实例里面去调用吧?

C++的内存可是和C#的内存管理完全不一样的
YFY 2008-03-25
  • 打赏
  • 举报
回复
看下楼主解决了没。
shinaterry 2008-03-25
  • 打赏
  • 举报
回复
[
DllImport("Kernel32.dll")
]
public static extern int LoadLibrary(String funcname);

[
DllImport("Kernel32.dll")
]
public static extern int GetProcAddress(int handle, String funcname);

[
DllImport("Kernel32.dll")
]
public static extern int FreeLibrary(int handle);

//
// 注意: 参数列表及返回值要与方法(test)对应..
//
public delegate int Invoker(string ProjName, string SchemeName, string Mile, ref double ContnMile, ref bool Reliability, ref short LineKind);

static void Main(string[] args)
{
int handle = LoadLibrary(@"test.dll"); //要调用的类库..

if (handle != 0)
{
int address = GetProcAddress(handle, "test"); //指定函数名称..

if (address != 0)
{
Invoker invoker = (Invoker)Marshal.GetDelegateForFunctionPointer(new IntPtr(address), typeof(Invoker));

//use invoker -> demo: invoker(...);

FreeLibrary(handle);
}
}
}
shrinerain 2008-03-25
  • 打赏
  • 举报
回复
你可以用C++/CLI做桥梁, 因为它既可以方便调用传统C++代码, 又可以很方便的与C#做互操作.
GiornoLuna 2008-03-25
  • 打赏
  • 举报
回复
试过用一些API函数,在进程间传数据~好像不支持中文~
liqngjun123 2008-03-25
  • 打赏
  • 举报
回复
http://blog.csdn.net/hailongchang/archive/2006/12/12/1439502.aspx
加载更多回复(1)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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