Dev-C++制作dll文件供Visual Basic调用程序
2015-09-17 16:13
585 查看
用c语言编写主要处理程序,而用可以调用dll的VB或其它界面友好的平台做为GUI,各取所长,二者结合,非常方便。
【一】制作dll文件
打开Dev-C++, 文件→新建→项目→DLL→C项目
在dll.h文件中写入如下代码
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif
DLLIMPORT __stdcall void HelloWorld();
DLLIMPORT __stdcall void HelloWorld1();
#endif
在dllmain.c文件中写入如下代码
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
DLLIMPORT __stdcall void HelloWorld()
{
MessageBox(0,"Hello World from DLL!\n","Hi",MB_ICONINFORMATION);
}
DLLIMPORT __stdcall void HelloWorld1(char *inputString)
{
MessageBox(0,inputString,"Hi",MB_ICONINFORMATION);
}
DLLIMPORT __stdcall char *HelloWorld2(char *inputString)
{
return inputString;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
case DLL_THREAD_ATTACH:
{
break;
}
case DLL_THREAD_DETACH:
{
break;
}
}
/* Return TRUE on success, FALSE on failure */
return TRUE;
}
保存项目后点击运行菜单,选择编译
然后在项目文件夹下会生成HelloWorld.dll文件,这个文件就是要供VB调用其中的函数的。
【二】VB调用dll文件
Private Declare Sub HelloWorld Lib "HelloWorld.dll" ()
Private Declare Sub HelloWorld1 Lib "HelloWorld.dll" (ByVal inputString As String)
Private Declare Function HelloWorld2 Lib "HelloWorld.dll" (ByVal inputString As String) As String
Private Sub Command1_Click()
HelloWorld
HelloWorld1 ("VB程序中也可以传入字符串")
Dim s As String
s = HelloWorld2("测试字符串传递输出")
MsgBox s
End Sub
注意dll文件的存放位置,在VB中可以用绝对或相对地址引用,这里放在exe文件同级目录中,直接用相对路径引用
传入传出各种参数注意在VB程序开始时声明
Private Declare Function HelloWorld2 Lib "HelloWorld.dll" (ByVal inputString As String) As String
生成exe文件后,点击运行,大功告成
【一】制作dll文件
打开Dev-C++, 文件→新建→项目→DLL→C项目
在dll.h文件中写入如下代码
#ifndef _DLL_H_
#define _DLL_H_
#if BUILDING_DLL
#define DLLIMPORT __declspec(dllexport)
#else
#define DLLIMPORT __declspec(dllimport)
#endif
DLLIMPORT __stdcall void HelloWorld();
DLLIMPORT __stdcall void HelloWorld1();
#endif
在dllmain.c文件中写入如下代码
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>
DLLIMPORT __stdcall void HelloWorld()
{
MessageBox(0,"Hello World from DLL!\n","Hi",MB_ICONINFORMATION);
}
DLLIMPORT __stdcall void HelloWorld1(char *inputString)
{
MessageBox(0,inputString,"Hi",MB_ICONINFORMATION);
}
DLLIMPORT __stdcall char *HelloWorld2(char *inputString)
{
return inputString;
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
{
break;
}
case DLL_PROCESS_DETACH:
{
break;
}
case DLL_THREAD_ATTACH:
{
break;
}
case DLL_THREAD_DETACH:
{
break;
}
}
/* Return TRUE on success, FALSE on failure */
return TRUE;
}
保存项目后点击运行菜单,选择编译
然后在项目文件夹下会生成HelloWorld.dll文件,这个文件就是要供VB调用其中的函数的。
【二】VB调用dll文件
Private Declare Sub HelloWorld Lib "HelloWorld.dll" ()
Private Declare Sub HelloWorld1 Lib "HelloWorld.dll" (ByVal inputString As String)
Private Declare Function HelloWorld2 Lib "HelloWorld.dll" (ByVal inputString As String) As String
Private Sub Command1_Click()
HelloWorld
HelloWorld1 ("VB程序中也可以传入字符串")
Dim s As String
s = HelloWorld2("测试字符串传递输出")
MsgBox s
End Sub
注意dll文件的存放位置,在VB中可以用绝对或相对地址引用,这里放在exe文件同级目录中,直接用相对路径引用
传入传出各种参数注意在VB程序开始时声明
Private Declare Function HelloWorld2 Lib "HelloWorld.dll" (ByVal inputString As String) As String
生成exe文件后,点击运行,大功告成
相关文章推荐
- FREEBASIC 编译可被python调用的dll函数示例
- VB简单实现防止文件被改写
- VB获取文件大小的方法
- VB实现的《QQ美女找茬游戏》作弊器实例
- VB实现禁用任务管理器的方法
- VBS教程:运算符-运算符(+)
- VB实现的16位和32位md5加密代码分享
- VB6中的LSet语句和RSet语句详解
- DLL(Dynamic Linkable Library) 详解说明
- delphi中一个值得大家来考虑的DLL问题
- VB实现的递归复制文件和搜索文件的代码分享
- 将ocx文件转换成C#程序引用的DLL文件的办法
- VB键盘鼠标无动作调用程序的尝试
- VB XMLHTTP使用方法
- VB语言使用ADO连接、操作SQLServer数据库教程
- VB使用ADO操作Access数据库
- VB使用shell函数打开外部exe程序的实现方法
- VB的TextBox文本框实现垂直居中显示的方法
- VB关机恶搞小程序