calling winAPI functions in vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TLeq
    New Member
    • Jun 2016
    • 3

    calling winAPI functions in vb.net

    have to "translate" / up-date a program that was written in C about 20 years ago to VB.net due to its old SQL connection style that isn't compatible anymore. However, I have minimal experience with C and even less with the winAPI(which the C application uses)... I was wondering, can the same functions from the API be use in VB.net?

    I have been able to add a declaration like this:

    Code:
      <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
            Public Shared Function SetWindowText(hWnd As IntPtr, lpString As String) As Boolean
            End Function
    to get a window Handle as IntPtr ( in vb.net). However the C program uses functions such as:

    Code:
      BOOL NEAR GoModal( HINSTANCE hInstance, LPCSTR lpszTemplate,HWND hWnd, DLGPROC lpDlgProc, LPARAM lParam )
    By importing the user32.dll I can't get the hWnd handle but I can get the IntPtr ( If I understood well, is a point to a window as an integer right? Kinda similar to hWnd --> I could be TOTALLY wrong, but this is what I understood)

    As I go to the definition of each type (i.e: NEAR , HINSTANCE, LPCSTR, etc...) I try to find an equivalent call in VB but ... its useless. For instance, I've seen a lot of questions on google asking on how to get the HINSTANCE such as:

    Code:
    System.Runtime.InteropServices.Marshal.GetHINSTANCE _(System.Reflection.Assembly.GetExecutingAssembly.GetModules() _(0)).ToInt32()
    which does work in Visual Studio (no errors) BUT I need to create an object like this :

    Code:
     Dim hinst as HINSTANCE
    which is how the C code made it ( of course not with dim! haha)

    Ultimately, my question is, would such call exist in vb.net / even be useful with the more modern framework and such?
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2

    Code:
    SetWindowText(Process.GetCurrentProcess().MainWindowHandle, "Amazing!")

    Comment

    Working...