VC编写DLL供VB调用

Eddy 发布于2009-12-26 23:8:30 分类: 程序设计 已浏览loading 网友评论0条 我要评论

VB封装了很多比较好的函数,这些函数在C++中实现起来还比较麻烦;但C++中的很多操作(比如移位指令)在VB中很难实现。好的办法就是用VC编写DLL供VB调用

代码示例:

reg.cpp文件:

#include "stdafx.h"
#include "reg.h"

HMODULE g_hModule;
BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    switch (ul_reason_for_call)
 {
  case DLL_PROCESS_ATTACH:
   g_hModule=(HMODULE)hModule;
   break;
    }
    return TRUE;
}

// This is an example of an exported function.
extern "C"
long __stdcall FindCode(unsigned char a1)
{
return (a1 >> 7)
+ ((a1 >> 3) & 4)
+ ((a1 >> 5) & 2)
+ ((a1 >> 1) & 8)
+ 2 * ((a1 & 8) + 4 * ((a1 & 4) + 4 * ((a1 & 2) + 4 * (a1 & 1))));
}

reg.def文件:

EXPORTS
FindCode

reg.h文件:

#ifdef REG_EXPORTS
#define REG_API __declspec(dllexport)
#else
#define REG_API __declspec(dllimport)
#endif

extern "C"
REG_API long __stdcall FindCode(unsigned char a1);

编译成reg.dll文件,然后在VB中调用:

函数声明:

Private Declare Function FindCode Lib "reg.dll" (ByVal a1 As Long) As Long

这样,就能在VB中使用FindCode函数了。

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/281.html     欢迎订阅Eddy Blog

关于 VC  DLL  VB  的相关文章

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言