Smart lib Import ? or improved function check?

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Smart lib Import ? or improved function check?

Post by Rescator »

Not sure how to explain this but let me use this example:

(Yeah I know, I left out the Protected SHGetFolderPath_.SHGetFolderPath_ from the "examples" below.)

Code: Select all

Prototype.i SHGetFolderPath_(hwndOwner.i,nFolder.l,hToken.i,dwFlags.l,*pszPath)
dll=OpenLibrary(#PB_Any,"shell32.dll")
If dll
		SHGetFolderPath_=GetFunction(dll,"SHGetFolderPathW")
		If SHGetFolderPath_
				result=#True
		EndIf
		If result
				;SHGetFolderPath available, let's do stuff using SHGetFolderPath() now.
		EndIf
EndIf
but imagine if we could do this:

Code: Select all

ImportS "shell32.lib"
		SHGetFolderPath.i(hwndOwner.i,nFolder.l,hToken.i,dwFlags.l,*pszPath)
EndImport

If IsFunction(SHGetFolderPath())
	;SHGetFolderPath available, let's do stuff now.
Else
 ;SHGetFolderPath not available, let's do it some other way now.
EndIf
A lot cleaner and simpler right? It does exactly the same as the first code
IsFunction() would open the lib shell32.dll if not already open, check if the function exist, and if it does, returns true.
ImportS would import the lib and specified function but not try to load at start like Import or ImportC would,
the benefit would be that the program would start even if shell32.dll was not available,
and IsFunction() would return false for those functions obviously.

Fred, Freak, is it possible to do something like this or similar at all?

Or even something like

Code: Select all

Prototype.i SHGetFolderPath_(hwndOwner.i,nFolder.l,hToken.i,dwFlags.l,*pszPath)
If IsFunction("shell32.dll",SHGetFolderPath())
	;SHGetFolderPath available, let's do stuff now.
Else
 ;SHGetFolderPath not available, let's do it some other way now.
EndIf
You know, just some way of simplifying this.

Myself I've begun to rely less and less on OS version etc. And instead just try to open the dll check if the function exist
and act on that instead, it's more future proof that way, and unlike Import you can inform the user if a dll or function that you need is missing more easily.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Smart lib Import ? or improved function check?

Post by Rescator »

Here is the easiest way to do what I mean using current PureBasic.

Code: Select all

EnableExplicit

Define dll.i
Prototype.i SHGetFolderPath_(hwndOwner.i,nFolder.l,hToken.i,dwFlags.l,*pszPath)
Global SHGetFolderPath_.SHGetFolderPath_
dll=OpenLibrary(#PB_Any,"shell32.dll")
If dll
		SHGetFolderPath_=GetFunction(dll,"SHGetFolderPathW")
EndIf

;Later in the source.

If SHGetFolderPath_
		SHGetFolderPath_() ;SHGetFolderPath available, let's do stuff now.
Else
 ;woops, not available, let's do it some other way.
EndIf
What I'm basically asking for is any way to simplify this further? The dll.i prototype, global and openlibrary and getfunction can get a bit messy once you get more than a handful of these :)
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Smart lib Import ? or improved function check?

Post by Rescator »

More brainstorming.
What if something similar to this was possible? (yeah I know, it kinda looks messy, but you get the idea I hope)

Code: Select all

Global Prototype.i SHGetFolderPath_("shell32.dll","SHGetFolderPathW").SHGetFolderPath_(hwndOwner.i,nFolder.l,hToken.i,dwFlags.l,*pszPath)

;Later in the source.
If SHGetFolderPath_
		SHGetFolderPath_() ;SHGetFolderPath available, let's do stuff now.
Else
 ;woops, not available, let's do it some other way.
EndIf
Post Reply