使用VC编写VB使用DLL
使用VC编写VB使用DLL
一、在函数定义前必须加上extern "c",_stdcall关键字。
extern "C" int _stdcall Sum(int x,int y)
{
return x+y;
}
二、DLL的.def文件中必须加上入口函数
EXPORTS
sample @1
sample是你要在VB中调用的函数名,@1表示该函数在DLL中的编号
三、要注意DLL中的函数和VB中的函数声明在名称、返回类型、参数类型、参数个数等方面必须完全相同,尤其要注意大小写的问题。
注意事项:
1、必须保证VB和VC的参数个数相同,参数所占字节数也一致
3.数组参数在DLL中的传递
由于DLL经常用来进行一些底层的运算操作,因此应用程序常需要传递大量的数据给DLL。在C++中,指针是进行数组操作的最佳选择,但VB中没有指针的概念。这通常可用两种方法来解决。
其一,在VB中声明DLL时,用byref来代替byval,即可将数组指针传递给DLL。
另外,将数组声明为变体型(variant),即可直接将数组传递给DLL。
四、在vb中声明("dllTest.dll" 使用的是相对路径,你可以将dll.dll放到.exe所在的目录或系统目录),函数名大小敏感
Private Declare Function Sum Lib "dllTest.dll" (ByVal x As Long, ByVal y As Long) As Long
五、例子
1、新建一个win32 dynamic library,空的工程
2、新建一个dllTest.cpp 文件,并添加到工程。内容如下
// dll4.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" int _stdcall Sum(int x,int y)
{
return x+y;
}
extern "C" int _stdcall GetNum(int Num) /*具体实现*/
{
return Num;
}
// dll4.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
相关文档:
传统方法是遍历一遍
如果listbox 项目过多
明显速度不行
好方法是通过sendmessge发消息给listbox让他把选中项目直接传到参数数组中
You can use the SendMessage() API function instead.
As
you probably know, this function lets you send a message to one or more
windows. The declaration statement conforms ......
不能直接使用CopyMemoryStr,应该将字符串转为byte数组,然后使用CopyMemory
Property Get item() As String
If h = 0 Then ErrRaise ERROR_INVALID_DATA
'BugAssert p <> pNull
Dim c As Long, ptr0 As Long
Dim ab() As Byte
& ......
VB与Matlab混合编程有多种方式,这里讨论Matlab将M文件编译成com组件共VB调用的方式。
Matlab版本:Matlab 7.7. 0.471 R2008b
VB版本:VB 6.0 & VB 2008
首先写好完成某个功能的m文件,比如在m文件中写入如下函数
function [returns,stderr]=GetReturns(prices)
returns=price2ret(prices)&nbs ......
“自动点击按钮”小工具VB源码
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Pri ......