c#intptr未得到处理
我正在用C#创建一个客户端,通过一些示例连接到bopup。作为windows应用程序运行时,句柄的值为0,而作为控制台应用程序运行时,句柄的值大于0。要在窗体中获取主窗口句柄,是否需要执行一些不同的操作 控制台:c#intptr未得到处理,c#,intptr,C#,Intptr,我正在用C#创建一个客户端,通过一些示例连接到bopup。作为windows应用程序运行时,句柄的值为0,而作为控制台应用程序运行时,句柄的值大于0。要在窗体中获取主窗口句柄,是否需要执行一些不同的操作 控制台: static void Main(string[] args) { string error = null; uint refResult = 0; CSCLIENTLib.ServerClientVB client = new
static void Main(string[] args)
{
string error = null;
uint refResult = 0;
CSCLIENTLib.ServerClientVB client = new CSCLIENTLib.ServerClientVB();
try
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
string server = "localhost";
// Console.WriteLine(server + "|" + 0 + "|" + (byte)VBClientType.Messenger + "|" + (uint)handle + "|" + refResult);
client.Initialize(server, 0, (byte)VBClientType.Messenger, (uint)handle, ref refResult);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
client.GetEventDescription(refResult, ref error);
Console.WriteLine(error);
}
}
作为windows窗体应用程序:
public partial class Form1 : Form
{
string error = null;
uint refResult = 0;
private static CSCLIENTLib.ServerClientVB client;
IntPtr handle;
public Form1()
{
InitializeComponent();
init_connection();
}
private void init_connection()
{
client = new CSCLIENTLib.ServerClientVB();
try
{
handle = Process.GetCurrentProcess().MainWindowHandle;
string server = "localhost";
MessageBox.Show(server + "|" + 0 + "|" + (byte)VBClientType.Messenger + "|" + (uint)handle + "|" + refResult);
client.Initialize(server, 0, (byte)VBClientType.Messenger, (uint)handle, ref refResult);
}
catch (Exception ex)
{
// Console.WriteLine(ex.ToString());
MessageBox.Show("ERROR: " + ex.ToString());
client.GetEventDescription(refResult, ref error);
// Console.WriteLine(error);
MessageBox.Show("ERROR: " + error.ToString());
}
}
}
如果需要当前窗体的句柄,请使用handle属性(this.handle)
在调用窗体的Show()方法之前,GUI应用程序没有窗口。因此init_connection()运行得太快,最早的时间是HandleCreated事件。加载事件在它之后运行。HandleCreated也是您需要发现表单具有不同句柄的事件,您需要知道,因为COM组件无法再正常工作。
handle = this.Handle;