Solved

How to receive the filename send by other application

Posted on 1998-09-16
10
131 Views
Last Modified: 2010-04-06
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
Comment
Question by:yc
  • 5
  • 5
10 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1339907
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
 

Author Comment

by:yc
ID: 1339908
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
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1339909
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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

Author Comment

by:yc
ID: 1339910
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1339911
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
 

Author Comment

by:yc
ID: 1339912
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1339913
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
 

Author Comment

by:yc
ID: 1339914
Hi, Madshi

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

Now, All is ok|

Thanks very much.

Ye Chen
0
 

Author Comment

by:yc
ID: 1339915
Hi, Madshi

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

Now, All is ok|

Thanks very much.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1339916
Yes, I should have had this idea, too...
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question