vb点虐 与c通讯 vbnet和c语言
vb点虐 调用C语言可执行程序并传值?
在c语言中每一个变量都有两个属性一个是值,一个是址。比如inta=2;变量a的值是2,变量a的地址,可以用取地址
创新互联建站是专业的淅川网站建设公司,淅川接单;提供成都网站制作、网站建设、外贸网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行淅川网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
操作符
获取,即a。因此以C语言的函数传递中具备两种方式传递参数,一种是传址,一种传值。比如voidf1(intv){;}//函数f1v
vb点虐 如何与c语言做的dll互相传递字符串
首先是C的DLL(我的项目叫ConsoleApplication2.dll):
#include Windows.h
// 导出Test函数,供外部使用
extern "C" __declspec(dllexport) BSTR __cdecl Test(LPSTR p);
BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH: break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
case DLL_PROCESS_DETACH: break;
}
return TRUE;
}
BSTR __cdecl Test(LPSTR p)
{
p[0] = 'X';
return SysAllocString((BSTR)p);
}
然后是VB.Net项目:
Imports System.Runtime.InteropServices
Imports System.Text
Module Module1
REM 生成的是ConsoleApplication2.dll,名字与C里面相同
DllImport("ConsoleApplication2.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.Cdecl)
Public Function Test(MarshalAs(UnmanagedType.LPTStr) ByVal str As StringBuilder) As MarshalAs(UnmanagedType.BStr) String
End Function
Sub Main()
Dim str As StringBuilder = New StringBuilder("Hello")
Dim rstr As String = Test(str)
Console.WriteLine(rstr)
Console.ReadKey()
End Sub
End Module
运行结果:
用vb点虐 实现网络通信
用 vb点虐 socket通信
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
本文名称:vb点虐 与c通讯 vbnet和c语言
转载源于:http://ybzwz.com/article/ddjgoed.html