跪求牛人帮忙用C# 调用C的dll 本帖最后由 caitlin_yu 于 2010-11-11 15:15:48 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 声明:本人C语言不太好,以下建议不保证完全正确首先引用该dll,[DllImport("kernel32.dll")]调用代码:double number=kernel32.durationCompute(参数1(类型:short int),参数2(类型:int)); [DllImport("你的dll", CallingConvention = CallingConvention.StdCall)]public static unsafe extern float durationCompute(short** frameMat,int frameCount);不是很懂,大概是这样 [DllImport("你的dll", CallingConvention = CallingConvention.StdCall)]public static unsafe extern float durationCompute(IntPtr frameMat,int frameCount); 以前研究过一下,不过也怎么清楚,这是我的代码,也许能帮上你1:打开vs2008,新建一个类库,名为Cmdll(C++.net语言)2:在Cmdll.h中输入以下代码:extern "C" __declspec(dllexport) int SampleMethod(int i){ return i*10;}3:编译一下,在项目的文件夹里会生成Cmdll.dll文件。这个就可以在c#中调用了。c#调用如下using System;using System.Runtime.InteropServices;namespace ConsoleApplication1{ class Program { [DllImport("Cmdll.dll", EntryPoint = "SampleMethod")] public static extern int SampleMethod(int x); static void Main(string[] args) { Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5)); Console.Read(); } }} extern "C" __declspec(dllexport) 的位置好像不能变。也记不清了,楼主你自己试试吧。别忘了把Cmdll.dll文件拷到c#的项目文件夹bin里,至于用不用添加引用就计不清了,你自己试试吧 [DllImport("Cmdll.dll", EntryPoint = "SampleMethod")] public static extern int SampleMethod(int x);这个是你在c#里的函数名字,你直接调用就相当于调用c的函数了吗。用一个对应类型的变量接收不就得了 你把API原函数贴出来吧。参考:http://www.cnblogs.com/yufb/archive/2010/06/02/1749837.html看看参数转换 楼主可参考这个SDK,里面有C#调用C++写的DLL的源代码,实现了一个简单的语音、视频通话的平台,估计有用:http://www.anychat.cn/faq/,在SDK包的:src\client\c#目录下。 大哥里面有好多SDK,你指的是哪个? C#引用导出函数的参数,我还是引不出来,是不是到处方法不对?那参数要怎么导出,我这**frameMat是双指针呀,怎么整啊~~大虾帮帮忙吧 百度一下调用win32使用ok. 如果是一个标准的C导出Dll,那么需要用DllImport导入函数,参考互操作内容实际调用类似于这样的代码 public class MPwdMngAPI { [DllImport("PwdMng.dll", CharSet = CharSet.Ansi, EntryPoint = "SetUserFirstLogin")] public extern static bool ResetUserToFirstUse(String username); [DllImport("PwdMng.dll", CharSet = CharSet.Ansi, EntryPoint = "ChangePassword")] public extern static int ChangePassword(String userName, String oldPwd, String newPwd); }要单独写个类 把那个DLL添加在Dbug里打包的时候也别忘了 我编程菜鸟一个dll里有这样一个导出函数,int volumeLevel(signed short **frameMat,const int frameCount)//能量等级检测{ int i; int averageVol=0; //用于保存能量向量 int *volume=(int*)malloc(frameCount*sizeof(int)); volumeCompute(frameMat,volume,frameCount); for(i=0;i<frameCount;i++) averageVol += volume[i]; averageVol /= frameCount; if(averageVol > 6000000) return 3; else if(averageVol > 500000) return 2; else if(averageVol > 50000) return 1; else return 0;}在C#中我怎样得到返回值呢?frameMat, frameCount这两个参数要怎么弄呢??拜托高人指点~~ [System.Runtime.InteropServices.DllImportAttribute("你的DLL", EntryPoint="durationCompute")]public static extern float durationCompute(ref System.IntPtr frameMat, int frameCount) ;不知道对不对了 使用C#编写的程式的窗体如何汉化呢? 高分求一校验码的生成,涉及到字符串转化、进制转化 c#如何实现点击FORM1上的BUTTON1弹出FORM2来而且同时隐藏FORM2上的PANEL1 高手帮忙!整数相乘问题 菜鸟求救, word中插入图片的问题! 请问:如何在一个窗体中去激发另一个窗体中的事件?? 请教在程序需要循环处理数据,用线程好还是用Timer好,个人感觉线程控制太麻烦 C# Winform DataGridView绑定数据源的类型 关于全局和局部ID问题 c#调用c++DLL的动态链接库问题? TCP.UDP.Socket.P2P问题
调用代码:
double number=kernel32.durationCompute(参数1(类型:short int),参数2(类型:int));
public static unsafe extern float durationCompute(short** frameMat,int frameCount);
不是很懂,大概是这样
public static unsafe extern float durationCompute(IntPtr frameMat,int frameCount);
1:打开vs2008,新建一个类库,名为Cmdll(C++.net语言)
2:在Cmdll.h中输入以下代码:
extern "C" __declspec(dllexport) int SampleMethod(int i)
{
return i*10;
}3:编译一下,在项目的文件夹里会生成Cmdll.dll文件。这个就可以在c#中调用了。
c#调用如下
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[DllImport("Cmdll.dll", EntryPoint = "SampleMethod")]
public static extern int SampleMethod(int x);
static void Main(string[] args)
{
Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
Console.Read();
}
}
}
的位置好像不能变。也记不清了,楼主你自己试试吧。
别忘了把Cmdll.dll文件拷到c#的项目文件夹bin里,至于用不用添加引用就计不清了,你自己试试吧
public static extern int SampleMethod(int x);
这个是你在c#里的函数名字,你直接调用就相当于调用c的函数了吗。用一个对应类型的变量接收不就得了
参考:http://www.cnblogs.com/yufb/archive/2010/06/02/1749837.html看看参数转换
大哥里面有好多SDK,你指的是哪个?
C#引用导出函数的参数,我还是引不出来,是不是到处方法不对?那参数要怎么导出,我这**frameMat是双指针呀,怎么整啊~~大虾帮帮忙吧
ok.
{
[DllImport("PwdMng.dll", CharSet = CharSet.Ansi, EntryPoint = "SetUserFirstLogin")]
public extern static bool ResetUserToFirstUse(String username); [DllImport("PwdMng.dll", CharSet = CharSet.Ansi, EntryPoint = "ChangePassword")]
public extern static int ChangePassword(String userName, String oldPwd, String newPwd);
}要单独写个类
把那个DLL添加在Dbug里
打包的时候也别忘了
我编程菜鸟一个
dll里有这样一个导出函数,
int volumeLevel(signed short **frameMat,const int frameCount)//能量等级检测
{
int i;
int averageVol=0;
//用于保存能量向量
int *volume=(int*)malloc(frameCount*sizeof(int));
volumeCompute(frameMat,volume,frameCount);
for(i=0;i<frameCount;i++)
averageVol += volume[i];
averageVol /= frameCount;
if(averageVol > 6000000)
return 3;
else if(averageVol > 500000)
return 2;
else if(averageVol > 50000)
return 1;
else
return 0;
}
在C#中我怎样得到返回值呢?frameMat, frameCount这两个参数要怎么弄呢??拜托高人指点~~
public static extern float durationCompute(ref System.IntPtr frameMat, int frameCount) ;
不知道对不对了