如何让已驻留内存中的程序再次获得参数并运行?(100分)

  • 主题发起人 主题发起人 hyby
  • 开始时间 开始时间
H

hyby

Unregistered / Unconfirmed
GUEST, unregistred user!
我编了个Flash播放器,并让它设为默认的Flash播放器,在资源管理器中双击Flash文件时,<br>程序会自动启动,并在启动时检测内存中是否已有启动好的程序:<br>  1、若无则顺序启动,并自动运行指定的那个Flash文件;<br>  2、若有,则不再启动,仅传送点中的那个文件名给已启动的程序,然后让那个驻留的<br>程序播放这个文件。<br>  第1点好办,已实现,可第2点不知怎么办,请哪位大侠多多指教!
 
&nbsp; &nbsp;换一个思路,比如检查到已经装载后,把原来的窗体给关闭,然后重新打开加载新的<br>文件。或者在硬盘或注册表中建立一个文件或键掷,播放时对它进行比较,如果文件名改变,则<br>重新加载。<br>&nbsp; &nbsp; 第二种方法应该快一些。当然你其实还可以用一些高级的方法,比如SendMessage对原<br>窗体发送消息,在程序中接收等等。
 
我的想法就是用API(好象也只能用API),但我不知怎么用,用什么?<br>我用了ShowWindow(Application.Handle, SW_SHOW),但没有任何反应,<br>想用ShowMain(Application.Handle,'','',SW_SHOW),但不知该在uses处加入哪个单元?!<br>不知有哪位懂API的帮帮忙忙!!!<br>另:有知道的能否教一教如何查找一个API函数所在的uses单元?
 
为什么这么久都没人答出来?我看到不少关于PostMessage和SendMessage的贴子,但没有<br>发现一个关于使用它们发送和接收字符串传送的(大部分都只是发送,没看到如何接收)。<br>真的没人知道如何将选中的文件名传给一个驻留程序并处理它吗?这样的程序其实很多的。
 
FindWindow找到你需要的窗口,发送WM_COPYDATA消息<br>对方需要处理WM_COPYDATA消息,就可以得到传过来的数据
 
to Pipi.:<br>能具体的写两行代码吗?如何发送与接收的。<br>发送是否为postmessage(handle,wm_copydata,integer(Pchar(paramstr(1))),0)?
 
pipi说得对,<br>陈宽达的《Delphi深度历险》就是这么说的,<br>我因为没有实际用过,记得不清楚了(书不在这),你可以找来看看[:)]
 
用事件来进行同步操作。<br><br>先创建一个事件<br>然后再程序中用WaitForSingleObject等待该事件。<br>然后写一个Shell的扩展,在双击flash文件的时候SetEven,<br>这样你的服务进程就可以就这执行了.
 
To Pipi,独孤求败,kfzd:<br>实在不好意思,API我用得很少,手头也没有相关书籍,所以各位能否写点具体的、<br>有效的代码?
 
&gt;另:有知道的能否教一教如何查找一个API函数所在的uses单元?<br>这简单,只要在delphi的win32 help中查到相应的声明即可。<br>这些其实是ms的东东。然后找到相应的头文件(include file),把它加入uses即可。<br><br>另外,不同程序之间用postmessage和sendmessage传指针无效,<br>这是由于windows的虚拟空间造成的。所以,应该考虑用别的方式传递。<br><br>
 
你为什么不让它在播放完成后自动关闭。
 
