• Welcome to Valhalla Legends Archive.
 

Invalid use of null?

Started by warz, May 13, 2006, 07:24 PM

Previous topic - Next topic

warz

I'm converting some C++ code of mine to visual basic for a friend. Does anyone know why I'm getting the run-time error "invalid use of null" ?

Quote from: msdn
Parameters

    lpClassName
        [in] Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.

        If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.

        If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.
    lpWindowName
        [in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Function InjectWindowByTitle(ByVal WindowTitle As String, ByVal FileName As String) As Boolean
    procInfo.hTargetHwnd = FindWindow(Null, WindowTitle)
    procInfo.szLibrary = FileName
   
    InjectWindowByTitle = Inject()
    Return
End Function


Line 2 is the problem line.

UserLoser

FindWindow(vbNullString, whatever)

topaz

I think its supposed to be FindWindow(WindowTitle, vbNullString)
RLY...?

warz

#3
Hm. Alright, further down in my code I've switched 'null' to 'vbnullchar'. It says type-mismatch. I'm not sure if it has to do with the vbnullchar variable, or not.


Private Declare Function WriteProcessMemory Lib "kernel32" _
    (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

    If (Not WriteProcessMemory(procInfo.hProcess, procInfo.rAddress, procInfo.szLibrary, Len(procInfo.szLibrary), vbNullString)) Then
        Call VirtualFreeEx(procInfo.hProcess, procInfo.rAddress, Len(procInfo.szLibrary), (MEM_RELEASE And MEM_DECOMMIT))
        Inject = False
        Return
    End If


The call to WriteProcessMemory is the error line.

Edit: Ah, vbNullString isn't a long. I truly need 'null', not a blank string.

warz

Quote from: Topaz on May 13, 2006, 07:35 PM
I think its supposed to be FindWindow(WindowTitle, vbNullString)

Nope. Look at the definition of FindWindow. The first argument is class name, and the second is window name.

Newby

- Newby

Quote[17:32:45] * xar sets mode: -oooooooooo algorithm ban chris cipher newby stdio TehUser tnarongi|away vursed warz
[17:32:54] * xar sets mode: +o newby
[17:32:58] <xar> new rule
[17:33:02] <xar> me and newby rule all

Quote<TehUser> Man, I can't get Xorg to work properly.  This sucks.
<torque> you should probably kill yourself
<TehUser> I think I will.  Thanks, torque.

warz

Ah. vbNull seems to be what I need. Anywho, the call the FindWindow is always returning 0. Am I doing something wrong, or missing something? Obviously, the window I'm trying to grab the hwnd of is open.

UserLoser

Quote from: warz on May 13, 2006, 08:43 PM
Ah. vbNull seems to be what I need. Anywho, the call the FindWindow is always returning 0. Am I doing something wrong, or missing something? Obviously, the window I'm trying to grab the hwnd of is open.

WindowHandle = FindWindow(vbNullString, "Brood War")

l)ragon

Quote from: warz on May 13, 2006, 08:43 PM
Ah. vbNull seems to be what I need. Anywho, the call the FindWindow is always returning 0. Am I doing something wrong, or missing something? Obviously, the window I'm trying to grab the hwnd of is open.
Some windows are hidden from that API call iirc, I would think it's probably best to get the window handle through the process listing.
*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*ˆ¨¯¯¨ˆ*^~·.,l)ragon,.-·~^*ˆ¨¯¯¨ˆ*^~·.,¸¸,.·´¯`·.,¸¸,.-·~^*

warz

Ah. Strange. Sometimes I need vbNull, and sometimes vbNullString. FindWindow apparantly likes vbNullString.

Anyways, the window handle is now found, but my OpenProcess call is returning 0. OpenProcess returns NULL on failure, and otherwise a handle to the opened process. I'm receiving 0, though.

Does visual basic interpret Null as 0, or would this code catch the null result. (it should)


    Private Const PROCESS_ALL_ACCESS& = &H1F0FFF
    Call GetWindowThreadProcessId(procInfo.hTargetHwnd, procInfo.dwProcessId) // this works
   
    If ((procInfo.hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, procInfo.dwProcessId)) = Null) Then
        Inject = False
        Exit Function
    End If

UserLoser

because vbNull = 1, while vbNullString = ""

FrOzeN

#11
Also vbNullChar is the same as Chr(0).

[EDIT] Also with your code above. Replace '= Null' with '= 0'. When an API call fails, generally when it returns a "Null" value for an error, it means it returns 0. The keyword 'Null' in Visual Basic is used to indicate that there is no valid data in an "As Variant" variable.
~ FrOzeN

warz

Hm, and also, did anyone see anything wrong with my OpenProcess call?


    Private Const PROCESS_ALL_ACCESS& = &H1F0FFF
    Call GetWindowThreadProcessId(procInfo.hTargetHwnd, procInfo.dwProcessId) // this works
   
    If ((procInfo.hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, procInfo.dwProcessId)) = Null) Then
        Inject = False
        Exit Function
    End If


Possibly my definition of PROCESS_ALL_ACCESS?

UserLoser

Don't use Null keyword at all in VB...it's rather pointless.  Btw, second parameter should be 0 not False.  There's a difference.  A BOOL isn't same as a boolean in Visual Basic.  NULL isn't same as Null keyword in VB.  Don't use it.  NULL as defined on MSDN would be 0 in Visual Basic (or in anything really).  #define NULL (void*)0 or something to that affect.  MSDN prototypes are all C++ syntaxish based.

warz

Quote from: UserLoser on May 14, 2006, 07:16 PM
Don't use Null keyword at all in VB...it's rather pointless.  Btw, second parameter should be 0 not False.  There's a difference.  A BOOL isn't same as a boolean in Visual Basic.  NULL isn't same as Null keyword in VB.  Don't use it.  NULL as defined on MSDN would be 0 in Visual Basic (or in anything really).  #define NULL (void*)0 or something to that affect.  MSDN prototypes are all C++ syntaxish based.

Well, false and 0 should be interpreted the same way. Either way, it doesn't change the result of the call.