关于.NET应用程序间数据的共享(data_seg)

关于.NET应用程序间数据的共享

 

.NET为了安全起见,框架并没有提供应用程序间的数据共享功能。然而,我们的项目中,经常需要跨进程间的数据共享,只能采用一些.NET以外的东西去实现此目的。

今天我给大家介绍一种跨进程间数据共享的方法。

CodeProject站点中找到了一个VC的源码,它是通过data_seg来实现数据的共享。

http://www.codeproject.com/dll/data_seg_share.asp)但是,它是实现VC应用程序间的数据共享,需要进行简单的修改之后才能正常使用。

下面的就是我修改之后的内容。

 

1、 TestMemorySpace.def

如果VCdll想让VB.NET等应用程序正常调用,必须将自身的函数EXPORTS出来,因此需要在def文件中进行声明。

EXPORTS

    ; Explicit exports can go here

       GetValueString @1

   SetValueString @2

 

2、重新编译dll

修改完上面的内容之后,就可以重新编译dll了。

 

3、测试

新建一个.NET的应用程序,在窗体上放一个文本框和两个按钮,编写如下的代码。

 

Imports  System.Runtime.InteropServices
Imports  Microsoft.VisualBasic
Public   Class Form1
    
<DllImport("TestMemorySpace.dll", EntryPoint:="SetValueByte", SetLastError:=True, _
    CharSet:
=CharSet.Unicode, ExactSpelling:=True, _
    CallingConvention:
=CallingConvention.StdCall)> _
    
Public Shared Sub SetValueByte(ByVal bytes() As Byte) _
        
' Leave the body of the function empty.
    End Sub

    
<DllImport("TestMemorySpace.dll", EntryPoint:="GetValueByte", SetLastError:=True, _
    CharSet:
=CharSet.Unicode, ExactSpelling:=True, _
    CallingConvention:
=CallingConvention.StdCall)> _
    
Public Shared Sub GetValueByte(ByVal bytes() As Byte) _
        
' Leave the body of the function empty.
    End Sub

    
<DllImport("TestMemorySpace.dll", EntryPoint:="SetValueString", SetLastError:=True, _
    CharSet:
=CharSet.Unicode, ExactSpelling:=True, _
    CallingConvention:
=CallingConvention.StdCall)> _
    
Public Shared Sub SetValueString(ByVal a() As Byte) _
        
' Leave the body of the function empty.
    End Sub

    
<DllImport("TestMemorySpace.dll", EntryPoint:="GetValueString", SetLastError:=True, _
    CharSet:
=CharSet.Unicode, ExactSpelling:=True, _
    CallingConvention:
=CallingConvention.StdCall)> _
    
Public Shared Sub GetValueString(ByVal a() As Byte) _
        
' Leave the body of the function empty.
    End Sub

    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    
End Sub


    
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
Dim bytes() As Byte
        bytes 
= System.Text.Encoding.Default.GetBytes(TextBox1.Text.ToString)
        SetValueString(bytes)

    
End Sub


    
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        
Dim bytes(4096As Byte
        GetValueString(bytes)
        TextBox1.Text 
= System.Text.Encoding.Default.GetString(bytes)
    
End Sub

End Class

 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值