C#中调用C生成的Dll 问题

zhouxm2003 2005-07-04 04:09:33
C#中调用C生成的Dll,总报程序发生异常错误..不懂为什么..
C的代码:
int Sum(int a,int b)
{
return a+b;
}
C#中调用的代码:
class Dll
{
[DllImport("EssDll.dll",EntryPoint="Sum",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int Sum(int a,int b);

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.Write("Dll测试开始\n");
Console.Write(Sum(5,6).ToString());
Console.Write("\n前面调用Dll完成!\n");
string ok = Console.ReadLine();
}
}

C生成Dll是用Cywin来生成的,命令如下:
EssDll:
gcc -g -c ./src/EssDll.c -I./include -L./lib
dllwrap -o EssDll.dll EssDll.o --def ./src/EssDll.def
mv ./EssDll.o ./obj/
mv ./EssDll.dll ./bin/

*.def文件如下:
LIBRARY EssDll
EXPORTS
Sum
...全文
245 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
iGray 2005-07-05
  • 打赏
  • 举报
回复
Console.Write(sum(5,6));
这样就可以了
iGray 2005-07-05
  • 打赏
  • 举报
回复
问题出来ToString,
注意了,Sum返回的是int,还不是System.Int32,
如果你要输出Sum的值,最好格式化一下
速马 2005-07-05
  • 打赏
  • 举报
回复
P/Invoke只支持stdcall
zhouxm2003 2005-07-05
  • 打赏
  • 举报
回复
出错的如下:

应用程序发生异常 未知的软件异常(0xc0000409),位置为 0x7c936310

不懂什么意思?

C#完整调用代码如下:
using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Dll
{
[DllImport("EssDll.dll",EntryPoint="Sum",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int Sum(int a,int b);

// public static int Sum(int a,int b)
// {
// return a+b;
// }
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.Write("Dll测试开始\n");
Console.Write(Sum(5,6).ToString());
Console.Write("\n前面调用Dll完成!\n");
string ok = Console.ReadLine();
}
}
}
iGray 2005-07-05
  • 打赏
  • 举报
回复
把异常代码全贴出来。

.NET的P/Invoke的调用机制很不错,正确使用时,不会有异常的。
zhouxm2003 2005-07-05
  • 打赏
  • 举报
回复
哪位有没有C下生成Dll,C#调用成功的例子....
zhouxm2003 2005-07-05
  • 打赏
  • 举报
回复
加了 using System.Runtime.InteropServices;
用VS.NET编译时没有出错,只是在运行时,程序去调用c的dll中函数时报错,说"出现异常错误"....

我是在windows xp 上用cywin来编辑c成dll的,不懂是不是这样不行? 因为没安装VC
iGray 2005-07-05
  • 打赏
  • 举报
回复
生成时报错??是指编译时就报错了?
贴出代码来。随便提一下,你加了using System.Runtime.InteropServices;没?
一般仅这样标一个属性就足够了[DllImport("EssDll.dll")]

给点建议:
1.不要用GCC编译dll,在linux下,函数的调用过程默认的不是__stdcall,编译dll时生成的代码的参数压栈方式与windows下不同。

2.跨系统使用模块时,最好用一下COM的方法。并不是指你一定要用COM规范来做dll,COM提供的一种二进制上的一致性,主要是通过接口来将实现隔离,《COM本质论》中把这些概念解释得很清楚。
iGray 2005-07-05
  • 打赏
  • 举报
回复
我已经测试过了,Console.Write(Sum(5,6).ToString());这样子调用也没有问题。程序运行正常。

说两点:
1.在生成dll时,我去掉了static关键字,不去掉,我用VC6编译通不过。至于这个原因,我还不清楚。
同时我在添加了__stdcall关键字,其实VC6编译时默认的就是以__stdcall。

2.在类中导入dll中的函数时,必须声明为static。就算dll中那个函数不是静态的也没有关系。

zhouxm2003 2005-07-05
  • 打赏
  • 举报
回复
都到底了,,顶下...
zhouxm2003 2005-07-05
  • 打赏
  • 举报
回复
我不打印,只是调用都会出错:

iGray(C#Online) 帮忙瞧瞧.

int ire;

Console.Write("Dll测试开始\n");
ire = Sum(5,6);
Console.Write("\n前面调用Dll完成!\n");
string ok = Console.ReadLine();
zhouxm2003 2005-07-05
  • 打赏
  • 举报
回复
我调用 Console.Write(Sum(5,6)); 仍然出错呢? 怪了..
shrinerain 2005-07-04
  • 打赏
  • 举报
回复
没用过c写dll,gz
star0796 2005-07-04
  • 打赏
  • 举报
回复
c也能写DLL真是好啊,小弟又长见识了!!
zhouxm2003 2005-07-04
  • 打赏
  • 举报
回复
以上是什么错误呢..高手们帮忙瞧瞧要怎么做
  • 打赏
  • 举报
回复
晕,不用C写DLL用什么写DLL,当然了C++,C#都可以写,不过最能体现动态联结本意的还是C写的DLL
lovebanyi 2005-07-04
  • 打赏
  • 举报
回复
不懂啊。历害啊。还用C写DLL
zhouxm2003 2005-07-04
  • 打赏
  • 举报
回复
没人,,自已顶下..

110,549

社区成员

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

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

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