Results 1 to 30 of 30

Thread: API Newbie

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    API Newbie

    Ok, I've read all over in the FAQ's etc about API's but i dont understand what they do.
    Is there a basic turtorial to get one of these running?

    This is what i wanted to do:

    Make a program in VB, that interacts with a terminal emulated program.
    I wanted to be able to just type in a "Order number" hit "go or search" and it does all the functions like i was in the terminal program it self.
    I wont be updating records etc, i just want the information out of the info.
    Here is a screen shot.


  2. #2

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    kk anyone?

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: API Newbie

    You can execute a hidden shell and stream the output.

    I used to do that with ping, it worked ok. I'll see if I can scrape together an example.

  4. #4
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: API Newbie

    I think this is what sevehalo meant...maybe:

    VB Code:
    1. Dim path As String = "C:\Program Files\Java\jdk1.5.0_05\bin"
    2.         Dim p As Process
    3.         p = New Process
    4.         p.StartInfo.CreateNoWindow = True
    5.         p.StartInfo.WorkingDirectory = path
    6.         p.StartInfo.Arguments = "HelloWorld"
    7.         p.StartInfo.FileName = "java.exe"
    8.         p.StartInfo.UseShellExecute = False
    9.         p.StartInfo.RedirectStandardOutput = True
    10.  
    11.         p.Start()
    12.         p.WaitForExit()
    13.         Dim io As String = p.StandardOutput.ReadToEnd()

    Note: HelloWorld is a java program - filename HelloWorld.class

    Whatever the output that shows up in the console is the string you end up with from the line: Dim io As String = p.StandardOutput.ReadToEnd()

    If this isn't what you meant, then just ignore it.

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Does anyone have an basic exmample of API used to get text from Note pad and display that text into a text box in the VB application?

    Ive been trying to figure this out for days now. I tried the examples above but cant figure it out.

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: API Newbie

    this gets the text from an open notepad(make sure there are some text on the notepad so you can see a result)

    VB Code:
    1. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    2.  
    3.  
    4.     Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
    5.  
    6.     Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    7.  
    8.     Public Const WM_GETTEXT = &HD
    9.  
    10.     Private Sub proc()
    11.         Dim hwnd As Integer
    12.         Dim hwndEx As Integer
    13.  
    14.         'gets handle of notepad
    15.         hwnd = FindWindow("Notepad", vbNullString)
    16.  
    17.         'gets handle of 'edit' area of notepad
    18.         hwndEx = FindWindowEx(hwnd, vbNullString, "edit", vbNullString)
    19.         Dim len As Integer = 255
    20.         Dim buffer As String    'buffer will contain the text from notepad
    21.         buffer = Space(len)
    22.        
    23.         'sends WM_GETTEXT message to 'edit' area of notepad
    24.         Dim x As Integer = SendMessage(hwndEx, WM_GETTEXT, len, buffer)
    25.         buffer = buffer.Substring(0, x)  
    26.  
    27.     End Sub
    Last edited by benmartin101; Dec 7th, 2005 at 04:00 PM.

  7. #7

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    when i try that code i get these erros:


  8. #8
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: API Newbie

    that code is meant for vb.net. You are obviously using vb 6.0. To fix:

    change all integers to 'long' type
    On situation with Dim x as integer = 1; change to:
    Dim x as integer
    x = 1
    And instead of using WM_GETTEXT, just substitute the value 13.

    vb6 doesn't have .substring, you have to figure out how to do some string manipulation

  9. #9

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Quote Originally Posted by benmartin101
    that code is meant for vb.net. You are obviously using vb 6.0. To fix:

    change all integers to 'long' type
    On situation with Dim x as integer = 1; change to:
    Dim x as integer
    x = 1
    And instead of using WM_GETTEXT, just substitute the value 13.

    vb6 doesn't have .substring, you have to figure out how to do some string manipulation
    I changed all that but i get errors:
    Public const 13 = &HD
    buffer = Space(len)
    x = SendMessage(hwndEx, WM_GETTEXT, len, buffer)

    **
    Does anyone have a VB 6.0 basic api call to notepad to get the text and put it in a text box in the VB app?

    Last edited by joefox; Dec 7th, 2005 at 06:37 PM.

  10. #10
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: API Newbie

    You misunderstood me on the 13 part. I meant, instead of using variablle WM_GETTEXT in one of the parameters, just put value 13.

    make buffer = space(len) to buffer = " " make it 255 blank spaces.

  11. #11

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Ok i hope i did this right, here is what i have, atleast it builds, but when i hit the button it dosent do anything":

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    5. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Long
    6.  
    7.     Public Sub proc()
    8.         Dim hwnd As Long
    9.         Dim hwndEx As Long
    10.  
    11.         'gets handle of notepad
    12.         hwnd = FindWindow("Notepad", vbNullString)
    13.  
    14.         'gets handle of 'edit' area of notepad
    15.         hwndEx = FindWindowEx(hwnd, vbNullString, "edit", vbNullString)
    16.         Dim x As Integer
    17.         x = 1
    18.         Dim buffer As String    'buffer will contain the text from notepad
    19.         buffer = "                                                       "
    20.        
    21.         'sends WM_GETTEXT message to 'edit' area of notepad
    22.         Dim x As Integer
    23.         x = SendMessage(hwndEx, 13, buffer)
    24.         buffer = buffer.Substring(0, x)
    25.         Text1.Text = buffer
    26.  
    27.     End Sub

  12. #12

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    anyone?

  13. #13
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: API Newbie

    I'm not sure if you're even looking at the sample code. Your sendmessage() only has 3 parameters, when the sample code shows 4.

  14. #14

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    I am looking at it, i dont know what values to pass into there.
    That is why i posted in these forums.

    I guess a simple API to notepad is turning into a nightmare.

  15. #15
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: API Newbie

    'You cant have public declares or public constants in form code

    'You cant dim a var and assign it a value all at once
    'len is a reserved word cant dim it as a var (all .NET stuff)
    'buffer.Substring(0, x) more .NET
    'In hwndEx = FindWindowEx(hwnd, vbNullString, "edit", vbNullString) you are passing the second param as a string, its looking for an Integer
    'also placing the 'buffer' in the wrong param of sendmessage


    So,

    VB Code:
    1. Option Explicit
    2. Private Const WM_GETTEXT          As Long = &HD
    3. Private Const WM_GETTEXTLENGTH    As Long = &HE
    4.  
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    6.                                                                      ByVal lpWindowName As String) As Integer
    7. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, _
    8.                                                                          ByVal hWnd2 As Integer, _
    9.                                                                          ByVal lpsz1 As String, _
    10.                                                                          ByVal lpsz2 As String) As Integer
    11. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, _
    12.                                                                        ByVal wMsg As Integer, _
    13.                                                                        ByVal wParam As Integer, _
    14.                                                                        ByVal lParam As String) As Integer
    15. Private Sub Form_Load()
    16.  
    17. Dim hwnd       As Integer
    18. Dim hwndEx     As Integer
    19. Dim intlen     As Integer
    20. Dim buffer     As String
    21.  
    22.     hwnd = FindWindow("Notepad", vbNullString)
    23.     'gets handle of 'edit' area of notepad
    24.     hwndEx = FindWindowEx(hwnd, 0, "edit", vbNullString)
    25.  
    26.     intlen = SendMessage(hwndEx, WM_GETTEXTLENGTH, 0, vbNullString)
    27.    
    28.     buffer = Space$(intlen)
    29.    
    30.     SendMessage hwndEx, WM_GETTEXT, intlen, ByVal buffer
    31.        
    32.     MsgBox buffer
    33.    
    34. End Sub

  16. #16

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Thanks for your help.
    Ok i tried running the code, but it only puts blank values into the msgbox, and when i changed the code to put it into the text1 field, it just puts blank spots, and i have text in notepad.

    VB Code:
    1. Option Explicit
    2. Private Const WM_GETTEXT          As Long = &HD
    3. Private Const WM_GETTEXTLENGTH    As Long = &HE
    4.  
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
    6.                                                                      ByVal lpWindowName As String) As Integer
    7. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, _
    8.                                                                          ByVal hWnd2 As Integer, _
    9.                                                                          ByVal lpsz1 As String, _
    10.                                                                          ByVal lpsz2 As String) As Integer
    11. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, _
    12.                                                                        ByVal wMsg As Integer, _
    13.                                                                        ByVal wParam As Integer, _
    14.                                                                        ByVal lParam As String) As Integer
    15. Private Sub Command1_Click()
    16.  
    17. Dim hwnd       As Integer
    18. Dim hwndEx     As Integer
    19. Dim intlen     As Integer
    20. Dim buffer     As String
    21.  
    22.     hwnd = FindWindow("Notepad", vbNullString)
    23.     'gets handle of 'edit' area of notepad
    24.     hwndEx = FindWindowEx(hwnd, 0, "edit", vbNullString)
    25.  
    26.     intlen = SendMessage(hwndEx, WM_GETTEXTLENGTH, 0, vbNullString)
    27.    
    28.     buffer = Space$(intlen)
    29.    
    30.     SendMessage hwndEx, WM_GETTEXT, intlen, ByVal buffer
    31.        
    32.     Text1.Text = buffer
    33.    
    34.  
    35. End Sub

  17. #17
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: API Newbie

    works for me.

  18. #18

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Quote Originally Posted by Jmacp
    works for me.
    Weird, i can see it copies the text from Notepad, but they are just spaces when they are displayed in my VB app.

    Do i need to include an references to anything?

    Right now, i typed Hellocheckme on Notepad, I hit the button, and it puts however many spaces HelloCheckme is into the text field.

  19. #19
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: API Newbie

    Beats me then, just tried it again no probs, did you cut and copy the code ?

  20. #20

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Yeah, i copy and cut the whole code, and added a textbox and button.

    Its weird, it seems to work, but just puts blank spots in there. It does the correct amount of spots, just no text lol.
    I tried to copy the blank spots out of the textbox and past them into work, and they are all blank spots.

  21. #21

    Re: API Newbie

    here, i think this might help


    VB Code:
    1. 'declerations:
    2. Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    3. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Declare Function SendMessageByNum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
    5. Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    6.  
    7.  
    8. Private Sub Command1_Click()
    9. Dim notepad&, notepadedit&, num&, buff$, edittxt&
    10.  
    11. '--find the right windows--
    12. notepad& = FindWindow("Notepad", vbNullString)
    13. notepadedit& = FindWindowEx(notepad&, 0&, "Edit", vbNullString)
    14. '--
    15. '--create a buffer the length of the string that needs to be loaded
    16. num& = SendMessageByNum(notepadedit&, 14, 0, 0&)
    17. buff$ = Space$(num&)
    18. '--
    19. '--actually retrieve the text-
    20. edittxt& = SendMessageByString(notepadedit&, 13, num& + 1, buff$)
    21.  
    22.  
    23. Text1.text = buff$
    24. End Sub
    Last edited by si_the_geek; Dec 13th, 2005 at 12:08 PM. Reason: added VBCode tags

  22. #22

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    OMG yes that worked, so what did we do different?

  23. #23

    Re: API Newbie

    i just declared the variables corretly really, but it was easier to write the code over than edit the other one. i added sendmessagebynum and sendmessagebystring, just because i use them to remember my different uses of the SendMessageA function in user32.dll. sendmessage, sendmessagebynum,and sendmessagebystring are all the same call.
    Last edited by ghettohacker; Dec 13th, 2005 at 06:08 PM.

  24. #24

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Thanks for the help, is there a way now i can get the text from this:


  25. #25

    Re: API Newbie

    if there is a will. there are several ways to do it though, is the window title caption always the same? does the classname change? i don't have that program so i don't know exactly what the best way to go about it would be. one must study its habbits

  26. #26

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Yes, the name of the program stays that same, with those values.

    I checked it a few times with Spy++

    here is my code, i press the button but it dosent get anything:

    Here is whats in my button click

    VB Code:
    1. Dim notepad&, notepadedit&, num&, buff$, edittxt&
    2.  
    3. '--find the right windows--
    4. notepad& = FindWindow("PCSWS:Main:00400000", vbNullString)
    5. notepadedit& = FindWindowEx(notepad&, 0&, "PCSWS:Pres:00400000", vbNullString)
    6. '--
    7. '--create a buffer the length of the string that needs to be loaded
    8. num& = SendMessageByNum(notepadedit&, 14, 0, 0&)
    9. buff$ = Space$(num&)
    10. '--actually retrieve the text-
    11. edittxt& = SendMessageByString(notepadedit&, 13, num& + 1, buff$)
    12. Text1.Text = buff$

  27. #27

    Re: API Newbie

    sendmessage in this context is for edit controls, and that my friend isn't an edit control...its a pcws whatever it is, i'm not a very good teacher, i'd have to have the program, or else i'll be posting a code i think will work, instead of pressing run to find out.

  28. #28

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    Oh i thought this code would work in the same way the one with notepad did.
    I figure i would just find the program, then copy all the text, and display in the box.

  29. #29

    Re: API Newbie

    do you have aim?

  30. #30

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: API Newbie

    ANyone?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width