例子:using System; using System.Runtime.InteropServices; namespace Beep { class Class1 { [DllImport("kernel32.dll")] public static extern bool Beep(int frequency, int duration); static void Main(string[] args) { Random random = new Random(); for (int i = 0; i < 10000; i++) { Beep(random.Next(10000), 100); } } } }
准确一点 [DllImport ("Advapi32.dll")] public extern static UInt32 ReadPortl(byte[] pData,uint iLen)
也可以做成COM,自动注册后,直接引用一下就行了,挺方便的.
一 创建DLL: 1,在头文件中,将要导出的函数前面加上extern "C" _declspec(dllexport), 只要是要导出的函数都要加。 2,在实现文件中,不必加extern "C" _declspec(dllexport)。 3,编译生成dll文件。在头文件中不加extern "C" _declspec(dllexport)不会生成.lib文件。 4,在使用该dll的工程中,将dll的头文件(加上extern "C" _declspec(dllexport)的)拷贝到工程目录下。 5,在使用该dll的工程中,添加文件:*dll.lib,也可以把.lib文件考过来再添加。 6,可以使用了。二 将类导出: 1,头文件中: class AFX_EXT_CLASS TxtBayes { public: double * base; double * key_value; double * diff_f; int end_tag; double * mul_value; BAYES * bayes; int kind_num; int pre_num; int read_file_line; ChangeStruct * spe_struct; public: TxtBayes(); ~TxtBayes(); void Study(char * path); void GetStudy(); int GetResult(char * path,int tag); int GetTest(int a, int b); }; 2,.cpp文件中不用改变。 3,生成*.dll文件。三 生成COM 1,新建项目,选择c++项目,选择ATL项目,点击确定。 2,选择解决方案,右击,选择添加类。 3,选择ATL简单对象。 4,定义类名。 5,选择类窗口,右击接口选项。选择添加-〉添加方法。 6,常用参数类型,一般数据类型,返回时可用指针,字符串用BSTR。但要按一下方式作一些处理,才可正常使用。 STDMETHODIMP Cmatrixcom::strtest(BSTR str) { CComBSTR sMsg; sMsg.AssignBSTR(str); CW2CT szMsg(sMsg); cout<<"this is strtest:"<<szMsg<<endl; return S_OK; } 7,生成COM,自动注册。四 使用COM(例如:C#) 1, 新建项目,右击引用,选择添加引用,选择com,双击你要用的COM。 2, 可按一下方式使用: matrix_com.CmatrixcomClass matrix = new matrix_com.CmatrixcomClass(); int i=0; matrix.add(3,ref i); matrix.strtest("I am chensongfeng"); Console.WriteLine("i is {0}",i);
using System.Runtime.InteropServices; namespace Beep
{
class Class1
{
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration); static void Main(string[] args)
{
Random random = new Random(); for (int i = 0; i < 10000; i++)
{
Beep(random.Next(10000), 100);
}
}
}
}
[DllImport ("Advapi32.dll")]
public extern static UInt32 ReadPortl(byte[] pData,uint iLen)
1,在头文件中,将要导出的函数前面加上extern "C" _declspec(dllexport),
只要是要导出的函数都要加。
2,在实现文件中,不必加extern "C" _declspec(dllexport)。
3,编译生成dll文件。在头文件中不加extern "C" _declspec(dllexport)不会生成.lib文件。
4,在使用该dll的工程中,将dll的头文件(加上extern "C" _declspec(dllexport)的)拷贝到工程目录下。
5,在使用该dll的工程中,添加文件:*dll.lib,也可以把.lib文件考过来再添加。
6,可以使用了。二 将类导出:
1,头文件中:
class AFX_EXT_CLASS TxtBayes
{
public:
double * base;
double * key_value;
double * diff_f;
int end_tag;
double * mul_value;
BAYES * bayes;
int kind_num;
int pre_num;
int read_file_line;
ChangeStruct * spe_struct;
public:
TxtBayes();
~TxtBayes();
void Study(char * path);
void GetStudy();
int GetResult(char * path,int tag);
int GetTest(int a, int b);
};
2,.cpp文件中不用改变。
3,生成*.dll文件。三 生成COM
1,新建项目,选择c++项目,选择ATL项目,点击确定。
2,选择解决方案,右击,选择添加类。
3,选择ATL简单对象。
4,定义类名。
5,选择类窗口,右击接口选项。选择添加-〉添加方法。
6,常用参数类型,一般数据类型,返回时可用指针,字符串用BSTR。但要按一下方式作一些处理,才可正常使用。
STDMETHODIMP Cmatrixcom::strtest(BSTR str)
{
CComBSTR sMsg;
sMsg.AssignBSTR(str);
CW2CT szMsg(sMsg);
cout<<"this is strtest:"<<szMsg<<endl;
return S_OK;
}
7,生成COM,自动注册。四 使用COM(例如:C#)
1, 新建项目,右击引用,选择添加引用,选择com,双击你要用的COM。
2, 可按一下方式使用:
matrix_com.CmatrixcomClass matrix = new matrix_com.CmatrixcomClass();
int i=0;
matrix.add(3,ref i);
matrix.strtest("I am chensongfeng");
Console.WriteLine("i is {0}",i);
请查看《.NET高级特性》这本书。其中对非托管代码说的很清了
public extern static UInt32 ReadPortl([MarshalAs(UnmanagedType.LPArry, SizeParamIndex = 1)] byte[] pData, uint iLen);MarshalAs的具体用法,请参见msdn得MarshalAsAttribute.SizeParamIndex