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.
FindWindow(vbNullString, whatever)
I think its supposed to be FindWindow(WindowTitle, vbNullString)
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.
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.
vbNull?
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.
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")
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.
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
because vbNull = 1, while vbNullString = ""
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.
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?
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.
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.
Dim HWND As Long, PiD As Long, phandle As Long
HWND = FindWindow(windowclass, windowcaption)
If (HWND <> 0) Then
GetWindowThreadProcessId HWND, PiD
phandle = OpenProcess(PROCESS_ALL_ACCESS, False, PiD)
If (phandle <> 0) Then
WriteProcessMemory phandle, ByVal Address&, Value, 1, 0&
End If
CloseHandle phandle
End If
This is how I use those calls from something I built quite some time ago and it still works aswell.
The way you have yours set up looks right.
Your PROCESS_ALL_ACCESS is the right value also.
So what are you useing this on?
The problem has been fixed. The visual basic version of my class now works. The definition of VirtualAllocEx was incorrect.
Quote from: warz on May 14, 2006, 07:32 PM
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.
False and 0 in Visual Basic is not the same as the Null keyword in Visual Basic at all. If anything needs to be passed as a null parameter, use 0 (same thing as passing vbNullString aka "")
Well, this is fairly old, but I wasn't talking about the Null keyword. I was only talking about the False keyword, and 0.
the way i interpret it is that False and 0 are the same, however, Null is more like the idea of nothing, while 0 is still a value of a defined variable.
As far as Java is concerned, false and 0 are values, that of a boolean or an int. As for null, it's a 0x0000000000000000 pointer to a java.lang.Object, I believe.
I realize that this is the VB forum, but I couldn't let misinformation go.
Quote from: J on May 24, 2006, 07:37 AM
As for null, it's a 0x0000000000000000 pointer to a java.lang.Object, I believe.
Well, it's only 0x00000000 on 32-bit platforms. And it's not pointing to anything, that's why it's null. 0x00000000 is usually an invalid memory reference.
null can be assigned to any reference of which the type is derived from Object. This should be more or less any reference, since all Java classes derive from java.lang.Object.
I'm not sure whether null in Java is implicitly typed. In C#, there are implicit null typing rules, such as:
string str = "abc";
IPAddress addr = Dns.Resolve("forum.valhallalegends.com").AddressList[0];
str = addr = null;
Line 3 would cause a compile-time error because, even though null is being assigned to addr, addr's type is IPAddress, and even as it's null, it can't be assigned to str.
In Visual Basic .NET, the keyword
Nothing should be used instead of
Null when you are referring to a null reference.
In Java, if I understand correctly, line three would also provide you with a nice error, but (again, I think) when null is passed or assigned it's assumed that it's a java.lang.Object, so replacing line three with something along the lines of addr = (IPAddress)null; would be allowed by the syntax (it may be incomptable types, but that's a different story).
But this is a nice topic and I don't want to kill it, so if someone wants to add to the java aspect, could you split it? Thanks.
yeaah yeaah yeahh jav-WAH?
Jawa!
Quote from: l2k-Shadow on May 22, 2006, 02:22 PM
the way i interpret it is that False and 0 are the same, however, Null is more like the idea of nothing, while 0 is still a value of a defined variable.
I believe that the value of False in visual basic 6 is actual -1, strangely enough. I don't have it installed, so I can't test it, but I seem to recall this being true.
' I hope this is valid Visual Basic syntax
Dim b as Boolean
Dim i as Integer
b = False
i = CInt(b)
MessageBox CStr(i)
Quote from: K on May 26, 2006, 05:50 PM
Quote from: l2k-Shadow on May 22, 2006, 02:22 PM
the way i interpret it is that False and 0 are the same, however, Null is more like the idea of nothing, while 0 is still a value of a defined variable.
I believe that the value of False in visual basic 6 is actual -1, strangely enough. I don't have it installed, so I can't test it, but I seem to recall this being true.
' I hope this is valid Visual Basic syntax
Dim b as Boolean
Dim i as Integer
b = False
i = CInt(b)
MessageBox CStr(i)
In VB6, False is "0". True is everything else, but default it's "-1". Also with your code, it's right except MessageBox is "MsgBox" :)
Quote from: UserLoser on May 22, 2006, 12:21 AM
False and 0 in Visual Basic is not the same as the Null keyword in Visual Basic at all. If anything needs to be passed as a null parameter, use 0 (same thing as passing vbNullString aka "")
vbNullString is actually not "", and it can be important to separate the two. Use the right one at the right time.
Quote from: Adron on May 27, 2006, 09:16 AM
vbNullString is actually not "", and it can be important to separate the two. Use the right one at the right time.
I've read about vbNullString hundreds of times but forget the reasoning behind it. But my understanding of it, is that as a value it's the same as "" but Visual Basic handles it different and is a preferred method as it provides some slightly more proficient capabilities over "" (non of which I can remember).
Can you tell me a case of where you would ever use "" instead of vbNullString? (Just as reference, I'm trying to grasp a full understanding of all the minor things in VB6 that tutorials don't touch up on)
This entire thread is easily answered by MSDN.
Quote
Constant: vbNullString
Equiv: String having value 0
Desc: Not the same as a zero-length string (""); used for calling external procedures.
NullChar
Constant: vbNullChar
Equiv: Chr(0)
Desc: Character having value 0
Null
Constant: vbNull
Desc: Null object.
False
Desc: False. The numeric value of this member is 0.