Call win32 api to OpenFileDialog question

Windows specific questions.
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: Call win32 api to OpenFileDialog question

Post by PaulSquires »

BTW, if you are having trouble with unicode languages within FreeBasic then you should really check out Jose's WinFBX framework. It handles unicode easily especially when using his CWSTR string class. I used his framework extensively when I re-wrote the WinFBE Editor. In WinFBE, you simply need to use Jose's framework and save your source files using UTF-8 or UTF-16 (you can easily change a file format by clicking on the label in the statusbar).

Here is Jose's framework.
https://github.com/JoseRoca/WinFBX

If you are already using the WinFBE Editor Suite, then Jose's framework is installed automatically in the \WinFBE_Suite-Editor\toolchains\FreeBASIC-1.09.0-winlibs-gcc-9.3.0\inc\Afx folder. You simply need to #Include his code files into your WinFBE source code files.
PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Re: Call win32 api to OpenFileDialog question

Post by PeterHu »

PaulSquires wrote: Oct 06, 2022 12:05
PeterHu wrote: Oct 06, 2022 3:09 When I run in WinFBE with below,it seems that IF pwszName evaluated false so no MessageBoxW pops up ,nor the folder dialog opened.Was I missing something?
DIM pwszName AS WSTRING PTR = AfxIFileOpenDialog(GetModuleHandle(0))
Why are you passing the EXE's handle using GetModuleHandle to the AfxIFileOpenDialog function instead of a valid Window handle? Pass the hWnd of the parent dialog that you want the OpenDialog to display above.
Because I have no WinMain(HINSTANCE,HINSTANCE,LPSTR,int) defined.Just a normal plain freebasic console app.
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: Call win32 api to OpenFileDialog question

Post by PaulSquires »

PeterHu wrote: Oct 06, 2022 13:58 Just a normal plain freebasic console app.
In that case you would need to ensure that COM is initialized.
Also, pass "0" (or NULL) as the Window hWnd which represents the Windows desktop.

Code: Select all

' // Initialize the COM library
CoInitialize(NULL)

DIM pwszName AS WSTRING PTR = AfxIFileOpenDialog(0)
IF pwszName THEN
   MessageBoxW(0, *pwszName, "IFileOpenDialog", MB_OK)
   CoTaskMemFree(pwszName)
END IF

' // Uninitialize the COM library
CoUninitialize

PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Re: Call win32 api to OpenFileDialog question

Post by PeterHu »

PaulSquires wrote: Oct 06, 2022 12:15 BTW, if you are having trouble with unicode languages within FreeBasic then you should really check out Jose's WinFBX framework. It handles unicode easily especially when using his CWSTR string class. I used his framework extensively when I re-wrote the WinFBE Editor. In WinFBE, you simply need to use Jose's framework and save your source files using UTF-8 or UTF-16 (you can easily change a file format by clicking on the label in the statusbar).

Here is Jose's framework.
https://github.com/JoseRoca/WinFBX

If you are already using the WinFBE Editor Suite, then Jose's framework is installed automatically in the \WinFBE_Suite-Editor\toolchains\FreeBASIC-1.09.0-winlibs-gcc-9.3.0\inc\Afx folder. You simply need to #Include his code files into your WinFBE source code files.
Yes,I have the suite.And per my poor knowledge about afx,I knew it is great and been widely used,some other IDEs (if I recall correct) like WinFBE,Visual FreeBasic,Visual FBEditor use afx as (part of) foundation library.During the past a couple of days I tested my pc and FreeBasic workshop configuration but I sadlly found there is something wrong with my pc or my configuration,esp. on unicode(asain charater)support.Ansi mode works properly in all the above mentioned IDEs but UTF8/16/32 modes don't.

With below code in WinFBE,ansi mode,string version displaying properly while cwstr version displayed all the asian words overlapped;
Changing to UTF8/UTF16 mode,recompiling-running-recompiling--running several times with the program,it behaves differently.Despite of the same overlapped display of cwstr version,the string version just sometimes displayed nothing(just blank),sometimes displayed.

I realised there is somthing definitely wrong with my PC or my configuration to my FreeBasic workshop,but I can't figure out what is the right one so I can stick to it.

Code: Select all

#include "afx\cwstr.inc"

print !"afx\\cwstr version----\n"
DIM cws AS CWSTR = "一个"
DIM s AS STRING = "三个"
cws = cws + " 两个 " + s
PRINT cws

sleep

print !"\n\n\nplain string version----\n"
dim str1 as string ="一个"
dim str2 as string ="两个"
dim str3 as string ="三个"

str1+=str2+str3
print str1


print !"\n\nPress any key to continue..."
sleep
Last edited by PeterHu on Oct 06, 2022 15:12, edited 2 times in total.
PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Re: Call win32 api to OpenFileDialog question

Post by PeterHu »

PaulSquires wrote: Oct 06, 2022 14:16
PeterHu wrote: Oct 06, 2022 13:58 Just a normal plain freebasic console app.
In that case you would need to ensure that COM is initialized.
Also, pass "0" (or NULL) as the Window hWnd which represents the Windows desktop.

Code: Select all

' // Initialize the COM library
CoInitialize(NULL)

DIM pwszName AS WSTRING PTR = AfxIFileOpenDialog(0)
IF pwszName THEN
   MessageBoxW(0, *pwszName, "IFileOpenDialog", MB_OK)
   CoTaskMemFree(pwszName)
END IF

' // Uninitialize the COM library
CoUninitialize

Sorry for my poor knowledge regarding win32 api,I am learning.

It works now,except when choosing a file which name containes asain charaters,printing the name in console displayed verlapped.
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: Call win32 api to OpenFileDialog question

Post by PaulSquires »

PeterHu wrote: Oct 06, 2022 15:03 It works now,except when choosing a file which name containes asain charaters,printing the name in console displayed verlapped.
I remember seeing many threads here on this forum about problems displaying unicode characters on the standard Windows terminal output screen. Something to do with the Windows locality and the Codepages. I have never had to use such a setup so someone more knowledgeable than me on the subject might be able to provide better guidance.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Call win32 api to OpenFileDialog question

Post by dodicat »

Try this PeterWu (your code with strings only)

Code: Select all

 
shell "chcp 936" 
DIM cws AS string = "一个"
DIM s AS STRING = "三个"
cws = cws + " 两个 " + s
PRINT cws

sleep

print !"\n\n\nplain string version----\n"
dim str1 as string ="一个"
dim str2 as string ="两个"
dim str3 as string ="三个"

str1+=str2+str3
print str1


print !"\n\nPress any key to continue..."
sleep  
You ide settings GB2312, or compile straight from the commandline.
PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Re: Call win32 api to OpenFileDialog question

Post by PeterHu »

dodicat wrote: Oct 06, 2022 18:48 Try this PeterWu (your code with strings only)

Code: Select all

 
shell "chcp 936" 
DIM cws AS string = "一个"
DIM s AS STRING = "三个"
cws = cws + " 两个 " + s
PRINT cws

sleep

print !"\n\n\nplain string version----\n"
dim str1 as string ="一个"
dim str2 as string ="两个"
dim str3 as string ="三个"

str1+=str2+str3
print str1


print !"\n\nPress any key to continue..."
sleep  
You ide settings GB2312, or compile straight from the commandline.
This works as well,either cp936(default cp) or ansi string.In my Win10 only ansi string works for all cases,not wstring,UTFs.Although I doubt this,it seems I have no other options at this moment.
Post Reply