Winapi declarations in FB are done like in VB?

New to FreeBASIC? Post your questions here.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Winapi declarations in FB are done like in VB?

Post by Tourist Trap »

Hello,

it can look a quite silly question but we don't have to 'import' explicitly the winapi stuff in general. This is done already thanks to the maintainers of "windows.bi" and so on.

In VB, or VBA, for example, to use GetClipboardData we need to write this:

Code: Select all

declare function GetClipboardData lib "user32" alias "GetClipboardDataA" (byval wFormat as long) as long
In FB , as said, a simple #include "windows.bi" will do the job.

So my question is simply this, could I use directly and exactly the VB syntax if for example I'm not finding my winapi function in "windows.bi"? Or is there some other syntax or alternative in the context of FB?

Thanks for the attention !
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winapi declarations in FB are done like in VB?

Post by dodicat »

Hi TT
Seems OK here, loading only three things.

Code: Select all

  


declare function GetClipboardData lib "user32" alias "GetClipboardData" (byval wFormat as long) as any ptr
declare function openclipboard lib "user32" alias "OpenClipboard" (byval as any ptr) as boolean
declare function closeclipboard lib "user32" alias "CloseClipboard"() as boolean



dim as any ptr p
openclipboard(0)
p= GetClipboardData(1)  '1=text
closeclipboard
print *cast(zstring ptr,p)


sleep

 
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Winapi declarations in FB are done like in VB?

Post by Tourist Trap »

dodicat wrote:Hi TT
Seems OK here, loading only three things.
Thanks for trying dodi!
The story is that I have to play with some vba stuff those days and there is no include things around there. So I was wondering if the way VBA imports the functions from the api was not also reusable in FB. The advantage is that whatever winapi function you learnt to use in vba, you can then bring home in FB later ;)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winapi declarations in FB are done like in VB?

Post by dodicat »

Your declared function is nearly exactly re-usable.
The difference is you have
alias "GetClipboardDataA"
(The A at the end)
This method of retrieving functions from a .dll is perfectly OK in FreeBASIC.

Have a look at the winapi includes.

Do this by
windows.bas
compile this file with -pp option.
(fbc -pp windows.bas)

Code: Select all

'' windows.bas
#include "windows.bi"
 
And search windows.pp.bas for functions.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Winapi declarations in FB are done like in VB?

Post by Tourist Trap »

dodicat wrote: ...
And search windows.pp.bas for functions.
Thanks again, very nice tip! ;)
(why append this A is still quite mysterious)
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Winapi declarations in FB are done like in VB?

Post by bcohio2001 »

The "A" stands for ASCII.
If you look at some of the .bi files there are 2 definitions.
GetClipboardDataA and GetClipboardDataW. (Which stands for "Wide String")
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winapi declarations in FB are done like in VB?

Post by dodicat »

bcohio2001 wrote:The "A" stands for ASCII.
If you look at some of the .bi files there are 2 definitions.
GetClipboardDataA and GetClipboardDataW. (Which stands for "Wide String")
I cannot find GetClipboardDataA or GetClipboardDataW amongst the .bi files.
Only GetClipboardData.
(Perhaps you are using it as an artificial example for ascii and wide strings?)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Winapi declarations in FB are done like in VB?

Post by MrSwiss »

dodicat wrote:I cannot find GetClipboardDataA or GetClipboardDataW amongst the .bi files.
Don't know about .bi files, however, on MSDN it states:
- GetClipboardDataA (ANSI)
- GetClipboardDataW (Unicode)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winapi declarations in FB are done like in VB?

Post by dodicat »

Thanks Mr Swiss.
But I cannot find these on msdn.
Could you link the page.
I only see GetClipboardData in the .def file of user32.dll.

...
...
GetClassWord
GetClientRect
GetClipCursor
GetClipboardAccessToken
GetClipboardData
GetClipboardFormatNameA
GetClipboardFormatNameW
GetClipboardOwner
GetClipboardSequenceNumber
...
...
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Winapi declarations in FB are done like in VB?

Post by MrSwiss »

MSDN wrote:Windows Data Types for Strings
Found it yesterday somewhere on MSDN, can't remember the exact location.
( the A or W ending, seems to apply, whenever a String is returned by a API-Function )
Very general, not directed at the point in question (Strings handling in WIN).
Link: Windows Data Types for Strings

See below (your code) with some added comment ...

