LB Booster
Programming >> Liberty BASIC language >> Associating files?
http://lbb.conforums.com/index.cgi?board=lblang&action=display&num=1431354682

Associating files?
Post by datwill on May 11th, 2015, 2:31pm

I know people on the normal LB forum would be angry a million times over if they asked me this question (EDIT: OPPPPS grin grin if I asked this question to them!), and if it makes people here mad too, I'm sorry sad but how do you associate files with certain applications with code?
Re: Associating files?
Post by Richard Russell on May 11th, 2015, 5:13pm

on May 11th, 2015, 2:31pm, Daniel Atwill wrote:
how do you associate files with certain applications with code?

You are quite right that the LB Community Forum forbids the discussion of Registry access (ridiculous, in my opinion, since it is no more 'dangerous' than many other things that are permitted there).

The code below (LBB only, although it could easily be adapted to run in LB) associates the extension specified as Extension$ (don't forget the dot) with the command Command$. The program assumes that the name of the file is passed as a simple command-line parameter to the application.

Naturally, this program will only work if it is 'Run As Administrator', and since it can change the association of an existing file type it should be used with care:

Code:
    Extension$ = ".xyz"
    ProgID$ = "xyzfile"
    Command$ = CHR$(34) + "C:\Temp\MyProg.exe" + CHR$(34) + " %1"

    ProgIDLen = LEN(ProgID$) + 1
    CommandLen = LEN(Command$) + 1

    STRUCT key, handle AS ULONG

    CALLDLL #advapi32, "RegCreateKeyExA", _HKEY_CLASSES_ROOT AS ULONG, _
      Extension$ AS PTR, 0 AS LONG, "" AS PTR, 0 AS LONG, _
      _KEY_WRITE AS LONG, 0 AS LONG, key AS STRUCT, _
      0 AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegCreateKeyEx failed (ProgID)" : END

    CALLDLL #advapi32, "RegSetValueExA", key.handle.struct AS ULONG, _
      "" AS PTR, 0 AS LONG, _REG_SZ AS LONG, _
      ProgID$ AS PTR, ProgIDLen AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegSetValueEx failed (ProgID)" : END

    CALLDLL #advapi32, "RegCloseKey", key.handle.struct AS ULONG, _
      ret AS LONG

    CALLDLL #advapi32, "RegCreateKeyExA", _HKEY_CLASSES_ROOT AS ULONG, _
      ProgID$ + "\shell\Open\command" AS PTR, 0 AS LONG, "" AS PTR, _
      0 AS LONG, _KEY_WRITE AS LONG, 0 AS LONG, key AS STRUCT, _
      0 AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegCreateKeyEx failed (Command)" : END

    CALLDLL #advapi32, "RegSetValueExA", key.handle.struct AS ULONG, _
      "" AS PTR, 0 AS LONG, _REG_SZ	AS LONG, _
      Command$ AS PTR, CommandLen AS LONG, ret AS LONG
    IF ret THEN NOTICE "RegSetValueEx failed (Command)" : END

    CALLDLL #advapi32, "RegCloseKey", key.handle.struct AS ULONG, _
      ret AS LONG 

Richard.
Re: Associating files?
Post by datwill on May 11th, 2015, 6:56pm

on May 11th, 2015, 5:13pm, Richard Russell wrote:
You are quite right that the LB Community Forum forbids the discussion of Registry access (ridiculous, in my opinion, since it is no more 'dangerous' than many other things that are permitted there).


I've heard you say that before! wink laugh
And thanks a lot for the code and I will take a look at it right away!
Re: Associating files?
Post by datwill on May 11th, 2015, 7:27pm

It doesn't seem to be working sad The first function returns an error cry
Re: Associating files?
Post by RobM on May 11th, 2015, 11:11pm

Did you try running it as an admin as Richard said you must do? If not you will get an 'access denied' error.
Re: Associating files?
Post by datwill on May 12th, 2015, 08:47am

I know, I just remembered last night as I was going to bed, I'll make an app and run as admin (even though I am one on my own computer...)
Re: Associating files?
Post by Richard Russell on May 12th, 2015, 09:28am

on May 12th, 2015, 08:47am, Daniel Atwill wrote:
I'll make an app and run as admin

You don't have to make an EXE to test it - you can run LBB 'as administrator' and do it that way (although it would be wise to quit LBB as soon as you have finished testing so you don't leave it running with admin privileges for too long).

Richard.

Re: Associating files?
Post by datwill on May 12th, 2015, 10:32am

OK!
Also, I've tried to look up ways of finding out a users status (i.e. Admin or not), and I got as far as finding a user name... I'll keep looking in MSDN but do you know how to do this?

EDIT: Would GetTrusteeTypeA fit my need? MSDN
Quote:
The GetTrusteeType function retrieves the trustee type from the specified TRUSTEE structure. This value indicates whether the trustee is a user, a group, or the trustee type is unknown.

I've found a whole list of security API calls, but don't know which one to use huh
https://msdn.microsoft.com/en-us/library/windows/desktop/aa446674%28v=vs.85%29.aspx
Re: Associating files?
Post by Richard Russell on May 12th, 2015, 11:35am

on May 12th, 2015, 10:32am, Daniel Atwill wrote:
I've tried to look up ways of finding out a users status (i.e. Admin or not)

Code:
    CALLDLL #shell32, "IsUserAnAdmin", admin AS LONG
    IF admin THEN
      PRINT "User is an admin"
    ELSE
      PRINT "User is not an admin"
    END IF 

(The function is deprecated, but seems to work in Windows 8.1. It is not available in Windows 2000.)

Richard.
Re: Associating files?
Post by datwill on May 12th, 2015, 4:03pm

Wow, I didn't know it was that easy...
embarassed I'm a bit ashamed actually...
Thanks