(原文:http://blog.chinaunix.net/u/18297/showart_296028.html)
為了能用上原來(lái)的C++代碼,只好研究下從C# 中調用DLL
首先必須要有一個(gè)聲明,使用的是DllImport關(guān)鍵字:
包含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關(guān)鍵字作用是告訴編譯器入口點(diǎn)在哪里,并將打包函數捆綁在這個(gè)類(lèi)中
在調用的時(shí)候
在類(lèi)中的時(shí)候 直接 mySum(a,b);就可以了
在其他類(lèi)中調用: XXXX. mySum(a,b);
[DllImport(“MyDLL.dll”)]在申明的時(shí)候還可以添加幾個(gè)屬性
[DllImport(“MyDLL.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingC

文件:
PInvoke.rar
大小:
2790KB
下載:
下載
onvention.StdCall)
]
EntryPoint: 指定要調用的 DLL 入口點(diǎn)。默認入口點(diǎn)名稱(chēng)是托管方法的名稱(chēng) 。
CharSet: 控制名稱(chēng)重整和封送 String 參數的方式 (默認是UNICODE)
CallingConvention指示入口點(diǎn)的函數調用約定(默認WINAPI)(上次報告講過(guò)的)
SetLastError 指示被調用方在從屬性化方法返回之前是否調用 SetLastError Win32 API 函數 (C#中默認false )
int 類(lèi)型
[DllImport(“MyDLL.dll")]
//返回個(gè)int 類(lèi)型
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 類(lèi)型
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 *類(lèi)型
[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不會(huì )被改變
return a+b;
}
DLL 需傳出char *類(lèi)型
[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); //定義委托函數類(lèi)型
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 傳遞結構 (見(jiàn)代碼)
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里面都有專(zhuān)題介紹,看的我都是暈暈的:)
其他參考請搜索:
在C#程序設計中使用Win32類(lèi)庫
C#中調用C++托管Dll
如何在C#中加載自己編寫(xiě)的動(dòng)態(tài)鏈接庫
相關(guān)文章:Creating a P/Invoke Library
能用上DLL以后感覺(jué)還是很好的,原來(lái)的C++代碼只要修改編譯通過(guò)就可以了,
高興沒(méi)多久,發(fā)現.net2005居然可以用VB,VC開(kāi)發(fā)智能設備項目,可以創(chuàng )建MFC智能設備項目
暈暈,難道可以直接用MFC來(lái)開(kāi)發(fā)smartphone的程序了,趕緊看看,,,
聯(lián)系客服