.dll函数调用问题
我在开发中要用到一个别人提供的开发包,主要为一个.dll动态链接库,其中的调用函数我不知道在C#中应该如何改写,请高人指点一二,谢谢了!
function MC_SetFileName(PlayerID : integer; FileName:Pchar):integer;stdcall;
function MC_SetSN(RegStr : Pchar):integer;stdcall;
function MC_GetVU(PlayerID:integer;ChannelID:Byte) : Word; stdcall;
[最优解释]
integer -> int
Pchar -> StringBuilder
Byte -> byte
[其他解释]
你手头上肯定有C++开发人员开发的dllDemo.dll,比如这个dllDemo.dll就只有求和,求最大值,求最小值的DLL,他们已经为你打包好了,那么下面举个例子,如何调用这个DLL.
/在c#中调用。新建,项目,控制台应用程序。(注意是C#,不是C#环境您重新切一下),
//在Program.cs中贴入以下代码:
//by 孔苏群 2012-11-29
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;//引入dll文件中的函数
class Program
{ //引入dll文件中的函数
[DllImport("dllDemo.dll")]
private static extern int Sum(int a, int b);
// private static extern int MC_SetFileName(int PlayerID , StringBuilder FileName)
[DllImport("dllDemo.dll")]
private static extern int Max(int a, int b);
// 知道怎么改了吧
[DllImport("dllDemo.dll")]
private static extern int Min(int a, int b);
static void Main(string[] args)
{
int a = Sum(3, 5);
Console.WriteLine(a);
Console.WriteLine(Max(5, 10));
Console.WriteLine(Min(12, 25));
Console.ReadKey();//直接main里边最后加个Console.Read()不就好了。要按键才退出。 }
}
}
#define COM4 4
#define COM5 5
#define COM6 6
#define COM7 7
#define COM8 8
#define COM9 9
返回:
如果该函数调用成功,返回一个零值。
否则,返回非零值请查看其他返回值定义,返回的错误代码请查看错误代码定义。