📄 subject_21300.htm
字号:
<p>
序号:21300 发表者:xiongli 发表日期:2002-11-15 17:00:29
<br>主题:如何把一个进程内字符串的地址传递给其他进程,使之能够使用
<br>内容:我是先用FindWindow找到目标窗口,然后通过SendMessage的lParam或者wParam把字符串地址传递过去。但是因为他们是不同的进程空间,好像这样就算传递过去了,双方看到的也是不同地址。<BR>然后我用GlobleAlloc,结果还是没有正确结果<BR>应该怎么做呢?<BR>谢谢
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:CDMA2000 回复日期:2002-11-15 17:17:18
<br>内容:你可以利用WM_COPYDATA这条特殊的WINDOWS消息,它是用来在不同的<BR>进程来传递数据的消息!这个绝对好用!<BR>至于用法,你参考MSDN相关文档了!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:CDMA2000 回复日期:2002-11-15 17:18:13
<br>内容:另外,也可以使用内存映射文件来在两个进程中共享数据
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2002-11-15 17:31:05
<br>内容:强!<BR>先给分<BR>我看看先再继续问
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:kevin 回复日期:2002-11-15 17:45:03
<br>内容:看看进程通讯好了
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2002-11-15 17:46:42
<br>内容:我用WM_COPYDATA暂时实现了<BR>但是我发现两个问题<BR>我在SendMessage中的wParam/lParam的值跟我另外一个程序中消息函数接收到的wParam/lParam值不一样!<BR>但是我把接收到的不一样的lParam转换成COPYDATASTRUCT结构后读取里面的内容却是我传递的。这可以理解,因为系统可能在内部进行了数据拷贝,然后再分派给目标进程,这样不同进程的数据就得到保护。但想不通的是为什么双方的wParam/lParam参数不一样呢??难道SendMessage函数会根据你不同的消息来动手脚??<BR>谢谢cdma
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:John Lan 回复日期:2002-11-15 19:20:40
<br>内容:看看MSDN的WM_COPYDATA的帮助,虽然讲的不是很清晰,但是给了暗示<BR>如果真的感兴趣,就用debugger跟踪一下.<BR>from MSDN:<BR>When you send a WM_COPYDATA message, SendMessage allocates a block of memory cbData bytes in size and copies the data from the caller's address space to this block. It then sends the message to the destination window. When the receiving window procedure processes this message, the lParam parameter is a pointer to a COPYDATASTRUCT structure that exists in the address space of the receiving process. The lpData member is a pointer to the copied block of memory, and the address reflects the memory location in the receiving process's address space. <BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2002-11-15 21:19:57
<br>内容:对阿<BR>不过还有一个麻烦,就是我现在在用户有wm_lbuttondown时,在回调函数里面发送wm_copydata消息对方程序可以收到,但是如果是在我程序刚刚开始,就是wm_create的时候发消息过去,对方程序就无法收到。如果我不建议窗口,在winmain后就直接发送消息,对方也无法收到,为什么??
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2002-11-15 22:13:11
<br>内容:其实我是想实现这样的功能:<BR>我的播放程序和一种文件实现了关联,用户可以双击文件用我的程序打开。如果用户选定一批文件,然后双击,那么,按照系统默认,就会打开很多个我的程序,这不是我需要的。我想用户选定一批文件双击后,还是只打开一个程序,然后其他文件不再打开程序而是添加到播放对列。<BR>我想这样实现:每次程序启动后,在建立窗口以前寻找窗口实例,如果没找到,那么就开始运行,如果找到了,说明已经有程序运行了,那么就发送一个消息给找到的窗口,同时想办法把需要添加到播放列表的文件名字符串也传递过去,消息发送完成后立刻退出。这样就保证只有一个实例运行,新的实例只会发送消息过去后退出。<BR>但是现在我的问题是如果使用WM_COPYDATA,在我窗口建立以前,甚至是窗口刚刚建立的时候,这个消息发送出去了对方是收不到的(不知道为什么),我该怎么做呢?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:John Lan 回复日期:2002-11-15 22:41:44
<br>内容:你问我的我也不清楚,我没研究过,<BR>你可以用 ::CreateFileMapping()来在进程之间share information<BR>保持single instance 可以在进程启动时OpenMutex()来打开第一个进程<BR>创建的Mutex对象来探测。<BR>(至于,你说的后两种收不到情况,你还是调试一下,看看WM_COPYDATA<BR>会不会产生其他的消息。)
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:xiongli 回复日期:2002-11-16 00:27:05
<br>内容:看了半天没看懂阿!<BR>能给一个例子吗?<BR>CreateFileMapping第一个参数应该怎么填?<BR>我是这样做的<BR>在地一个进程中<BR> HANDLE m_hMap=CreateFileMapping(0,0,PAGE_READWRITE,0,0x1000,0);<BR> void *p=(char*)MapViewOfFile(m_hMap,FILE_MAP_WRITE,0,0,0);<BR> char *pWrite="abcd";<BR> memcpy(p,pWrite,4);<BR> int a=SendMessage(hFindWnd,WM_USER+1,(int)m_hMap,0);<BR><BR><BR>然后在第二个进程中<BR> case WM_USER+1:<BR> {<BR> HANDLE m_hMap=CreateFileMapping(0,0,PAGE_READWRITE,0,0x1000,0);//不知道这句话要不要<BR> void *q=(char*)MapViewOfFile(m_hMap,FILE_MAP_WRITE,0,0,0);//不知道第一个参数是不是就写传递进来的wParam<BR> char *pRead[100];<BR> memcpy(pRead,q,4);<BR> MessageBox(0,(char*)q,0,0);<BR> break;<BR> }<BR>最好给一个例子<BR>我看了两个小时,还是没办法。身边有几本书都是一笔带过<BR>谢谢
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:John Lan 回复日期:2002-11-16 00:32:00
<br>内容:(from MSDN)<BR>Creating Named Shared Memory<BR><BR>The first process calls the CreateFileMapping function to create a file-mapping object and give it the name MyFileMappingObject. By using the PAGE_READWRITE flag, the processes will have read/write permission to the memory through any file views that are created. <BR><BR>HANDLE hMapFile;<BR><BR>hMapFile = CreateFileMapping(hFile, // Current file handle. <BR> NULL, // Default security. <BR> PAGE_READWRITE, // Read/write permission. <BR> 0, // Max. object size. <BR> 0, // Size of hFile. <BR> "MyFileMappingObject"); // Name of mapping object. <BR> <BR>if (hMapFile == NULL) <BR>{ <BR> ErrorHandler("Could not create file-mapping object."); <BR>} <BR>The process then uses the file-mapping object handle returned by CreateFileMapping in the call to MapViewOfFile to create a view of the file in the process's address space. The MapViewOfFile function returns a pointer to the file view.<BR><BR>LPVOID lpMapAddress;<BR>lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. <BR> FILE_MAP_ALL_ACCESS, // Read/write permission <BR> 0, // Max. object size. <BR> 0, // Size of hFile. <BR> 0); // Map entire file. <BR> <BR>if (lpMapAddress == NULL) <BR>{ <BR> ErrorHandler("Could not map view of file."); <BR>} <BR><BR>*The second process* calls the *OpenFileMapping* function with the name MyFileMappingObject to use the same file-mapping object as the first process. Like the first process, the second process uses the MapViewOfFile function to obtain a pointer to the file view. <BR><BR>HANDLE hMapFile;<BR>LPVOID lpMapAddress;<BR><BR>hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, // Read/write permission. <BR> FALSE, // Do not inherit the name<BR> "MyFileMappingObject"); // of the mapping object. <BR> <BR>if (hMapFile == NULL) <BR>{ <BR> ErrorHandler("Could not open file-mapping object."); <BR>} <BR> <BR>lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object. <BR> FILE_MAP_ALL_ACCESS, // Read/write permission. <BR> 0, // Max. object size. <BR> 0, // Size of hFile. <BR> 0); // Map entire file. <BR> <BR>if (lpMapAddress == NULL) <BR>{ <BR> ErrorHandler("Could not map view of file."); <BR>} <BR><BR>2002-11-16 0:33:33
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -