问题描述
类模块:PublicClassMyEnumWindowsPrivateStructureWindowsinfoPublicTitlesAsStringPublicHwndsAsLongPublicPIDAsLongEndStructurePrivateDelegateFunctionCallBackPor(ByValhwndAsLong,ByVallParamAsLong)AsBooleanPrivateDeclareFunctionEnumWindowsLib"user32"(ByVallpEnumFuncAsCallBackPor,ByVallParamAsLong)AsLongPrivateDeclareFunctionGetWindowTextLib"user32"Alias"GetWindowTextA"(ByValhwndAsLong,ByVallpStringAsString,ByValcchAsLong)AsLongPrivateDeclareFunctionGetWindowThreadProcessIdLib"user32"(ByValhwndAsLong,ByVallpdwProcessIdAsLong)AsLongPrivateDeclareFunctionGetWindowTextLengthLib"user32"Alias"GetWindowTextLengthA"(ByValhwndAsIntPtr)AsIntegerPrivateWindows()AsWindowsinfoPrivateWindowsNumAsLongPrivateFunctionMyEnumWindowLoad(ByValhwndAsLong,ByVallParamAsLong)AsBooleanDimstrTitleAsString=Space(GetWindowTextLength(hwnd)+1)ReDimPreserveWindows(WindowsNum)Windows(WindowsNum).Hwnds=hwndGetWindowText(hwnd,strTitle,strTitle.Length)Windows(WindowsNum).Titles=strTitleGetWindowThreadProcessId(hwnd,Windows(WindowsNum).PID)WindowsNum=WindowsNum+1MyEnumWindowLoad=TrueEndFunctionPublicSubNew()CallEnumWindows(AddressOfMyEnumWindowLoad,0)EndSub
运行到18行时提示:对PInvoke函数“WindowsApplication1!WindowsApplication1.MyEnumWindows::GetWindowText”的调用导致堆栈不对称。原因可能是托管的PInvoke签名与非托管的目标签名不匹配。请检查PInvoke签名的调用约定和参数与非托管的目标签名是否匹配。
解决方案
解决方案二:
string的长度(strTitle.Length)是整型的,不是长整型的。而你定义GetWindowText函数中的第三个参数需要的是长整型的,所以出错。
解决方案三:
你这完全是VB6的代码,除了class和new。hwnd是个指针,是IntPtr类型;lpdwProcessId是指向进程ID的指针,应该声明为ByReflpdwProcessIdAsUInteger;cch指明缓冲区大小,是Integer类型。详细的定义请参考pinvoke.net
解决方案四:
本人刚从6.0转向.net。。所以代码中有许多6.0的痕迹。。大家见谅啊。。欢迎大家指出来