Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 142
  • Last Modified:

How to receive the filename send by other application

I have want my application can open a kind of file which
extension filename is .abc.

Now, through read the command line parameters, my program
can receive the filename and open it.

But how can i received the filename when i select a .abc file and open it with my application which is running already?

Thanks for any helping.

Ye Chen
0
yc
Asked:
yc
  • 5
  • 5
1 Solution
 
MadshiCommented:
yc,

I'm not sure what you're meaning.

(1) Do you want to register your application so that windows calls in whenever an "*.abc" file is double clicked?
(2) Or do you want to avoid your application from being loaded two times?
(3) Or do you want another thing that I can't imagine? Please describe some more details.

For (1) do the following:
Add a key named "HKEY_CLASSES_ROOT\.abc" to registry. Then change the "(Default)" value of this key to "MyAbcFile".
Then add the keys "HKEY_CLASSES_ROOT\MyAbcFile\shell\open\command". Then change the "(Default)" value of the "command" key to "C:\PathOfYourApp\YourApp.Exe "%1"" (with quotes only around %1).

If you mean (2) or (3), write again. Then I'll provide help for that.

Regards, Madshi.
0
 
ycAuthor Commented:
Hi, Madshi

I am glad to receive your answer so fast.

I had done the working of (1) already.

(2) Your writing is I need.


Ye Chen
0
 
MadshiCommented:
My suggestion:

const CMsg_ReceiveFileName = WM_USER+777;
      CYCsRegPath          = 'Software\YCsGreatApp\';

type
  TForm1 = class(TForm)
    ...
  private
    ...
    procedure ReceiveFileName(var Message: TMessage); message CMsg_ReceiveFileName;
  end;

// function you have to fill:
procedure ShowFile(fileName: string);
function RegRdCard(key: HKEY; path: string; var value: cardinal) : boolean; // use TRegistry component
function RegWrCard(key: HKEY; path: string; value: cardinal) : boolean;
function RegRdStr(key: HKEY; path: string; var value: string) : boolean;
function RegWrStr(key: HKEY; path: string; value: string) : boolean;
function RegDelKey(key: HKEY; path: string) : boolean;

procedure TForm1.ReceiveFileName(var Message: TMessage);
var s1 : string;
begin
  if (Message.wParam=777) and (Message.lParam=777) and
     RegRdStr(HKEY_LOCAL_MACHINE,CYCsRegPath+'fileName',s1) then begin
    RegDelVal(HKEY_LOCAL_MACHINE,CYCsRegPath+'fileName');
    ShowFile(s1);
  end;
end;

procedure TForm1.Create(Sender: TObject);
var wh : cardinal;
    s1 : string;
begin
  s1:=GetFileFromParameters;
  if RegRdCard(HKEY_LOCAL_MACHINE,CYCsRegPath+'windowHandle',wh) and IsWindow(wh) then begin
    RegWrStr(HKEY_LOCAL_MACHINE,CYCsRegPath+'fileName',s1);
    PostMessage(wh,CMsg_ReceiveFileName,777,777);
    ExitProcess(0); // exit; is not the first instance...
  end;
  RegWrCard(HKEY_LOCAL_MACHINE,CYCsRegPath+'windowHandle',handle);
  ShowFile(s1);
end;

procedure TForm1.Destroy(Sender: TObject);
begin
  RegDelVal(HKEY_LOCAL_MACHINE,CYCsRegPath+'windowHandle');
end;

Regards, Madshi.

P.S: Instead of transfering the fileName via registry and notifying the first application with PostMessage you could use SendMessage(WM_COPYDATA,...). Look at WM_COPYDATA. It's a little bit more difficult to program but perhaps the better way?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ycAuthor Commented:
Hi, Madshi

The code you give me is excellent!

But i still have two little problem before ending the question.

a) How to set the application first loading as the topmost window on the desktop above all other applications?

b) Is it possible to use the LParam in PostMessage() to send the filename but not use registry?

Thanks again.

Ye Chen

0
 
MadshiCommented:
Ye,

(a) Use SetForegroundWindow(wh).
(b) No. You'll get an access violation if you simply transmit a pointer to a string, because then the other application would try to access "alien" memory. But Microsoft implemented the WM_COPYDATA message that is quite useful for directly transfering memory blocks between applications. Look at help for WM_COPYDATA.

Regards, Madshi.
0
 
ycAuthor Commented:
Hi, Madshi

I found SetForegroundWindow(wh) must run before the postmesage().

A new problem is appear:

When i click try to open the EXAMPLE.abc file inserted in a E-mail file in outlook98, if my application is already running,
after the second running program postmessage to the first and then closed. the first program can not open the file, it said
"File EXAMPLE.abc is not exists", i found outlook put the EXAMPLE.abc in the c:\windows98\temp, when second program closed,
the EXAMPLE.abc is deleted!

If my application is not running, when click EXAMPLE.abc, my program can open EXAMPLE.abc, but the attribute of c:\windows98\temp\EXAMPLE.abc is readonly and is in there already.

Thanks

Ye Chen

0
 
MadshiCommented:
Perhaps the second instance of your program should copy that file to another location (win API CopyFile)?
Or it should copy the file to another name in temp directory and then posting not 777/777, but 777/778, so the first instance of your program knows that it has to delete the file after closing...

Regards, Madshi.
0
 
ycAuthor Commented:
Hi, Madshi

I had use sendmessage() to instead the postmessage().

Now, All is ok|

Thanks very much.

Ye Chen
0
 
ycAuthor Commented:
Hi, Madshi

I had use sendmessage() to instead the postmessage().

Now, All is ok|

Thanks very much.
0
 
MadshiCommented:
Yes, I should have had this idea, too...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now