建议你装一套msdn,作为一个程序员没有msdn是不行的。<br>给你发一段代码,从msdn上copy的<br>这段代码写的就是如何用TEven内核对象来同步程序执行的<br>---------------------------------------------------<br>&nbsp; <br>Platform SDK: DLLs, Processes, and Threads <br>Using Event Objects<br>Win32-based applications use event objects in a number of situations to notify a waiting thread of the occurrence of an event. For example, overlapped I/O operations on files, named pipes, and communications devices use an event object to signal their completion. For more information about the use of event objects in overlapped I/O operations, see Synchronization and Overlapped Input and Output.<br><br>In the following example, an application uses event objects to prevent several threads from reading from a shared memory buffer while a master thread is writing to that buffer. First, the master thread uses the CreateEvent function to create a manual-reset event object. The master thread sets the event object to nonsignaled when it is writing to the buffer and then resets the object to signaled when it has finished writing. Then it creates several reader threads and an auto-reset event object for each thread. Each reader thread sets its event object to signaled when it is not reading from the buffer. <br><br>#define NUMTHREADS 4 <br><br>HANDLE hGlobalWriteEvent; <br><br>void CreateEventsAndThreads(void) <br>{<br>&nbsp; &nbsp; HANDLE hReadEvents[NUMTHREADS], hThread; <br>&nbsp; &nbsp; DWORD i, IDThread; <br><br>&nbsp; &nbsp; // Create a manual-reset event object. The master thread sets <br>&nbsp; &nbsp; // this to nonsignaled when it writes to the shared buffer. <br><br>&nbsp; &nbsp; hGlobalWriteEvent = CreateEvent( <br>&nbsp; &nbsp; &nbsp; &nbsp; NULL, &nbsp; &nbsp; &nbsp; &nbsp; // no security attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; TRUE, &nbsp; &nbsp; &nbsp; &nbsp; // manual-reset event<br>&nbsp; &nbsp; &nbsp; &nbsp; TRUE, &nbsp; &nbsp; &nbsp; &nbsp; // initial state is signaled<br>&nbsp; &nbsp; &nbsp; &nbsp; "WriteEvent" &nbsp;// object name<br>&nbsp; &nbsp; &nbsp; &nbsp; ); <br><br>&nbsp; &nbsp; if (hGlobalWriteEvent == NULL) { <br>&nbsp; &nbsp; &nbsp; &nbsp; // error exit<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; // Create multiple threads and an auto-reset event object <br>&nbsp; &nbsp; // for each thread. Each thread sets its event object to <br>&nbsp; &nbsp; // signaled when it is not reading from the shared buffer. <br><br>&nbsp; &nbsp; for(i = 1; i &lt;= NUMTHREADS; i++) <br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; // Create the auto-reset event.<br>&nbsp; &nbsp; &nbsp; &nbsp; hReadEvents = CreateEvent( <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL, &nbsp; &nbsp; // no security attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FALSE, &nbsp; &nbsp;// auto-reset event<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TRUE, &nbsp; &nbsp; // initial state is signaled<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NULL); &nbsp; &nbsp;// object not named<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if (hReadEvents == NULL) <br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Error exit.<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br><br>&nbsp; &nbsp; &nbsp; &nbsp; hThread = CreateThread(NULL, 0, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (LPTHREAD_START_ROUTINE) ThreadFunction, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;hReadEvents, &nbsp;// pass event handle<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, &amp;IDThread); <br>&nbsp; &nbsp; &nbsp; &nbsp; if (hThread == NULL) <br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Error exit.<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }<br>}<br>Before the master thread writes to the shared buffer, it uses the ResetEvent function to set the state of hGlobalWriteEvent (an application-defined global variable) to nonsignaled. This blocks the reader threads from starting a read operation. The master then uses the WaitForMultipleObjects function to wait for all reader threads to finish any current read operations. When WaitForMultipleObjects returns, the master thread can safely write to the buffer. After it has finished, it sets hGlobalWriteEvent and all the reader-thread events to signaled, enabling the reader threads to resume their read operations. <br><br>VOID WriteToBuffer(VOID) <br>{<br>&nbsp; &nbsp; DWORD dwWaitResult, i; <br><br>&nbsp; &nbsp; // Reset hGlobalWriteEvent to nonsignaled, to block readers.<br>&nbsp;<br>&nbsp; &nbsp; if (! ResetEvent(hGlobalWriteEvent) ) <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; // Error exit.<br>&nbsp; &nbsp; } <br><br>&nbsp; &nbsp; // Wait for all reading threads to finish reading.<br><br>&nbsp; &nbsp; dwWaitResult = WaitForMultipleObjects( <br>&nbsp; &nbsp; &nbsp; &nbsp; NUMTHREADS, &nbsp; // number of handles in array<br>&nbsp; &nbsp; &nbsp; &nbsp; hReadEvents, &nbsp;// array of read-event handles<br>&nbsp; &nbsp; &nbsp; &nbsp; TRUE, &nbsp; &nbsp; &nbsp; &nbsp; // wait until all are signaled<br>&nbsp; &nbsp; &nbsp; &nbsp; INFINITE); &nbsp; &nbsp;// indefinite wait<br><br>&nbsp; &nbsp; switch (dwWaitResult) <br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; // All read-event objects were signaled.<br>&nbsp; &nbsp; &nbsp; &nbsp; case WAIT_OBJECT_0: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Write to the shared buffer.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // An error occurred.<br>&nbsp; &nbsp; &nbsp; &nbsp; default: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Wait error: %d/n", GetLastError()); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitProcess(0); <br>&nbsp; &nbsp; } <br><br>&nbsp; &nbsp; // Set hGlobalWriteEvent to signaled.<br><br>&nbsp; &nbsp; if (! SetEvent(hGlobalWriteEvent) ) <br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; // Error exit.<br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; // Set all read events to signaled.<br>&nbsp; &nbsp; for(i = 1; i &lt;= NUMTHREADS; i++) <br>&nbsp; &nbsp; &nbsp; &nbsp; if (! SetEvent(hReadEvents) ) { <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Error exit.<br>&nbsp; &nbsp; &nbsp; &nbsp; } <br>}<br>Before starting a read operation, each reader thread uses WaitForMultipleObjects to wait for the application-defined global variable hGlobalWriteEvent and its own read event to be signaled. When WaitForMultipleObjects returns, the reader thread's auto-reset event has been reset to nonsignaled. This blocks the master thread from writing to the buffer until the reader thread uses the SetEvent function to set the event's state back to signaled. <br><br>VOID ThreadFunction(LPVOID lpParam) <br>{<br>&nbsp; &nbsp; DWORD dwWaitResult;<br>&nbsp; &nbsp; HANDLE hEvents[2]; <br><br>&nbsp; &nbsp; hEvents[0] = *(HANDLE*)lpParam; &nbsp;// thread's read event<br>&nbsp; &nbsp; hEvents[1] = hGlobalWriteEvent; <br><br>&nbsp; &nbsp; dwWaitResult = WaitForMultipleObjects( <br>&nbsp; &nbsp; &nbsp; &nbsp; 2, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// number of handles in array<br>&nbsp; &nbsp; &nbsp; &nbsp; hEvents, &nbsp; &nbsp; &nbsp;// array of event handles<br>&nbsp; &nbsp; &nbsp; &nbsp; TRUE, &nbsp; &nbsp; &nbsp; &nbsp; // wait till all are signaled<br>&nbsp; &nbsp; &nbsp; &nbsp; INFINITE); &nbsp; &nbsp;// indefinite wait<br><br>&nbsp; &nbsp; switch (dwWaitResult) <br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // Both event objects were signaled.<br>&nbsp; &nbsp; &nbsp; &nbsp; case WAIT_OBJECT_0: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Read from the shared buffer.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; <br><br>&nbsp; &nbsp; &nbsp; &nbsp; // An error occurred.<br>&nbsp; &nbsp; &nbsp; &nbsp; default: <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Wait error: %d/n", GetLastError()); <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExitThread(0); <br>&nbsp; &nbsp; }<br><br>&nbsp; &nbsp; // Set the read event to signaled.<br><br>&nbsp; &nbsp; if (! SetEvent(hEvents[0]) ) <br>&nbsp; &nbsp; { <br>&nbsp; &nbsp; &nbsp; &nbsp; // Error exit.<br>&nbsp; &nbsp; } <br>}<br>Built on Thursday, May 11, 2000<br>----------------------------------------------------<br>
 
