I am trying to import the GetWindowText in a VB.NET application.
So based on other sites you just have to import the interopt service and add a DLLImport statement similar to C#. But somehow it is not recognizing the statement and getting a BC30001 (Statement is not valid in a namespace) compile error.
Here is the code I used.
Imports System.Runtime.InteropServices
<DllImport("user32", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetWindowText(ByVal hWnd As IntPtr, <Out(), MarshalAs(UnmanagedType.LPTStr)> ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function
What assembly do I need to import to make this work? Any ideas?
The problem has nothing to do with importing assemblies. You simply cannot define free functions in a namespace in VB.NET.
You have to place them in either a
Module(basically a static class) or aClass.It’s recommended you place native Win32 functions in a class named
NativeMethodsanyway, so rewrite your code to look like this: