Should I explicitly delete object with custom format from clipboard? - windows

I have following question - should I delete explicitly object placed in windows clipboard? What does EmptyClipboard function in such case? Global object was created via GlobalAlloc function, and has custom format (which was registered via RegisterClipboardFormat function). I did not find section which says about such aspect

No, once you place the object on the clipboard, the clipboard is responsible for destroying it. You allocated the memory with GlobalAlloc, as required, and the system is therefore able to deallocate the memory using GlobalFree. Which is does when it needs to.

Related

Is it valid to change lpstrFile on CDN_FILEOK via Hook on GetOpenFileName/GetSaveFileName?

Is it valid to change the contents of lpstrFile on CDN_FILEOK via Hook on GetOpenFileName()/GetSaveFileName()?
The reason is that I may need to append additional information to the file name for GetSaveFileName(). The user could enter a generic name like "my file" and the program handles adding information user wouldn't know to add (already part of filter). Then it would check for overwrite and put up a question if okay to overwrite. If not, I can just use the custom data field, but I couldn't find any reference if it's allowed to change the contents of lpstrFile
TIA!!
Depends on your definition of valid. The designers of this API did not intend for you to do this but if it works, it works™. These dialogs will probably not change their internal design ever again since Microsoft have a fair amount of application compatibility to worry about in this area and these dialogs were replaced by the COM based API in Vista+.
My recommendation would be to store the address of your buffer in lCustData as well before calling the Open/Save function and only modify that buffer. The dialog could in theory use its own buffer in some places and there might not be space for you to write anything in those and there is no way to figure out their size.
In practice, there seems to be no problem to play with the buffer behind Windows' back. In the past I have even replaced the actual buffer address with a new bigger memory block when implementing support for multi-file-select.

Why does Windows have no DeleteConditionVariable() function to go together with InitializeConditionVariable()?

I'm trying out Windows support for Condition Variables today (as provided by Microsoft for Windows Vista and later). To initialize a condition variable, I call InitializeConditionVariable(), which is straightforward enough, but I don't see any way provided to destroy the condition variable when I'm done using it. Why is there no DeleteConditionVariable() function?
(I'd expect the API to be analogous to the existing CreateCriticalSection() / DestroyCriticalSection() API)
A conditional variable is a very light-weight object that is internally based on a single global kernel keyed event object that is always available through every process's entire lifetime. The conditional variable simply contains a pointer to that object. So there is nothing that needs to be freed explicitly, thus no delete function is needed.

DELPHI: Store a some data in a shared memory to have an access from several apps

Problem defitintion
I have two applications: First and Second. The First is mine, the Second is developed by the outsource.
1) At a certain time I need to send a message from the First app to the Second, to make Second one visible and maximized, after it has been minimized.
2) In order to do the (1) step, Second app should store its handle somewhere in a shared memory, which could be accessed by the name or by whatever it might be (like mutexes do).
Question
So, what is the better option to store data (a handle) in an operating memory?
If you want to store this information in a shared place, then you would typically use shared memory. In Windows terms that's a file mapping object. Create one by calling CreateFileMapping. These are kernel objects and so can be named in the kernel namespace.
File mappings are not a whole lot of fun to work with, so you might like to find an easier solution. Give your application's main form a unique class name. For instance you might name the form's class TMyCompanyNameMyProductNameMainForm. Then call FindWindow passing that class name to find an existing application window.

should i free the memory i copied to clipboard?

When I copy data in my win32 program to the clipbord, should i free the memory i copied to clipboard, after I paste it elsewhere? or the system is responsible for this.
There are two ways of putting data on the clipboard.
Method 1: Put the data onto the clipboard directly, by calling SetClipboardData and passing a non-NULL value as the second parameter. In that case, the system will take responsibility for the data, and you should not free it yourself.
Method 2: Put a placeholder onto the clipboard, by calling SetClipboardData and passing NULL as the second parameter. In that case, the application is responsible for the data until the point it calls SetClibpoardData with a non-NULL second parameter, at which point responsibility transfers to the operating system.
It's not clear from your question which method you are using.
Read the documentation:
If SetClipboardData succeeds, the system owns the object identified by the hMem parameter. The application may not write to or free the data once ownership has been transferred to the system
Keeping track of the clipboard data so you can remove it from the clipboard when closing your app is completely optional. Once the data is on the clipboard, the system owns it and it is separate from your app, so you can choose to leave it on the clipboard so it remains available for continued usage after your app is closed. Unless you are using delayed rendering, that ism in which case it does make sense to remove it from the clipboard when closing your app, since your app will not be running anymore to render the data when requested by other apps.
Your application is responsible for handling data on the clipboard, if it put it there. This is why a lot of applications, like Microsoft Office, ask you if you want to keep a large amount of data on the clipboard or not, when you exit the application.
I would strongly advise user-interaction however, since you do not know if the user needs the data on the clipboard somewhere else later.
It is generally done by the system however, some responsible applications also take care of asking the user to free up the clipboard before leaving.
e.g. MSWord will ask the user to keep the data in memory or not before it quits.
For general purpose, you can leave it to system.
Remember here that user might want to keep it in Clipboard (Even after closing application) so you should not mangle with it and remove it from memory.

Windows API and Passing Values

I know that we can pass parameters to API functions in three types of Passing by Value, Passing Indirectly and Passing by Reference.
My question is about Indirect mode; can we change address of allocated memory space on demand or that's done by windows in some restricted area which is owned by windows core?
In other words can we tell windows upon structure creation time to make and store needed structure in a memory area that we specified?
How about passing by Reference? if we call the API function which is accept parameters by reference, does windows places and keeps structures in the same memory area on each call or not?

Resources