启动时发现已有实例在运行时,向原来实例发WM_SETTEXT消息<br>&nbsp; &nbsp; hWnd := 0;<br>&nbsp; &nbsp; Application.Title := '';<br>&nbsp; &nbsp; hWnd := FindWindow('TMyApp',nil);<br>&nbsp; &nbsp; hApp := FindWindow('TApplication','MyApp');<br>&nbsp; &nbsp; if hApp &lt;&gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; SetForegroundWindow(hApp);<br>&nbsp; &nbsp; &nbsp; ShowWindow(hApp,SW_RESTORE);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if hWnd &lt;&gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if trim(ParamStr(1)) &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; GetMem(lpszFileName,255);<br>&nbsp; &nbsp; &nbsp; &nbsp; lpszFileName := PChar(trim(ParamStr(1)));<br>&nbsp; &nbsp; &nbsp; &nbsp; SendMessage(hWnd,WM_SETTEXT,0,LPARAM(lpszFileName));<br>&nbsp; &nbsp; &nbsp; &nbsp; SendMessage(hWnd,WM_USERFILE,0,0);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br><br>在主窗体中处理WM_USERFILE自定义消息<br>procedure TMyApp.OnUserFile(var msgMessage: TMessage);<br>var<br>&nbsp; lpszText: PChar;<br>begin<br>&nbsp; GetMem(lpszText,255);<br>&nbsp; if SendMessage(Application.Mainform.Handle,WM_GETTEXT,255,LPARAM(lpszText)) &gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; LoadMyFile(string(lpszText));<br>&nbsp; end;<br>&nbsp; FreeMem(lpszText);<br>end;<br>
 
Borinp:您的方法我一直没空试,不过我已用WM_COPYDATA成功了,但仍然感谢您的回答<br>,谢谢!
 
好了,我早已解决了此问题,谢谢各位参加!
 

Similar threads

后退
顶部