teky 的 blog
在vck中学习,在vck中进步~
<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
公告

留言簿(11)

随笔档案

文章分类

文章档案

相册

Windows Mobile

我的好友

朋友创业,其路漫漫

搜索

最新评论

阅读排行榜

评论排行榜

 
VC知识库BLOG   首页  新随笔  联系  聚合  登录 
  随笔-30 文章-17 评论-94 Trackbacks-4

 

为了能用上原来的C++代码,只好研究下从C# 中调用DLL
首先必须要有一个声明,使用的是DllImport关键字:
包含DllImport所在的名字空间
using System.Runtime.InteropServices;
public class XXXX{

[DllImport(“MyDLL.dll")]
public static extern int mySum (int a,int b);

}


[DllImport(“MyDLL.dll")]
public static extern int mySum (int a,int b);
代码中DllImport关键字作用是告诉编译器入口点在哪里,并将打包函数捆绑在这个类中
在调用的时候
在类中的时候 直接   mySum(a,b);就可以了
在其他类中调用: XXXX. mySum(a,b);
 
[DllImport(“MyDLL.dll”)]在申明的时候还可以添加几个属性
[DllImport(“MyDLL.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)
]
EntryPoint: 指定要调用的 DLL 入口点。默认入口点名称是托管方法的名称 。
CharSet: 控制名称重整和封送 String 参数的方式 (默认是UNICODE)
CallingConvention指示入口点的函数调用约定(默认WINAPI)(上次报告讲过的)
SetLastError 指示被调用方在从属性化方法返回之前是否调用 SetLastError Win32 API 函数 (C#中默认false )


int 类型
[DllImport(“MyDLL.dll")]
//返回个int 类型
public static extern int mySum (int a1,int b1);
//DLL中申明
extern “C” __declspec(dllexport)  int WINAPI mySum(int a2,int b2)

//a2 b2不能改变a1 b1
//a2=..
//b2=...
 return a+b;
}

//参数传递int 类型
public static extern int mySum (ref int a1,ref int b1);
//DLL中申明
extern “C” __declspec(dllexport)  int WINAPI mySum(int *a2,int *b2)

//可以改变 a1, b1
*a2=...
*b2=...
 return a+b;
}


DLL 需传入char *类型
[DllImport(“MyDLL.dll")] 
//传入值
public static extern int mySum (string  astr1,string bstr1);
//DLL中申明
extern “C” __declspec(dllexport)  int WINAPI mySum(char * astr2,char * bstr2)
{
//改变astr2 bstr 2  ,astr1 bstr1不会被改变
 return a+b;
}


DLL 需传出char *类型
[DllImport(“MyDLL.dll")]
// 传出值
public static extern int mySum (StringBuilder abuf, StringBuilder bbuf );
//DLL中申明
extern “C” __declspec(dllexport)  int WINAPI mySum(char * astr,char * bstr)
{
//传出char * 改变astr bstr -->abuf, bbuf可以被改变
 return a+b;
}
 
DLL 回调函数

BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)



using System;
using System.Runtime.InteropServices;
public delegate bool CallBack(int hwnd, int lParam); //定义委托函数类型
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
public static void Main() {
CallBack myCallBack = new CallBack(EnumReportApp.Report); EnumWindows(myCallBack, 0);
}
public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd); return true;
}
}
 

DLL  传递结构 
BOOL PtInRect(const RECT *lprc, POINT pt);

using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Point {
 public int x;
public int y;
 }
[StructLayout(LayoutKind.Explicit)]
 public struct Rect
 {
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
 }
Class XXXX {
 [DllImport("User32.dll")]
public static extern bool PtInRect(ref  Rect r, Point p);
 }

DLL 回调函数,传递结构 想看的msdn里面都有专题介绍,看的我都是晕晕的:)

其他参考请搜索:

在C#程序设计中使用Win32类库
C#中调用C++托管Dll
如何在C#中加载自己编写的动态链接库

相关文章:Creating a P/Invoke Library


能用上DLL以后感觉还是很好的,原来的C++代码只要修改编译通过就可以了,
高兴没多久,发现.net2005居然可以用VB,VC开发智能设备项目,可以创建MFC智能设备项目
晕晕,难道可以直接用MFC来开发smartphone的程序了,赶紧看看,,,

 

posted on 2006-04-14 16:01 teky 阅读(7521) 评论(11)  编辑 收藏
Comments
  • # re: C# 中调用DLL
    晓寒
    Posted @ 2006-04-14 17:07
    呵呵,就是pInvoke,偶还写了个例子呢。呵呵。可以到偶blog看看。

    推荐你个好地方。http://www.pinvoke.net/
  • # re:晓寒
    teky
    Posted @ 2006-04-14 17:27
    好的,谢谢先~~
  • # 问一下, C#可以调用C#写的dll么?
    jzhang
    Posted @ 2006-04-17 09:04
    或者C#里有没有动态链接库这样的概念?
  • # to[jzhang]
    晓寒
    Posted @ 2006-04-17 09:25
    有!但是他们都是托管的,如果是托管的,就不分什么语言,都可以使用。但是这里我们说的c++动态库不是托管的代码。所以使用起来有点费劲,只是有点。
  • # 我看到的一些例子,C#里调用dll都很麻烦的
    jzhang
    Posted @ 2006-04-17 09:41
    不像C++的那样简洁阿。
  • # re: C# 中调用DLL
    晓寒
    Posted @ 2006-04-17 10:38
    如果是C#使用C#的话,会非常简洁。c#和c++相比,在操作xml和wmi上,C#使用起来太方便了,c++则不然。
  • # re: C# 中调用DLL
    路过
    Posted @ 2008-07-09 02:45
    不错的教程
  • # re: C# 中调用DLL
    zac
    Posted @ 2008-07-09 02:45
  • # re: C# 中调用DLL
    阿木
    Posted @ 2008-07-09 02:46
    是哦,用上DLL感觉好很多...
  • # re: C# 中调用DLL
    UP
    Posted @ 2008-07-09 02:47
    UP
  • # re: C# 中调用DLL
    甄进
    Posted @ 2008-09-07 05:56
    本人急须网页能调动态库程序,重金收买该代码.联系电话:13512062076/13114830827
标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]