您的位置:首页 > 编程语言 > VB

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++ VB GUI 互相引用