Click here to Skip to main content
15,896,606 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Control Collections with Multiple Forms Pin
carterza1-Mar-09 18:53
carterza1-Mar-09 18:53 
QuestionSQL queries - Where is my mistake Pin
ivo751-Mar-09 7:50
ivo751-Mar-09 7:50 
AnswerRe: SQL queries - Where is my mistake Pin
Henry Minute1-Mar-09 8:10
Henry Minute1-Mar-09 8:10 
GeneralRe: SQL queries - Where is my mistake Pin
Luc Pattyn1-Mar-09 9:18
sitebuilderLuc Pattyn1-Mar-09 9:18 
QuestionHow can I automate Winforms of .NET in VB6? Pin
Oliverikawood1-Mar-09 6:29
Oliverikawood1-Mar-09 6:29 
AnswerRe: How can I automate Winforms of .NET in VB6? Pin
ABitSmart1-Mar-09 22:38
ABitSmart1-Mar-09 22:38 
GeneralRe: How can I automate Winforms of .NET in VB6? Pin
Oliverikawood5-Mar-09 5:40
Oliverikawood5-Mar-09 5:40 
AnswerRe: How can I automate Winforms of .NET in VB6? Pin
Oliverikawood9-Mar-09 0:45
Oliverikawood9-Mar-09 0:45 
Hi All,

I have managed VirtualAllocEx. Before I declared this function as Private. Then when I moved it to Module file and changed it to Public, I don't get 0 anymore for bufferMem. Also I need to set lpAddress as ByVal. Otherwise, the length of controlName (retLength) that given is twice than correct length (I don't know exactly how this happens).
Declare Function VirtualAllocEx Lib "kernel32" _
                             (ByVal hProcess As Long, _
                             ByVal lpAddress As Long, _
                             ByVal dwSize As Long, _
                             ByVal flAllocationType As Long, _
                             ByVal flProtect As Long) As Long

Now I got the correct retLength, but unfortunately retVal is 0, return the error description: Invalid procedure call or argument.
If retLength <> 0 Then
          'Now read the component's name from the shared memory location.
              retVal = ReadProcessMemory(processHandle, bufferMem, bytearray, size, VarPtr(written))
              If retVal = 0 Then
                   Error Err 'ReadProcessMemory API Failed
              End If
          End If

My declaration of ReadProcessMemory is:
Declare Function ReadProcessMemory Lib "kernel32" _
                             (ByVal hProcess As Long, _
                             ByVal lpBaseAddress As Long, _
                             lpBuffer As Any, _
                             ByVal nSize As Long, _
                             lpNumberOfBytesRead As Long) As Long

And when I run the whole thing (ignore the retVal value), I got error in WideCharToMultiByte:
Function ByteArrayToString(bytes As String, length As Long) As String
   Dim retValStr As String
   Dim l As Long

   If IsWin9x() Then
       retValStr = Left(bytes, InStr(1, bytes, Chr(0)) - 1)
   Else
       retValStr = String$(length + 1, Chr(0))
       l = WideCharToMultiByte(CP_ACP, 0, bytes, -1, retValStr, length + 1, Null, Null)
   End If

   ByteArrayToString = retValStr
End Function

The error is saying:
Run-time error '94':

Invalid use of Null

Any idea?

Here is the code:
Function GetWindowsFormsID(ByVal wnd As Long) As String
    Dim PID As Long 'pid of the process that contains the control
    Dim msg As Long

    ' Define the buffer that will eventually contain the desired
    ' component's name.
    Dim bytearray As String * 65526

    ' Allocate space in the target process for the buffer as shared
    ' memory.
    Dim bufferMem As Long
    ' Base address of the allocated region for the buffer.
    Dim size As Long
    ' The amount of memory to be allocated.
    Dim written As Long
    ' Number of bytes written to memory.
    Dim retLength As Long
    Dim retVal As Long
    Dim errNum As Integer
    Dim errDescription As String

    size = 65527 'Len(bytearray)

    ' Creating and reading from a shared memory region is done
    ' differently in Win9x than in newer Oss.
    Dim processHandle As Long
    Dim fileHandle As Long

    msg = RegisterWindowMessage("WM_GETCONTROLNAME")

    If Not IsWin9x() Then
        On Local Error GoTo Error_Handler_NT
            Dim dwResult As Long
            Call GetWindowThreadProcessId(wnd, PID)

            processHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, PID)
            If processHandle = 0 Then
                Error Err 'OpenProcess API Failed
            End If

            bufferMem = VirtualAllocEx(processHandle, 0, size, MEM_RESERVE Or MEM_COMMIT, PAGE_READWRITE)
            If bufferMem = 0 Then
                Error Err 'VirtualAllocEx API Failed
            End If

            ' Send message to the control's HWND for getting the
            ' Specified control name.
            retLength = SendMessage(wnd, msg, size, ByVal bufferMem)

            If retLength <> 0 Then
            ' Now read the component's name from the shared memory location.
                retVal = ReadProcessMemory(processHandle, bufferMem, bytearray, size, VarPtr(written))
                If retVal = 0 Then
                     Error Err 'ReadProcessMemory API Failed
                End If
            End If

