Solved

How to receive the filename send by other application

Posted on 1998-09-16
10
129 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now