One of the arguments in C + + 's callback function is to return a string, as follows:
typedef void (*tdataevent) (char *adata, int ALen);
Where Char *adata returns a string from the DLL, and the memory of the string is already allocated in the DLL
The delegate that I defined in C # below
public delegate void Tdataevent (byte[] adata, int ALen);
The following is the setup code for the callback function:
Event=new clreceiveldllpoxy.tdataevent (getDate);
Recedllpoxy.addserver (1024,event,2);
Where the event is an instance of the delegate above, I define it as a member so that I do not release myself.
Here is the implementation of the callback function in C #
public void getDate(byte[] AData,int ALen)
{
//发现每次回调是 AData只有一个字节
}
Reproduced below a code of others, thank you
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Reflection.Emit;
namespace Appdlltest
{
/**////<summary>
Unmanaged dynamic invocation of the base class, which cannot be used directly, requires the implementation of the virtual function of funtable ()
///</summary>
public abstract class Cldllbasepoxy
{
//-Mount DLL---------
public bool Open (string dllfilename)
{
Hand = LoadLibrary (Dllfilename);
if (Hand = 0)
{
return false;
}
Funset (Getfuntable ());
return true;
}
//-Close DLL---
public bool Close ()
{
return FreeLibrary (Hand)!=0;
}
public abstract string[] getfuntable (); The function table is set by the external proxy class through getfuntable
//calls the Mount function in the KERNELE32 library under Windows32 to complete a reference to an unmanaged DLL-------#region// Call the Mount function in the KERNELE32 library under Windows32 to complete the reference to the unmanaged DLL-------
//--------------------------------------------------------------
[DllImport ("Kernel32")]
private static extern int GetProcAddress (int handle, String funcname);
[DllImport ("Kernel32")]
private static extern int LoadLibrary (String funcname);
[DllImport ("Kernel32")]
private static extern int FreeLibrary (int handle);
private int Hand = 0; Handle of DLL
private static Delegate getaddress (int dllmodule, string functionname, Type t)///convert pointer to C # proxy
{
int addr = GetProcAddress (Dllmodule, functionname);
if (addr = 0)
{
return null;
}
Else
{
return Marshal.getdelegateforfunctionpointer (new IntPtr (addr), T);
}
}
//--Association Agents and functions in DLLs-----------
private bool Funset (string[] afun)
{
Type TP = this. GetType ();
string[] Value;
for (int i = 0; i < afun.length; i++)
{
Value = Afun[i]. Split (', '); "Box, Tbox, _box" The first item is the instance name of the proxy, the second is the definition name of the proxy, and the third is the function name in the DLL
if (value.length = 3)
{
FieldInfo fi = TP. GetField (Value[0]. Trim ()); Find an instance
Type type = TP. GetNestedType (Value[1]. Trim ());/Find Hulto
if (fi!= null && type!= null)
{
fi. SetValue (This, getaddress (Hand, value[2). Trim (), type)); Create Association
}
}
}
return true;
}
#endregion
}
public class Cldllpoxy:cldllbasepoxy
{
public override string[] Getfuntable ()
{
string[] funtable = new string[]{
"Getfixparamcount, Tgetfixparamcount, _getfixparamcount",
"Getfixparam, Tgetfixparam, _getfixparam"
};
return funtable;
}
//--output function----------------------------------------------
public Tgetfixparamcount Getfixparamcount;
public Tgetfixparam Getfixparam;
//--Agent Description----------------------------------------------
public delegate int Tgetfixparamcount (); Gets the number of fixed parameters
public delegate bool Tgetfixparam (int aindex, byte []aoutbuf); Fixed parameter
}
/**////<summary>
C # dynamically invokes the base class of a managed DLL--------------
///</summary>
public class Clnetdllpoxy
{
//--load Dynamic library----------
public bool Open (string dllfilename, string className)
{
fasembly = Assembly.LoadFrom (dllfilename);
if (fasembly = null)
{
return false;
}
Type type = Fasembly.gettypes () [0]; The first corresponding namespace is called the name space
fdllname = dllfilename;
fclassname = className;
if (type!= null)
{
fnamespace = type. Namespace;
}
return true;
}
//--set the range of functions in the DLL---------
public void Setarea (string nameSpace, string className)
{
Fnamespace = NameSpace;
fclassname = className;
}
//--invokes the specified method, note: All parameters of the method are converted to the object type
public Object Invoke (String funname, object[] objarray_parameter)
{
Try
{
type[] types = Fasembly.gettypes ();
foreach (Type tp in types)
{
if (TP). Namespace = = Fnamespace && TP. Name = = fclassname)
{
MethodInfo metinfo = TP. GetMethod (Funname);
if (metinfo!= null)
{
Object obj = activator.createinstance (TP); Create Class object
if (obj!= null)
{
return Metinfo.invoke (obj, objarray_parameter);
}
}
}
}
}
Catch
{ }
return null;
}
private Assembly fasembly; DLL's Assembly
private String Fdllname;
private String Fnamespace;
private String Fclassname;
}
}