Error_Handler_NT:
            errNum = Err
            errDescription = Error$
            ' Free the memory that was allocated.
            retVal = VirtualFreeEx(processHandle, bufferMem, 0, MEM_RELEASE)
            If retVal = 0 Then
                Error Err 'VirtualFreeEx API Failed
            End If
            CloseHandle (processHandle)
            If errNum <> 0 Then
                On Local Error GoTo 0
                Error errNum 'errDescription
            End If
        On Local Error GoTo 0


    Else
       On Local Error GoTo Error_Handler_9x

            Dim SA As SECURITY_ATTRIBUTES

            fileHandle = CreateFileMapping(INVALID_HANDLE_VALUE, SA, PAGE_READWRITE, 0, size, Null)
            If fileHandle = 0 Then
                Error Err 'CreateFileMapping API Failed
            End If
            bufferMem = MapViewOfFile(fileHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0)
            If bufferMem = 0 Then
                Error Err 'MapViewOfFile API Failed
            End If

            Call CopyMemory(bufferMem, bytearray, size)

            ' Send message to the treeview control's HWND for
            ' getting the specified control's name.
            retLength = SendMessage(wnd, msg, size, bufferMem)

            ' Read the control's name from the specific shared memory
            ' for the buffer.
            Call CopyMemoryA(bytearray, bufferMem, 1024)

Error_Handler_9x:
            errNum = Err
            errDescription = Error$

            ' Unmap and close the file.
            UnmapViewOfFile (bufferMem)
            CloseHandle (fileHandle)

            If errNum <> 0 Then
                On Local Error GoTo 0
                Error errNum 'errDescription
            End If
        On Local Error GoTo 0

    End If

    If retLength <> 0 Then
    ' Get the string value for the Control name.
    GetWindowsFormsID = ByteArrayToString(bytearray, retLength)
    End If

End Function

QuestionHow can I over come UnAuthorizedAccessPermission when I check a media via IMAPI2? Pin
JUNEYT1-Mar-09 3:10
JUNEYT1-Mar-09 3:10 
Questionflash in vb Pin
ahlamissa1-Mar-09 2:21
ahlamissa1-Mar-09 2:21 
AnswerRe: flash in vb Pin
ABitSmart1-Mar-09 22:24
ABitSmart1-Mar-09 22:24 
QuestionBeginners question: Get class name from within the class Pin
Sonhospa1-Mar-09 1:50
Sonhospa1-Mar-09 1:50 
AnswerRe: Beginners question: Get class name from within the class Pin
0x3c01-Mar-09 6:31
0x3c01-Mar-09 6:31 
GeneralRe: Beginners question: Get class name from within the class Pin
Sonhospa2-Mar-09 8:13
Sonhospa2-Mar-09 8:13 
QuestionRe: Beginners question: Get class name from within the class Pin
Sonhospa2-Mar-09 21:42
Sonhospa2-Mar-09 21:42 
QuestionObject reference not...... error Pin
kralcobra_371-Mar-09 0:16
kralcobra_371-Mar-09 0:16 
AnswerRe: Object reference not...... error Pin
0x3c01-Mar-09 0:25
0x3c01-Mar-09 0:25 
AnswerRe: Object reference not...... error Pin
kralcobra_371-Mar-09 0:34
kralcobra_371-Mar-09 0:34 
GeneralRe: Object reference not...... error Pin
0x3c01-Mar-09 1:06
0x3c01-Mar-09 1:06 
GeneralRe: Object reference not...... error Pin
Luc Pattyn1-Mar-09 3:06
sitebuilderLuc Pattyn1-Mar-09 3:06 
Questionwhat is the code in vb.net to browes for images of types jpg gif bmp in the same button ? Pin
ahlamissa28-Feb-09 22:28
ahlamissa28-Feb-09 22:28 
AnswerRe: what is the code in vb.net to browes for images of types jpg gif bmp in the same button ? Pin
Rupesh Kumar Swami28-Feb-09 23:11
Rupesh Kumar Swami28-Feb-09 23:11 
QuestionAttach Form Class to the it's instance created by assembly Pin
Aiman Farouk Mohamed28-Feb-09 21:39
Aiman Farouk Mohamed28-Feb-09 21:39 
AnswerRe: Attach Form Class to the it's instance created by assembly Pin
Dave Kreskowiak1-Mar-09 4:19
mveDave Kreskowiak1-Mar-09 4:19 
QuestionPrinting multiple pages in Viusal Basic .net each from a different source using Sub Routines Pin
tbkfile28-Feb-09 19:42
tbkfile28-Feb-09 19:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.