Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: CreateWindowEx is resulting Zero.

  1. #1
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    117
    Rep Power
    17

    CreateWindowEx is resulting Zero.

    Hi all,
    I am trying to create a window with win32 functions. But CreateWindowEx is returning 0 . This is my code. Please check this and guide me.
    Uses "Console"
     
    Type tWNDCLASSEX
      cbSize as DWord
      style As Long
      lpfnWndProc As Long
      cbClsExtra as Long
      cbWndExtra as Long
      hInstance As Long
      hIcon As Long
      hCursor As Long
      hbrBackground As Long
      lpszMenuName as Long
      lpszClassName as Long
      hIconSm As Long  
    End Type
    
    Type tPoint
    	x As dword
    	y as dword
    end type
    
    Type tMsg 
      hwnd as Long
      message as Long
      wParam as Long
      lParam as Long
      time as DWord
      pt as tPoint
      lPrivate as DWord
    end type
    
    Declare Function GetModuleHandle Lib "Kernel32.dll" Alias "GetModuleHandleW"(ByVal lpModuleName as Long) As Long
    Declare Function LoadIcon Lib "User32.dll" Alias "LoadIconW"(ByVal hInstance as Long, ByVal lpIconName As Long) As Long
    Declare Function LoadCursor Lib "User32.dll" Alias "LoadCursorW"( ByVal hInstance as Long, ByVal lpIconName As Long) As Long
    Declare Function RegisterClassEx Lib "User32.dll" Alias "RegisterClassExW" (pcWndClassEx As tWNDCLASSEX) As DWord
    Declare Function GetStockObject Lib "Gdi32.dll" Alias "GetStockObject" (ByVal nIndex As Long) As Long
    Declare Function CreateWindowEx Lib "User32.dll" Alias "CreateWindowExW" (ByVal dwExStyle As DWord, ByVal lpClassName As Long, ByVal lpWindowName As Long, ByVal dwStyle As DWord, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByVal lpParam As Long) As Long
    Declare Function ShowWindow Lib "User32.dll"(ByVal lhwnd As Long, ByVal nCmdShow As Long) As Long
    Declare Function UpdateWindow Lib "User32.dll" (ByVal lhwnd As Long) As Long
    Declare Function DefWindowProc Lib "User32.dll" Alias "DefWindowProcW" (ByVal lhwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    '// Declare Function GetMessage Lib "User32.dll" Alias "GetMessageW" (ByRef lpMsg As tMsg, ByVal lhwnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long) As Long
    Declare Function TranslateMessage Lib "User32.dll" Alias "TranslateMessageW" (ByRef lpMsg As tMsg) As Long
    Declare Function DispatchMessage Lib "User32.dll" Alias "DispatchMessageW" (ByRef lpMsg As tMsg) As Long
    
    
    Function MakeIntRs(i As Long) As Variant
      Dim temp As Long = i AND 0xffff
      Function = temp
    End Function
    
    
    
    Dim CSVREDRAW = 0x0001 
    Dim CSHREDRAW = 0x0002
    Dim CSDBLCLKS = 0x0008
    Dim IDC_ARROW = MakeIntRs(32512)	'// I am getting zero as value !
    Dim IDI_APPLICATION = MakeIntRs(32512)
    Dim My_OVERLAPPEDWINDOW As DWord = 0x00000000 OR 0x00C00000 OR 0x00080000 OR 0x00040000 OR 0x00020000 OR 0x00010000 
    Dim SWSHOW As Long = 5
    
    '// %'CS_OWNDC = 0x0020	'// These equates are not working !!!!
    
    Type Application
      ClassName As Asciiz
      hInstance as DWord 
      
      Function RegWindow() 
        Dim wndClsx As tWNDCLASSEX
        me.ClassName = "ThinBasic Window"
        me.hInstance = GetModuleHandle(0)
         with wndClsx
          .cbSize = SizeOf(wndClsx)
          .style = CSHREDRAW OR CSVREDRAW OR CSDBLCLKS
          .lpfnWndProc = Function_GetPtr(WndProc)	'// is this the correct way of getting a function pointer ?
          .cbClsExtra = 0
          .cbWndExtra = 0
          .hInstance = me.hInstance
          .hIcon = LoadIcon(0, IDI_APPLICATION) 
          .hCursor = LoadCursor(0, IDC_ARROW)
          .hbrBackground = GetStockObject(0)
          .lpszMenuName = 0
          .lpszClassName = StrPtr(me.ClassName)
          '// .hIconSm = 0
        End With
    	
        if RegisterClassEx(wndClsx) = 0 Then	'// Well, this is success.
          MsgBox("Window Registration Failed")
        EndIf
        
        PrintL PEEK$(ASCIIZ, wndClsx.lpszClassName)
      End Function   
    End Type
    
    Function WndProc(h As Long, msg As Long, wp As Long, lp As Long)
      Function = DefWindowProc(h, msg, wp, lp)
    End Function
    
    Dim ap as Application
    ap.RegWindow()
    
    dim wname As Asciiz = "My Thin Window"
    dim pWn As Long = StrPtr(wname)
    Dim hwn As Long = CreateWindowEx(0, ap.ClassName, wname, My_OVERLAPPEDWINDOW, 10, 10, 400, 400, 0, 0, ap.hInstance, NULL) '// Main problem lies on this function call.
    PrintL("Window Handle - ", hwn)	'// This will print a zero.
    PrintL(ap.hInstance)			'// But this will print a number.
    
    if hwn > 0 Then					
    	ShowWindow(hwn, 5)
    	UpdateWindow(hwn)
    
    	Dim uMsg As tMsg 
    	While Win_GetMessage(uMsg, hWnd, 0, 0) <> 0
    	    TranslateMessage(uMsg)
    	    DispatchMessage(uMsg)
        Wend
    
    Else
    	MsgBox("Window creation Failed..!")
    EndIf
    
    WaitKey
    
    Last edited by kcvinu; 03-04-2021 at 11:51.

  2. #2
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,417
    Blog Entries
    1
    Rep Power
    156
    am not sure but if you want ti create a windowvlass of the medieval generation there was something to do as calling to initcommon controls checl the tninbasic sample files - i think tjhere washidden on a folcer before ui in a folder hidden behind an example how to load the win32 controls library and use its functions,
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  3. #3
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    117
    Rep Power
    17
    @ReneMiner

    Thanks for the reply. But there is some include files are missing.
    1. "APPUTILS.INC"
    2. "APPPROPS.INC"

    If i get these files, i can try. This is the path of that script.
    C:\thinBasic\SampleScripts\UI\SDK
    In that folder, i couldn't find the include files. If you have them, I request you to kindly upload those. Thanks in advance.

  4. #4
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,417
    Blog Entries
    1
    Rep Power
    156
    no its function?getPtr. the code is not compiled so are no real function pointers, use codeptr and read the manual hpw thinbasic-users can get around this individuality
    i meant the scripts i attchhed here, - it might be right on time for your curren studies / and i think ius 3 scripts of 2 different projects
    Attached Files Attached Files
    Last edited by ReneMiner; 05-04-2021 at 06:18.
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  5. #5
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    117
    Rep Power
    17
    @ReneMiner
    Suddenly it worked !!!.
    After losing hope, I've decided to do some experiments with data types. So I started changing lpWindowName parameter. But my problem area was lpClassName's type.
    So after wasting some time, i decided to experiment with class name. I prefer "Long" since the parameter name starts with lp aka Long ptr. Then i tried Varptr in calling site. And voilą ! It worked all of a sudden. Anyhow, thanks for the scripts. Let me check them. Thanks for the support. At last, i have made my unicode aware window in thinBasic. Now, its time to wrap those things up in a class and write some properties & methods.

    BTW, there was no way for me to understand what is the error but just guessing. Now i want to learn about thinBasic's include file. All the include file i have seen was only contains the function declarations. Can i use them for function & class implementation ?
    Last edited by kcvinu; 05-04-2021 at 23:30.

  6. #6
    thinBasic MVPs ReneMiner's Avatar
    Join Date
    Oct 2012
    Location
    Germany
    Age
    51
    Posts
    1,417
    Blog Entries
    1
    Rep Power
    156
    you can use any tpye of script for what you like,
    Even possible to make your own filetypes and extensions but thinAir will complain. But thincore executes everything. Importan that it must have an app/entrypoint (TBMain mostly.) but App_Entrypoint is a function to use alternatives.That might be useful if you develop something that should run on different systems Win on x64 or x86 or in WINE or react-OS etc.

    I use thinBasics scripttypes as

    .tBasic - always the script containing function tbMain and especially for the current app developed functions

    .tBasicC - plain console only.

    .tBasicI - include files / those are mostly re/useable declarations, wrappers/headers and allround-functions as you might separate the function for printing a page and not to contain it to the mainfile so in your next projct the already well/tested specialization is embedded with 1 line only. i prefer to use include for imported stuff or that translated from other languages

    .tBasicU- thinbasic unit files is what i use for plain thinbasiccode without any external dependencies, preferable for UDTs wher the first line of a unit is actually
    ' info text very interesting lalala
    
    #MinVersion 1.2.3.4
    ' date always good to know what was available / what features were new that time...
    ' written in sepobruary 2123
    
    ' about this and that and this
    ' unit is based on tMyBasetype that is an inflateable SabertoothTiger:
    ' 
    #Include Once "tMyBasetype.tBasicU"
    
    ' now already included because required...
    ' this is a splitteable Mammooth that allows to turn times back
    ' when fed with ancient data
    
     
    Type tMyType [ Extends tMyBasetype ]
      [properties]
     static enum as ...assign to shared& defaults to all objects of this type 
    '                          already before any variable was declared
    ...A As B 
       C As D 
      Functions to be used on this type...
     '... etc
    
    End Type
    
    so these are really Units. completely enclosed no loose code but only what is within Type and End Type.
    Exceptional are #Preparsing/Statements or necessary declarations that are required for one or more functions of that type
    So i am sure that i can embedd -what ever i wrote - in one piece .

    Actually i use one selfmade type of file with an extension as ".thinBasic" that is alike a project-file and contains no real code but only directives and preparsing statements, declarations, included Files and global variable dimensioning (if any)
    when all those things are setup it comes to load the final unit; the main script, containing subs and functions, some callbacks possibly and as last of the script i place tbMain to avoid any error that a function were unknown i let the parser run over everything before execution is performed.
    And i have some fake/scripttype, also named ".thinBasic", registered as "thinBasic.Project.File" and to open with thinBasic.exe. It has right in the classes root a "ShellNew"'-command attached and the Data/Setting is a tiny script Uses "console" and "OS"
    checks the OS_GetCommand(1) for its own name and the folder inside that it was called after i double-clicked the file to know where i wanted the location of my new project

    ip if its own name still contains "New" and "thinBasic". if yes it opens a common doalog to browse for the location where the projectfolder is to create. The folders name or the name of the script if changed becomes the project name, either the file was renamed or not, the files name will give the folder a name and create it and place a script template inside that i selected from a list, renamed to be the <Projectname>_Main.tBasic
    it creates a skeleton using #Region "Directives" & #EndRegion, #Region "External Sources" etc until #Region "Global Variables".

    "TheNewthinBasic.Project.File# that was in the directory where is now a new projectfolder vanished already and inside of the folder are 2 scripts already named before i start to write a line of code.
    running thinBasic in HP Pavillon Desktop 590 a0xxx 8GB Ram AMD A6-9225 Radeon R4 5Compute Cores 2C+3G @2.6GHz

    OS: downgraded again to Windows 10 Enterprise x64 10.0.18363 Build 18363 (1909)
    (alternate OS: modified WinRE - boot from a fake ramdisk - currently in "reconstruction mode")

  7. #7
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    117
    Rep Power
    17
    @ReneMiner,
    Thanks for the detailed reply.
    I don't know life is full of obstacles in thinBasic. Here is my declaration of my CreateWindowEx.
    Declare Function CreateWindowEx Lib "User32.dll" Alias "CreateWindowExW" (ByVal dwExStyle As DWord, _
                                                                              Byval lpClassName As Long, _
                                                                              Byval lpWindowName As WStringZ, _
                                                                              ByVal dwStyle As DWord, _
                                                                              ByVal x As Long, ByVal y As Long, _
                                                                              ByVal nWidth As Long, ByVal nHeight As Long, _
                                                                              ByVal hWndParent As DWord, _
                                                                              ByVal hMenu As DWord, _
                                                                              ByVal hInstance As DWord, _
                                                                              lpParam As Any) As DWord
    
    And here is my window craetion code. It is working like a charm.
    dim WinTxt As String = "My Window"  
    dim WinClass As String = "ThinBasic Window"
    dim hInsta As DWord = GetCurrentInstance
    
    Dim hwn As DWord = CreateWindowEx(0, VarPtr(WinClass ), WinTxt , 
                                        %WS_OVERLAPPEDWINDOW, 
                                        600, 100, 700, 400, 
                                        %HWND_DESKTOP, %NULL, 
                                        hInsta, %NULL)
    
    Now, i use CreateWindowEx again to create a button but it's not working. Here is the button creation code.
    Dim btxt as string = "Click Me"
    Dim bc As String= "Button"
    Dim bhwn As DWord  = CreateWindowEx(0, Varptr(bc), btxt , 
                                        %WS_CHILD or %WS_VISIBLE, 
                                        140, 120, 150, 50, 
                                        hwn, %NULL, 
                                        hInsta, 
                                        %NULL)
    PrintL("Button handle ", bhwn) '\\ This is printing zero.
    
    Same function declaration. Same data types. But one is working, another is not.
    Last edited by kcvinu; 06-04-2021 at 20:49.

  8. #8
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    Ciao,

    just to let you know I'm following this thread testing what you are trying to do and see if there are problems from thinBasic side.

    I can confirm there is something I need to fix and release in next thinBasic version.
    I found a big bug in CodePtr function and possibly some problems in passing parameters in external declared functions.

    I will be more precise in next days and possibly release a new thinBasic version with the fixes.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  9. #9
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    54
    Posts
    8,636
    Blog Entries
    2
    Rep Power
    10
    Quote Originally Posted by kcvinu View Post
    Now, i use CreateWindowEx again to create a button but it's not working. Here is the button creation code.
    Dim btxt as string = "Click Me"
    Dim bc As String= "Button"
    Dim bhwn As DWord  = CreateWindowEx(0, Varptr(bc), btxt , 
                                        %WS_CHILD or %WS_VISIBLE, 
                                        140, 120, 150, 50, 
                                        hwn, %NULL, 
                                        hInsta, 
                                        %NULL)
    PrintL("Button handle ", bhwn) '\\ This is printing zero.
    
    Same function declaration. Same data types. But one is working, another is not.
    Try to change
    Dim bc As String= "Button"
    
    to
    Dim bc As String= UTF8ToWideChar$("button")
    
    And use StrPtr (not VarPtr) to return the pointer to the first byte of a dynamic string
    Last edited by ErosOlmi; 07-04-2021 at 16:10.
    www.thinbasic.com | www.thinbasic.com/community/ | psch.thinbasic.com
    Win10Pro 64bit - 8GB Ram - Intel i7 M620 2.67GHz - NVIDIA Quadro FX1800M 1GB

  10. #10
    Member
    Join Date
    Aug 2015
    Location
    Keralam, India
    Posts
    117
    Rep Power
    17
    @ErosOlmi,
    Thanks for your help. I am glad that you are following this thread and improving thinBasic. I tried your suggestions and this the scenario which worked for me.
    In the function declaration of CreteWindowEx, this is enough.
    Byval lpClassName As Long, _
    Byval lpWindowName As String, _  '// Yes, it worked with just string
    
    And in the window creation site,
    dim clsName As String = UTF8ToWideChar$("ThinBasic Window")
    dim WinText As String = UTF8ToWideChar$("ThinBasic Unicode Window") 
    Dim hwn As DWord = CreateWindowEx(0, VarPtr(clsName ), WinText ,  '// Yes !!! Strptr is not worked here. 
                                        %WS_OVERLAPPEDWINDOW, 
                                        600, 100, 700, 400, 
                                        %HWND_DESKTOP, %NULL, 
                                        hInsta, %NULL)
    
    So far so good. Now, the interesting part is the button creation.
    Dim btxt as string = UTF8ToWideChar$("Unicode Button")
    Dim bc As String= UTF8ToWideChar$("button")
    Dim bhwn as DWord  = CreateWindowEx(0, StrPtr(bc), btxt, '//  Surprisingly, Varptr not worked here. But Strptr worked like a charm.
                                       btnStyle, 
                                        140, 120, 150, 50, 
                                        hwn, 101, 
                                        hInsta, 
                                        0)
    
    See the differences ?
    Last edited by kcvinu; 07-04-2021 at 21:43.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •