By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,334 Members | 2,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,334 IT Pros & Developers. It's quick & easy.

calling winAPI functions in vb.net

P: 3
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:

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

Expand|Select|Wrap|Line Numbers
  1.   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:

Expand|Select|Wrap|Line Numbers
  1. 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 :

Expand|Select|Wrap|Line Numbers
  1.  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?
Jun 3 '16 #1
Share this Question
Share on Google+
1 Reply


iam_clint
Expert 100+
P: 1,208
http://www.pinvoke.net/default.aspx/....setwindowtext
Expand|Select|Wrap|Line Numbers
  1. SetWindowText(Process.GetCurrentProcess().MainWindowHandle, "Amazing!")
  2.  
Jun 6 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.