Somethin' went wrong :'(

Windows specific questions.
Post Reply
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Somethin' went wrong :'(

Post by datwill310 »

Hi. I've got this code which is supposed to create a window, and then destroy it when the user presses a key at the console:

Code: Select all

function createwin(windowName as LPCTSTR, x as integer, y as integer, winWidth as integer, winHeight as integer, styles as DWORD = WS_OVERLAPPEDWINDOW+WS_CLIPSIBLINGS+WS_VISIBLE, extStyles as DWORD = 0, parent as HWND = null, menu as HMENU = null) as HWND
	var hLib = dylibload("user32.dll")
	if hLib = null then return null
	dim pCreateWindowEx as function (byval as DWORD, byval as LPCTSTR, byval as LPCTSTR, byval as DWORD, byval as integer, byval as integer, byval as integer, byval as integer, byval as HWND, byval as HMENU, byval as HINSTANCE, byval as LPVOID) as HWND
	pCreateWindowEx = dylibsymbol(hLib, "CreateWindowExA")
	if pCreateWindowEx = null then dylibfree(hLib): return null
	var res = pCreateWindowEx(extStyles, chr(0), windowName, styles, x, y, winWidth, winHeight, parent, menu, null, null)
	dylibfree(hLib)
	return res
end function

function killwin(hWin as HWND) as boolean
	var hLib = dylibload("user32.dll")
	if hLib = null then return false
	dim pDestroyWindow as function (byval as HWND) as boolean
	pDestroyWindow = dylibsymbol(hLib, "DestroyWindow")
	if pDestroyWindow = null then dylibfree(hLib): return false
	var res = pDestroyWindow(hWin)
	dylibfree(hLib)
	return res
end function

dim as HWND windowHandle = createwin("Tester Window", 50, 35, 175, 100)
if windowHandle = 0 then print "Somethin' went wrong :'("
sleep
print killwin(windowHandle)
sleep
system
The functions are the way they are because they are going to be part of a fairly big and still expanding library of mine, and they are being tested. I've checked, by printing messages, that both the DLL and function load with their appropriate functions. It's something to do with my parameters in the createwin function I pass to the CreateWindowExA function, or the CreateWindowExA function just didn't work for some other reason. Could somebody lead me in the right direction, and if this sort of API thing happens again, ways in which I can find out the problem without having to post for help (could I implement a temporary GetLastError call)?
SARG
Posts: 1766
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Somethin' went wrong :'(

Post by SARG »

Hi,

In pCreateWindowEx(extStyles, chr(0), windowName, styles, x, y, winWidth, winHeight, parent, menu, null, null)

No class is defined : chr(0) ...... If you replace chr(0) by "button" you get a window (button).

Extract from Windows's API help file : lpClassName

Points to a null-terminated string or is an integer atom. If lpClassName is an atom, it must be a global atom created by a previous call to GlobalAddAtom. The atom, a 16-bit value less than 0xC000, must be in the low-order word of lpClassName; the high-order word must be zero.

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


For registring a class something like that :

Code: Select all

     '' Setup window class
     ''
     fb_hinstance=hinstance
     With wcls
     	.style         = CS_HREDRAW Or CS_VREDRAW
     	.lpfnWndProc   = Cast(WndProc,@WndProc)
     	.cbClsExtra    = 0
     	.cbWndExtra    = 0
     	.hInstance     = hInstance
     	.hIcon         = LoadIcon( hInstance,MAKEINTRESOURCE(1))'LoadIcon( null, byval IDI_APPLICATION )
     	.hCursor       = LoadCursor( NULL, ByVal IDC_ARROW )
     	.hbrBackground = GetStockObject( WHITE_BRUSH )
     	.lpszMenuName  = NULL
     	.lpszClassName = StrPtr( fb_szAppName )  <<<<<------------------- here 
     End With
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Somethin' went wrong :'(

Post by sancho2 »

Because your code requires you to #include "windows.bi", you don't need to include the library loading in your functions. For example:

Code: Select all

#Include "windows.bi"
function createwin(windowName as LPCTSTR, x as integer, y as integer, winWidth as integer, winHeight as integer, styles as DWORD = WS_OVERLAPPEDWINDOW+WS_CLIPSIBLINGS+WS_VISIBLE, extStyles as DWORD = 0, parent as HWND = null, menu as HMENU = null) as HWND
   var res = CreateWindowEx(extStyles, "button", windowName, styles, x, y, winWidth, winHeight, parent, menu, null, null)
   return res
end function
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Somethin' went wrong :'(

Post by datwill310 »

sancho2 wrote:Because your code requires you to #include "windows.bi", you don't need to include the library loading in your functions. For example:

Code: Select all

#Include "windows.bi"
function createwin(windowName as LPCTSTR, x as integer, y as integer, winWidth as integer, winHeight as integer, styles as DWORD = WS_OVERLAPPEDWINDOW+WS_CLIPSIBLINGS+WS_VISIBLE, extStyles as DWORD = 0, parent as HWND = null, menu as HMENU = null) as HWND
   var res = CreateWindowEx(extStyles, "button", windowName, styles, x, y, winWidth, winHeight, parent, menu, null, null)
   return res
end function
I know this is going to seem stupid, but I knew there was something wrong with the whole thing in the first place <:D *nervous laughs*. Thanks for reminding me of how simple that was;

Code: Select all

#include "windows.bi"
dm res as HWND = CreateWindowExA(0, "button", "How stupid datwill310 can be sometimes...", ...)
ect.
Will this work with all DLLs, that is not just uer32.dll, but ones like kernel32.dll and comctrl32.dll?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Somethin' went wrong :'(

Post by MichaelW »

There is a function here that returns a system-defined error message for the last-error code value, making it a little easier to interpret the error code. Note that the returned message is valid only after a function that sets the last-error code value returns (to the caller) a value that indicates an error. To determine if a given function sets the last-error code value, and what it returns when it does so, refer to the function documentation.
Post Reply