2012-02-17 37 views
1

我想在VB.NET應用程序中導入GetWindowText。什麼彙編你需要在VB.NET中導入DLL庫

因此,基於其他站點,您只需導入interopt服務並添加類似於C#的DLLImport語句。但不知何故,它不會識別該語句並獲得一個BC30001(語句在命名空間中無效)編譯錯誤。

這是我使用的代碼。

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 

我需要導入什麼樣的組件才能使其工作?有任何想法嗎?

回答

2

該問題與導入程序集無關。您無法在VB.NET的命名空間中定義自由函數。

您必須將它們放置在Module(基本上是靜態類)或Class

我們建議您將本機Win32函數在一個名爲NativeMethods反正類,因此重寫代碼看起來像這樣:

Imports System.Runtime.InteropServices 

Friend Class NativeMethods 

    <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 

End Class 
+0

我覺得我裏克·佩裏。糟糕... – Nap 2012-02-17 04:10:39

相關問題