为什么Openicon在VB.NET 2010 Express中不起作用? [英] Why will Openicon not work in VB.NET 2010 express?
问题描述
我以前使用过此代码,但是在VS2010 Express中不起作用.
它会编译,但随后在调用时会给出堆栈错误:
{对PInvoke函数"WindowsApplication1!WindowsApplication1.Form1 :: OpenIcon"的调用已使堆栈不平衡.这可能是因为托管PInvoke签名与非托管目标签名不匹配.检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配.}
它在openicon和setforegroundwindow上崩溃.
我使用的路径是正确的.
私有 声明 功能 OpenIcon 库 " ( ByVal hWnd As 长) class ="code-keyword"> As 长
私有 声明 功能 FindWindow 库 " " FindWindowA"( ByVal lpClassName As 字符串, ByVal lpWindowName As 字符串) As 长
私有 声明 功能 SetForegroundWindow 库 " ( As 长
Dim hWnd As 长
hWnd = FindWindow(vbNullString," )
OpenIcon(hWnd)
SetForegroundWindow(hWnd)
' SendKeys等
<之所以这么说是因为您使用的是为VB6编写的声明(Declare语句). VB6的Long类型是32位带符号整数,而在VB.NET中则是64位带符号整数.此代码仅在VB6中不能在任何版本的VB.NET中起作用.
VB.NET中正确的参数类型是IntPtr,而不是Long.
Hi,
I have used this code before, however it does not work in VS2010 Express.
It compiles, but then on call, it gives a stack error:
{A call to PInvoke function ''WindowsApplication1!WindowsApplication1.Form1::OpenIcon'' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.}
It crashes on openicon and setforegroundwindow.
The path I used is correct.
Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Dim hWnd As Long
hWnd = FindWindow(vbNullString, "C:\newfolder\RFDRIVERSIMULATION.EXE")
OpenIcon (hWnd)
SetForegroundWindow (hWnd)
' SendKeys etc
It says this because you''re using a declaration (Declare statement) written for VB6. VB6''s Long type is a 32-bit signed Integer, whereas in VB.NET it''s a 64-bit signed Integer. This code would not have worked in any version of VB.NET, only VB6.
The correct parameter type in VB.NET is IntPtr, not Long.
这篇关于为什么Openicon在VB.NET 2010 Express中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!