111,094
社区成员




function FuncA:Funcs;stdcall; //这个是dll 导出的函数 可以由C#调用的
//一个有 ABC 3个参数的委托? AB 是string类型的
callback= procedure(const A, B : PAnsiChar;C: Boolean );stdcall;
Funcs=record //C#里边的结构?
CL:procedure(p:callback);stdcall; //一个没有返回值的 参数是callback
S:functioin(const A,B:PAnsiChar):Boolean:stdcall;//一个返回bool有AB两个string参数的委托?
end;
[DllImport("H:\\API.dll", EntryPoint = "FuncA", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
internal static extern void FuncA(IntPtr intPtr);
public delegate void Callback(string a, string b, bool c);
public delegate void CLMethod(Callback cb);
public delegate bool SMethod(string a, string b);
static void APICallBack(string a, string b, bool c)
{
}
static void Main(string[] args)
{
try
{
Funcs funcs= new Funcs();
int len = Marshal.SizeOf(funcs);
IntPtr ptr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(funcs, ptr, true);
FuncA(ptr);
funcs= (Funcs )Marshal.PtrToStructure(ptr, typeof(Funcs ));
Marshal.FreeHGlobal(ptr);
funcs.CL(APICallBack);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct Funcs
{
public CLMethod CL;
public SMethod S;
}