Code: Select all

p= GetClipboardData(1)  ' 1=text (ANSI) 13=text (Unicode)
"Clipboard Standard Formats" details ...
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winapi declarations in FB are done like in VB?

Post by dodicat »

Yes, the magic number is 13 for unicode.
Thanks Mr Swiss.
On the first notepad, select and copy then close notepad.
The next notepad will be the unicode copy from clipboard.

Code: Select all

 

declare function GetClipboardData lib "user32" alias "GetClipboardData" (byval wFormat as long) as any ptr
declare function openclipboard lib "user32" alias "OpenClipboard" (byval as any ptr) as boolean
declare function closeclipboard lib "user32" alias "CloseClipboard"() as boolean

Sub savefileUTF(filename As String,p As wstring)
    Dim As Long n=Freefile
    Open filename For Output Encoding "utf16" As #n
    Print #n,p
    Close #n
End Sub


function getclipboard() as string
  openclipboard(0)  
   dim as any ptr p= GetClipboardData(1)  '1=text
closeclipboard
return *cast(zstring ptr,p)
end function

function getclipboardW() as wstring ptr
  openclipboard(0)  
   dim as any ptr p= GetClipboardData(13)  '13=unicodetext
closeclipboard
return cast(wstring ptr,p)
end function

Dim As Wstring * 3500 h
h="SELECT ALL AND COPY THIS FILE THEN CLOSE NOTEPAD"+chr(13,10)
h+="START "
For n As Long=2000 To 3500
    h+=Wchr(Val("&h"+Str(n)))+" "
    if n mod 30=0 then h+=wchr(13,10)
Next
h+="END"
SavefileuTF("Unicode.txt",h)
shell "notepad Unicode.txt"

dim as wstring ptr w=getclipboardW
*w=ltrim(*w,"SELECT ALL AND COPY THIS FILE THEN CLOSE NOTEPAD")
*w="THIS IS FROM THE CLIPBOARD" +Chr(13,10) + *w
SavefileUTF("unicodefromclipboard.txt",*w)
shell "notepad unicodefromclipboard.txt"
sleep
kill "Unicode.txt"
kill "unicodefromclipboard.txt"



  
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Winapi declarations in FB are done like in VB?

Post by jj2007 »

MrSwiss wrote:
dodicat wrote:I cannot find GetClipboardDataA or GetClipboardDataW amongst the .bi files.
Don't know about .bi files, however, on MSDN it states:
- GetClipboardDataA (ANSI)
- GetClipboardDataW (Unicode)
GetClipboardData does not have that Ansi/Unicode distinction, simply because it is not limited to strings:
GetClipboardData(CF_TEXT)
GetClipboardData(CF_UNICODETEXT)
GetClipboardData(CF_BITMAP)
etc - common formats are listed in WinUser.h, e.g.

Code: Select all

#define CF_TEXT             1
#define CF_BITMAP           2
#define CF_METAFILEPICT     3
#define CF_SYLK             4
#define CF_DIF              5
#define CF_TIFF             6
#define CF_OEMTEXT          7
#define CF_DIB              8
#define CF_PALETTE          9
#define CF_PENDATA          10
#define CF_RIFF             11
#define CF_WAVE             12
#define CF_UNICODETEXT      13
#define CF_ENHMETAFILE      14
#define CF_HDROP            15
#define CF_LOCALE           16
#define CF_DIBV5            17
...
#define CF_OWNERDISPLAY     0x0080
#define CF_DSPTEXT          0x0081
#define CF_DSPBITMAP        0x0082
#define CF_DSPMETAFILEPICT  0x0083
#define CF_DSPENHMETAFILE   0x008E
There are also private formats (0x0200 ... 0x02FF) and GDIOBJ formats (0x0300 ... 0x03FF). The latter allow to pass Gdi objects to the clipboard, which, at the moment when the clipboard gets cleared by another application, get eliminated correctly by the OS using DeleteObject.
bcohio2001
Posts: 556
Joined: Mar 10, 2007 15:44
Location: Ohio, USA
Contact:

Re: Winapi declarations in FB are done like in VB?

Post by bcohio2001 »

dodicat wrote:(Perhaps you are using it as an artificial example for ascii and wide strings?)
Yes, was just using as example. May be should have just said: SomeFunctionA and SomeFunctionW.
Sorry for the confusion.
Post Reply