扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
'vb.net2008
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、成都小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了东港免费建站欢迎大家使用!
项目-添加引用-浏览
选定后在左侧工具箱中所有windows窗体中找,很有可能是最后一个
上级目录不可以,但是为了保证程序路径改变,引用不会被破坏。可以有几个方法:
1.将DLL文件放在程序同级目录
2.将DLL文件放在程序子目录
3.将DLL文件放在System32目录下(系统目录,可以直接引用DLL)
4.可以将DLL文件打包成资源文件,融合到EXE程序中,每次执行判断同级目录是否有DLL文件,不存在,释放资源文件。
Private Declare function x Lib "dlltest.dll" (ByVal a As long, byval b as long) as long
假设你的dll程序放在d:\123\123.dll
在别的电脑上,运行regsvr32 d:\123\123.dll
就可以解决以上问题
我不会VB,但是都是.net。c#的代码稍微转换一下就可以了。
C#实现访问网络共享文件夹,使用 WNetAddConnection2A 和 WNetCancelConnection2A。
在目标服务器建立共享文件夹,建立访问账号test;
public enum ERROR_ID
{
ERROR_SUCCESS = 0, // Success
ERROR_BUSY = 170,
ERROR_MORE_DATA = 234,
ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
ERROR_INVALID_LEVEL = 124,
ERROR_ACCESS_DENIED = 5,
ERROR_INVALID_PASSWORD = 86,
ERROR_INVALID_PARAMETER = 87,
ERROR_BAD_DEV_TYPE = 66,
ERROR_NOT_ENOUGH_MEMORY = 8,
ERROR_NETWORK_BUSY = 54,
ERROR_BAD_NETPATH = 53,
ERROR_NO_NETWORK = 1222,
ERROR_INVALID_HANDLE_STATE = 1609,
ERROR_EXTENDED_ERROR = 1208,
ERROR_DEVICE_ALREADY_REMEMBERED = 1202,
ERROR_NO_NET_OR_BAD_PATH = 1203
}
public enum RESOURCE_SCOPE
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET = 2,
RESOURCE_REMEMBERED = 3,
RESOURCE_RECENT = 4,
RESOURCE_CONTEXT = 5
}
public enum RESOURCE_TYPE
{
RESOURCETYPE_ANY = 0,
RESOURCETYPE_DISK = 1,
RESOURCETYPE_PRINT = 2,
RESOURCETYPE_RESERVED = 8,
}
public enum RESOURCE_USAGE
{
RESOURCEUSAGE_CONNECTABLE = 1,
RESOURCEUSAGE_CONTAINER = 2,
RESOURCEUSAGE_NOLOCALDEVICE = 4,
RESOURCEUSAGE_SIBLING = 8,
RESOURCEUSAGE_ATTACHED = 16,
RESOURCEUSAGE_ALL = (RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
}
public enum RESOURCE_DISPLAYTYPE
{
RESOURCEDISPLAYTYPE_GENERIC = 0,
RESOURCEDISPLAYTYPE_DOMAIN = 1,
RESOURCEDISPLAYTYPE_SERVER = 2,
RESOURCEDISPLAYTYPE_SHARE = 3,
RESOURCEDISPLAYTYPE_FILE = 4,
RESOURCEDISPLAYTYPE_GROUP = 5,
RESOURCEDISPLAYTYPE_NETWORK = 6,
RESOURCEDISPLAYTYPE_ROOT = 7,
RESOURCEDISPLAYTYPE_SHAREADMIN = 8,
RESOURCEDISPLAYTYPE_DIRECTORY = 9,
RESOURCEDISPLAYTYPE_TREE = 10,
RESOURCEDISPLAYTYPE_NDSCONTAINER = 11
}
[StructLayout(LayoutKind.Sequential)]
public struct NETRESOURCE
{
public RESOURCE_SCOPE dwScope;
public RESOURCE_TYPE dwType;
public RESOURCE_DISPLAYTYPE dwDisplayType;
public RESOURCE_USAGE dwUsage;
[MarshalAs(UnmanagedType.LPStr)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpComment;
[MarshalAs(UnmanagedType.LPStr)]
public string lpProvider;
}
public class NetworkConnection
{
[DllImport("mpr.dll")]
public static extern int WNetAddConnection2A (NETRESOURCE[] lpNetResource, string lpPassword, string lpUserName, int dwFlags);
[DllImport("mpr.dll")]
public static extern int WNetCancelConnection2A (string sharename, int dwFlags, int fForce);
public static int Connect (string remotePath, string localPath, string username, string password)
{
NETRESOURCE[] share_driver = new NETRESOURCE[1];
share_driver[0].dwScope = RESOURCE_SCOPE.RESOURCE_GLOBALNET;
share_driver[0].dwType = RESOURCE_TYPE.RESOURCETYPE_DISK;
share_driver[0].dwDisplayType = RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;
share_driver[0].dwUsage = RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
share_driver[0].lpLocalName = localPath;
share_driver[0].lpRemoteName = remotePath;
Disconnect(localPath);
int ret = WNetAddConnection2A(share_driver, password, username, 1);
return ret;
}
public static int Disconnect (string localpath)
{
return WNetCancelConnection2A(localpath, 1, 1);
}
调用方式:
public static void TestNetWorkConnection()
{
string localpath = "X:";
//int status = NetworkConnection.Connect(@"\\192.168.0.2\test", localpath, @"test", "test");
int status = NetworkConnection.Connect("\\\\192.168.0.2\\test", localpath, @"test", "test");
if (status == (int)ERROR_ID.ERROR_SUCCESS)
{
FileStream fs = new FileStream(localpath + @"\\123.txt", FileMode.OpenOrCreate);
using (StreamWriter stream = new StreamWriter(fs))
{
stream.WriteLine("你好呀,成功了");
stream.Flush();
stream.Close();
}
fs.Close();
}
else
{
Console.WriteLine(status);
}
NetworkConnection.Disconnect(localpath);
}
DLL(动态链接库)是一个很有用的东西,在开发大项目的时候显得非常重要,因为多人合作开发时,可以给每个人分配一个任务,用DLL完成,最后组合起来,就不会出现互相冲突的问题。这里给出最简单的DLL编写与调用的示例
首先,我们打开VB.NET,选择类库,名称改为test
然后输入以下代码
Public Class test
Public Function test(ByVal a As Long, ByVal b As Long) As Long
Return a + b
End Function
End Class
保存后,生成DLL文件。
这就是最简单的一个DLL,下面是调用该DLL的示例
新建一个工程,单击“项目” -- 添加引用
找到刚才生成的DLL,双击它
添加引用以后,似乎什么也没发生,这时我们输入以下代码:
Imports test.test
这样,就包含了该DLL的类。
然后我们定义一个类
Dim test As New test.test
这样,就可以使用里面的函数了,下面是程序示例
Imports test.test
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim test As New test.test
MsgBox(test.test(1, 2))
End Sub
End Class
运行该程序,可以看到,调用了DLL内的函数。
这就是最简单的DLL示例,可以将一些复杂的代码集成到DLL里,以后升级或重用都比较方便。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流