Set/GetEnvironmentVariable()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post by Blue »

Thanks Danilo.
I didn't realize that using the Clipboard this way is a breach of Windows etiquette.
I just keep learning fun stuff from you ! :)

But is it actually a breach of etiquette if, in fact, appA is simply mimicking what the user HAS to do to get the data to appB ? In other words, the user would normally go through a Cntl-C /switch to appB/Cntl-V/switch back to A sequence, but appA, being the nice app that it is, provides a time-saving shortcut to do it (to say nothing of the years it adds to the user's finger life!). I suppose that, strictly speaking, the answer is 'Yeah', but in this particular instance, appA could be forgiven for actually saving the user a lot of keyboard gymnastics, shouldn't it ?

Anyway, facetiousness aside, i can see why that point makes sense and has to be taken into consideration.
So I'll add a large button that the user will have to press to explicitly authorize appA to enable this shortcut process, just to keep everything totally lawful and above board.

PS : Do you know of a smart memory-based alternative ?
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Set/GetEnvironmentVariable()

Post by ts-soft »

Blue wrote:PS : Do you know of a smart memory-based alternative ?

Code: Select all

DeclareModule FileMap
  Declare Create(Name.s, Size.i)
  Declare Open(Name.s)
  Declare Close(*Mem)
EndDeclareModule

Module FileMap
  EnableExplicit
  
  Global hMap.i
  
  Procedure Create(Name.s, Size.i)
    hMap = CreateFileMapping_(#INVALID_HANDLE_VALUE, 0, #PAGE_READWRITE | #SEC_COMMIT | #SEC_NOCACHE, 0, Size, @Name)
    If hMap
      ProcedureReturn MapViewOfFile_(hMap, #FILE_MAP_ALL_ACCESS, 0, 0, 0)
    EndIf    
  EndProcedure
  
  Procedure Open(Name.s)
    hMap = OpenFileMapping_(#FILE_MAP_ALL_ACCESS, 0, @Name)
    If hMap
      ProcedureReturn MapViewOfFile_(hMap, #FILE_MAP_ALL_ACCESS, 0, 0, 0)
    EndIf
  EndProcedure
  
  Procedure Close(*Mem)
    UnmapViewOfFile_(*Mem)
    CloseHandle_(hMap)  
  EndProcedure
  
EndModule
Other example:
http://www.purebasic.fr/german/viewtopi ... 12#p198712
http://www.purebasic.fr/english/viewtop ... 54#p335754
http://www.purebasic.fr/english/viewtop ... 65#p392065

Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Set/GetEnvironmentVariable()

Post by luis »

Just think if all programmers started to write programs which communicate between them using the clipboard on their own timing.
Every suite of these programs would corrupt the data for any other suite running and yours data too when you are trying to copy and paste something.
And you too would destroy their data.
Should be obvious enough it's a very bad idea without the need of documenting it :)
Last edited by luis on Sat Apr 04, 2015 10:22 pm, edited 1 time in total.
"Have you tried turning it off and on again ?"
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post by Blue »

ts-soft wrote:

Code: Select all

DeclareModule FileMap
  Declare Create(Name.s, Size.i)
  Declare Open(Name.s)
  Declare Close(*Mem)
EndDeclareModule
[...]
Greetings - Thomas
Hello ts-soft (I bet that stands for Thomas-so-soft, he ?)

Thanks for the suggested code.
I've seen the above before ( one of your postings on the German board, maybe ??),
but to my great embarassment, i couldn't figure out how to use it. :oops:
I remember reading it, not understanding it, and leaving with the notion that it is file-based (wrong interpretation of FileMap, most likely).

So allow me an additional request here : would you post a simple working example ? (2 lines or less would do fine...)

 
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Set/GetEnvironmentVariable()

Post by ts-soft »

No problem:
FileMap.pbi:

Code: Select all

DeclareModule FileMap
  Declare Create(Name.s, Size.i)
  Declare Open(Name.s)
  Declare Close(*Mem)
EndDeclareModule

Module FileMap
  EnableExplicit
 
  Global hMap.i
 
  Procedure Create(Name.s, Size.i)
    hMap = CreateFileMapping_(#INVALID_HANDLE_VALUE, 0, #PAGE_READWRITE | #SEC_COMMIT | #SEC_NOCACHE, 0, Size, @Name)
    If hMap
      ProcedureReturn MapViewOfFile_(hMap, #FILE_MAP_ALL_ACCESS, 0, 0, 0)
    EndIf   
  EndProcedure
 
  Procedure Open(Name.s)
    hMap = OpenFileMapping_(#FILE_MAP_ALL_ACCESS, 0, @Name)
    If hMap
      ProcedureReturn MapViewOfFile_(hMap, #FILE_MAP_ALL_ACCESS, 0, 0, 0)
    EndIf
  EndProcedure
 
  Procedure Close(*Mem)
    UnmapViewOfFile_(*Mem)
    CloseHandle_(hMap) 
  EndProcedure
 
EndModule
to start first:

Code: Select all

XIncludeFile "FileMap.pbi"

Define Mem
Mem = FileMap::Create("MyMem", 100)
PokeS(Mem, "Hello Program 2")

Delay(50000)
to start after first:

Code: Select all

XIncludeFile "FileMap.pbi"

Define Mem

Mem = FileMap::Open("MyMem")
If Mem
  Debug PeekS(Mem)
  FileMap::Close(Mem)
EndIf
ts-soft = Thomas Schulz - Software :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post by Blue »

luis wrote:Just think if all programmers started to write programs which communicate between them using the clipboard [...]
Should be obvious enough it's a very bad idea without the need of documenting it :)
Agreed. With the word obvious emphasized.

I didn't at all have in mind a generalized 2-way usage of the Clipboard as the in nightmare scenario you warn against.
I was more looking for something similar to what's done by certain applications that keep 'listening' to the clipboard for something that concerns them (think of download managers that grab URLs as they appear in the clipboard). They do not modify, nor corrupt the Clipboard, in any way, since they simply pick up specific data as it goes by.

But maybe even that can, possibly, somehow, be abused, although i can't think how right now.

To be specific, i follow the following scenario :
(1) appA provides a context menu entry that says "Send to clipboard" : that action conforms to the etiquette, as it is equivalent to the user typing Cntl-C in Windows.
(2) appA prefixes the data with a "series:" label, and sends it to the clipboard.
(3) appB, listening to the clipboard, grabs it, strips the "series:" prefix away and does with the rest of the string what it is expected to do. It never modifies the clipboard content in any way.

I don't think that sequence violates the etiquette, or deviates from the intended purpose of the clipboard, since users only find there what they put there, which they can use however they please.

In fact, the whole question simply boils down to this : is it OK for an app to listen to the clipboard ?
Agreed ?

   
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post by Blue »

ts-soft wrote: [...]
to start first:

Code: Select all

XIncludeFile "FileMap.pbi"

Define Mem
Mem = FileMap::Create("MyMem", 100)
PokeS(Mem, "Hello Program 2")

Delay(50000)
to start after first:

Code: Select all

XIncludeFile "FileMap.pbi"

Define Mem

Mem = FileMap::Open("MyMem")
If Mem
  Debug PeekS(Mem)
  FileMap::Close(Mem)
EndIf
ts-soft = Thomas Schulz - Software :wink:
Nah ! I still prefer Thomas-So-Soft... :wink:

Thanks for the examples. Without them, i would never have figured out that you had to use Poke and Peek to access the shared memory. Of course, now, pfttttt, it's sooooooooooo obvious. Gosh !

Plus, it works marvelously well, so much so that I think that the PB Gods should build that right into the language.

Thomas, start a new topic titled "Inter-process communication", with your code and examples, so that such useful stuff does not remain buried under a topic that's nowhere near related.

Many thanks.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Set/GetEnvironmentVariable()

Post by luis »

Blue wrote: Agreed. With the word obvious emphasized.
Oh sorry, it's hard to guess what it's obvious for others and what's not. It's true you start mentioning you wanted to initiate the copy after a specific user interaction but that was way late in the thread, and after Danilo mentioned it specifically. Maybe I misunderstood.
Let's left it there to the benefit of someone else, I'm sure he's out there.
Blue wrote: In fact, the whole question simply boils down to this : is it OK for an app to listen to the clipboard ?
Agreed ?

Yes, it's just more risky since it can always misinterpret the data there as directed to itself when it's not, compared to the full human-controlled cycle of copy (I know I'm copy certain data to the clipboard) and paste (I know the program I'm pasting to is built to process this data). The human is expecting the receiving program to work on that data. It's telling that clearly by initiating the pasting operation.
The url sniffer you mentioned is an acceptable example of the listeners family since it does rely on a standardized pattern (http://, https://) which *should* be followed by url. But sometimes it doesn't, like in this paragraph.

In short:

When the user oversee the entire cycle he takes the responsibility of routing valid data in and out *and* arbitrate the way this happens avoiding conflicts between multiple programs running.
The original idea, where nothing bad happens unexpectedly and you know exactly what's happening step by step.
He acts like a human mutex of sort.
The destination application can still reject the pasted data if it wants.

When a software listen and grab what it *thinks* it's of interested to itself and does not alter the clipboard it takes part of this responsibility and can damage just itself by producing garbage (garbage in -> garbage out) if it's wrong.
So no harm done to others.

My nightmare-ish scenario is hell on Earth.
It's with good reason there are API on every OS dedicated to various way to do inter-process communication: files, shared memory, pipes, messages, sockets, etc. etc.
Blue wrote: I would never have figured out that you had to use Poke and Peek to access the shared memory. Of course, now, pfttttt, it's sooooooooooo obvious. Gosh !
Agreed. With the word obvious emphasized.

See ? It's hard to guess it right. :)
"Have you tried turning it off and on again ?"
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Set/GetEnvironmentVariable()

Post by Blue »

luis wrote:
Blue wrote: Agreed. With the word obvious emphasized.
Oh sorry, it's hard to guess what it's obvious for others and what's not. [...]

Agreed. With the word obvious emphasized.

See ? It's hard to guess it right. :)
As usual, solid and interesting feedback from you, Luis.

But it got off on the wrong foot : my line "With the word obvious emphasized." wasn't a sarcastic dig at you at all. It was to be taken at face value, as in "i agree with you that it stands so much to reason (emphasis by me on 'so much') that it doesn't require much of a debate"

Funny how words alone, without the usual accompanying facial expressions and hand choreography, can deviate from their meaning!

In clear : NO disparaging NOR pique were ever intended towards your reply. :shock:
And, by the same token, no need for you to be sorry for anything. I never perceived any pique in your reply.

Same with my message to Thomas-So-Soft : i was only laughing at myself for not having understood the now obvious (oh! that misleading word...) necessity to use Pokes and Peeks to address raw memory cells. I was pointing out how, sometimes, i get pretty dense... :oops: , and, again, NOT trying to get back at you. :wink:
Although i can see how it could be interpreted that way.

What we need is a video forum. (Fred, are you listening ?)



   
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Post Reply