Author | Message | Time |
---|---|---|
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 author="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. [/quote] [code] 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 [/code] Line 2 is the problem line. | May 14, 2006, 12:24 AM |
UserLoser | FindWindow(vbNullString, whatever) | May 14, 2006, 12:31 AM |
Topaz | I think its supposed to be FindWindow(WindowTitle, vbNullString) | May 14, 2006, 12:35 AM |
warz | 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. [code] 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 [/code] The call to WriteProcessMemory is the error line. Edit: Ah, vbNullString isn't a long. I truly need 'null', not a blank string. | May 14, 2006, 12:37 AM |
warz | [quote author=Topaz link=topic=14973.msg152411#msg152411 date=1147566943] I think its supposed to be FindWindow(WindowTitle, vbNullString) [/quote] Nope. Look at the definition of FindWindow. The first argument is class name, and the second is window name. | May 14, 2006, 12:38 AM |
Newby | vbNull? | May 14, 2006, 12:54 AM |
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. | May 14, 2006, 1:43 AM |
UserLoser | [quote author=warz link=topic=14973.msg152418#msg152418 date=1147571039] 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] WindowHandle = FindWindow(vbNullString, "Brood War") | May 14, 2006, 2:21 AM |
dRAgoN | [quote author=warz link=topic=14973.msg152418#msg152418 date=1147571039] 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] 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. | May 14, 2006, 2:22 AM |
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) [code] 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 [/code] | May 14, 2006, 2:57 AM |
UserLoser | because vbNull = 1, while vbNullString = "" | May 14, 2006, 3:56 AM |
FrOzeN | 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. | May 14, 2006, 4:27 AM |
warz | Hm, and also, did anyone see anything wrong with my OpenProcess call? [code] 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 [/code] Possibly my definition of PROCESS_ALL_ACCESS? | May 14, 2006, 9:46 PM |
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. | May 15, 2006, 12:16 AM |
warz | [quote author=UserLoser link=topic=14973.msg152468#msg152468 date=1147652191] 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] Well, false and 0 should be interpreted the same way. Either way, it doesn't change the result of the call. | May 15, 2006, 12:32 AM |
dRAgoN | [code] 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[/code] 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? | May 15, 2006, 1:17 AM |
warz | The problem has been fixed. The visual basic version of my class now works. The definition of VirtualAllocEx was incorrect. | May 15, 2006, 1:33 AM |
UserLoser | [quote author=warz link=topic=14973.msg152470#msg152470 date=1147653139] [quote author=UserLoser link=topic=14973.msg152468#msg152468 date=1147652191] 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] Well, false and 0 should be interpreted the same way. Either way, it doesn't change the result of the call. [/quote] 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 "") | May 22, 2006, 5:21 AM |
warz | Well, this is fairly old, but I wasn't talking about the Null keyword. I was only talking about the False keyword, and 0. | May 22, 2006, 5:55 PM |
l2k-Shadow | 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. | May 22, 2006, 7:22 PM |
JoeTheOdd | 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. | May 24, 2006, 12:37 PM |
Myndfyr | I realize that this is the VB forum, but I couldn't let misinformation go. [quote author=J link=topic=14973.msg153082#msg153082 date=1148474235] As for null, it's a 0x0000000000000000 pointer to a java.lang.Object, I believe. [/quote] 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: [code] string str = "abc"; IPAddress addr = Dns.Resolve("forum.valhallalegends.com").AddressList[0]; str = addr = null; [/code] 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. | May 24, 2006, 6:15 PM |
JoeTheOdd | 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 [tt]addr = (IPAddress)null;[/tt] 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. | May 25, 2006, 2:44 AM |
warz | yeaah yeaah yeahh jav-WAH? | May 25, 2006, 7:28 AM |
JoeTheOdd | Jawa! | May 25, 2006, 12:26 PM |
K | [quote author=l2k-Shadow link=topic=14973.msg152962#msg152962 date=1148325766] 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. [/quote] 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. [code] ' I hope this is valid Visual Basic syntax Dim b as Boolean Dim i as Integer b = False i = CInt(b) MessageBox CStr(i) [/code] | May 26, 2006, 10:50 PM |
FrOzeN | [quote author=K link=topic=14973.msg153210#msg153210 date=1148683813] [quote author=l2k-Shadow link=topic=14973.msg152962#msg152962 date=1148325766] 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. [/quote] 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. [code] ' I hope this is valid Visual Basic syntax Dim b as Boolean Dim i as Integer b = False i = CInt(b) MessageBox CStr(i) [/code] [/quote] 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" :) | May 27, 2006, 2:29 AM |
Adron | [quote author=UserLoser link=topic=14973.msg152948#msg152948 date=1148275284] 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 "") [/quote] vbNullString is actually not "", and it can be important to separate the two. Use the right one at the right time. | May 27, 2006, 2:16 PM |
FrOzeN | [quote author=Adron link=topic=14973.msg153245#msg153245 date=1148739385] vbNullString is actually not "", and it can be important to separate the two. Use the right one at the right time. [/quote] 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) | May 27, 2006, 2:21 PM |
warz | 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. [/quote] | May 27, 2006, 7:34 PM |