Solved

How to receive the filename send by other application

Posted on 1998-09-16
10
136 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magic Software info 18 154
How to create virtual printer while installing my application 5 84
Delphi: making a BW image transparent 10 137
scroll down TListBox component in Delphi 1 44
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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