纽曼来电通二次开发,遇到问题,求高手来解答
大家好!小弟第一次发帖,第一次接触C#,原先搞JAVA开发,低下两个C#的类文件代码:
namespace WindowsApplication1{ public delegate void LDTCallBack(int port); class LDT { [DllImport("Sandu.dll", EntryPoint = "Setup_Tel")] public static extern void Setup_Tel(LDTCallBack CallBack, int y); [DllImport("Sandu.dll", EntryPoint = "Begin_Tel")] public static extern int Begin_Tel(int Port, char Power); [DllImport("Sandu.dll", EntryPoint = "End_Tel")] public static extern int End_Tel(int Port); [DllImport("Sandu.dll", EntryPoint = "GetNumber_Tel")] public static extern StringBuilder GetNumber_Tel(int Port); [DllImport("Sandu.dll", EntryPoint = "DevCount_Tel")] public static extern int DevCount_Tel();}}
public delegate bool CallBack(int hwnd, int lParam); public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void MyCallBack(int port) { if (port == 0) { //设备有改变 } else { string number; number = LDT.GetNumber_Tel(port).ToString(); listBox1.Items.Add("端口"+port+": 来电号码 "+number); } } private void btnSetup_Click(object sender, EventArgs e) { //设置回调 LDTCallBack callback = new LDTCallBack(MyCallBack); LDT.Setup_Tel(callback, 1); listBox1.Items.Add("设置回调成功"); } private void button1_Click(object sender, EventArgs e) { //打开设备 for (int i = 1; i <= LDT.DevCount_Tel(); i++) { if (LDT.Begin_Tel(i, '1') == 1) { listBox1.Items.Add("开启设备" + i.ToString() + "成功"); } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { // return; try { for (int i = 1; i <= LDT.DevCount_Tel(); i++) { LDT.End_Tel(i); } } catch { } } private void button1_Click_1(object sender, EventArgs e) { frmEnum frm = new frmEnum(); frm.ShowDialog(); frm.Close(); } private void Form1_Load(object sender, EventArgs e) { } }
void TestCallBack2(FPTR2 pf2, char* value);
C#:
public delegate bool FPtr( int value );
public delegate bool FPtr2( String value );
public class LibWrap
{// Declares managed prototypes for unmanaged functions.
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern void TestCallBack( FPtr cb, int value );
[ DllImport( "..\\LIB\\PinvokeLib.dll" )]
public static extern void TestCallBack2( FPtr2 cb2, String value );
}
public class App
{
public static void Main()
{
FPtr cb = new FPtr( App.DoSomething );
LibWrap.TestCallBack( cb, 99 );
FPtr2 cb2 = new FPtr2( App.DoSomething2 );
LibWrap.TestCallBack2( cb2, "abc" );
}
public static bool DoSomething( int value )
{
Console.WriteLine( "\nCallback called with param: {0}", value );
…
}
public static bool DoSomething2( String value )
{
Console.WriteLine( "\nCallback called with param: {0}", value );
…
}
}