Home » Questions » Using API to pass a filename to an app already running

0

0

Hello all,

This is one that I have been fooling around with for sometime and just can’t get it to work so I’m asking for help from the hardcore Delphi coderz.

I have been writing a player for sometime and the very last feature I want to add is being able to add a file to a playlist by just double clicking on it (My associations are setup correctly). If my application is not running I can easily do this by using cmdline and ParamStr.

The problem arises when I already have my player running and double click on a song to be added to the playlist, the ParamStr doesn’t change from the initial loading. So I need to find a way to do it and I have tried everything I can think of.

MS Media Player has this function, as do most media players, and I think it could be a good topic and of use to all of us as well.

Umm I know how to do this via VB, I could post the code here, perhaps someone could port it to Delphi for you, its mainly setting up a message hook and listening for messages from another instanance of the app.

I am going to assume that you mean so that, if your application is already running, and you douple click say an mp3, rather then run a new instance of your application it would simply pass the mp3 file to your already running application?

Anyhow if your looking for this let me know and I’ll post, as I’am not entirely sure what your looking for.

Ok here goes, applogies for the lenght but theres alot of code involved please read through the entire comments on this before you go abouts using it. By the way, look at the [b:30sfusbq]Form1.ComShell Cmd[/b:30sfusbq] Within the function [b:30sfusbq]WindowProc[/b:30sfusbq] This is what function to exectue when it recives a new command arg from the new application. You may change this to your liking.

How I would do it is for your application to detect whether another instace of that application is already running. There are various ways to do this, such as mutex, FindWindow, etc. If another instance is found, then it sends the filename to the instance that is already running (look up inter-process communication in the Win32 help file) and exits. In Delphi, this would all be done in the DPR file prior to the first call to CreateForm.

This is a good introduction. Sendmessage IPC is useful as long as your windows all have handles and you want to handle data synchronously with events (useless for service applications), and the messages are not huge datastructures.

Mapped memory is good for async data storage and sharing.

You could also use an atom, or named pipe, but I wouldn’t suggest it under winblows. Since atoms are not really ‘suggested use’ anymore, and named pipes can give you threading nightmares