最近本人在搞个程序,一个小功能是需要在程序开始时判断是否有已存在的实例在运行,如果有就告诉那个实例去打开文件(传递个字符串过去)然后结束当前实例.功能就是自动在一个MDI窗口中打开多个文档.
最开始我设想用DDE实现,忽然发现VB2010已经舍弃DDE了,大悲
后来又想到用写文件和注册表的方法,一想效率太低,用socket在没网卡的机子上就是个悲剧,而且这个程序和网络没关系,用socket容易触发防火墙的提示
然后我又用Sendmessage函数+Windows消息+内存映射文件,终于成功了,问题是现在需要.Net4.0支持,很多学校电脑只有.Net3.5,我演示的时候会悲剧......现在求助各位,如何用Sendmessage传递个字符串参数?wParam和lParam都是数字类型的,没法传递字符串,我试过传递指针和结构,都悲剧....
这是我的程序:
Public Class Form1
<XXXXXXXXXXteropServices.DllImport("user32.dll", CharSet:=XXXXXXXXXXXXXXXXXXXXXXXXXXarSet.Unicode, entrypoint:="SendMessageW")> _ Public Shared Function SendMessage(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As Integer) As Integer
End Function
Protected Overrides Sub DefWndProc(ByRef m As XXXXXXXXXXXXXXXXXrms.Message) Select Case XXXXg Case &H400
If m.WParam = 3180 Then
Dim s As New XXXXXreamReader(XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXeateOrOpen("asdn.open", 1024).CreateViewStream()) openfile(XXXXadToEnd) End If
Case Else
XXXXXXXXXfWndProc(m) End Select
End Sub
Sub openfile(f)
Text = f
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
If XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXunt = 1 Then Dim Proc_List As Process() = XXXXXXXXXXtProcessesByName(XXXXXXXXXXtCurrentProcess().ProcessName) If Proc_List.Length > 1 Then
For Each i As Process In Proc_List
If XXXX <> XXXXXXXXXXtCurrentProcess().Id Then Dim s As New XXXXXreamWriter(XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXeateOrOpen("asdn.open", 1024).CreateViewStream()) s.Write(XXXXXXXXXXXXXXXXXXmandLineArgs(0)) s.Flush()
XXXXose() SendMessage(XXXXinWindowHandle, &H400, 3180, 0) End If
Next
XXXXXose() Else
openfile(XXXXXXXXXXXXXXXXXXmandLineArgs(0)) End If
End If
End Sub
End Class
200字以内,仅用于支线交流,主线讨论请采用回复功能。