本文将介绍在vs2008 中C++ 调用vb6.0 生成的activex dll。具体的源代码可以从以下链接下载:http://download.csdn.net/detail/xsjm206/4092111
第一步:vb6.0制作的Activex DLL,发布为PRO.dll,并手动注册该dll文件(regsvr32 )。
第二步:vs2008,C++ 制作控制台程序调用PRO.dll,main 函数中的代码如下:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include<atlstr.h>
#include <iostream>
#include <string>
#include <string.h>
#include <comdef.h>
using namespace std;
#import "C:\PRO.dll" no_namespace //导入activex dll,"C:\PRO.dll"是dll的路径,也可以放在执行目录下或C:\windows\system32下
int _tmain(int argc, _TCHAR* argv[])
{
short Ret;
CString strDir("D:\\NonSave\\aa");//斜杠需要转义
BSTR strC =strDir.AllocSysString();
CString strFile("D:\\NonSave\\a.txt");
BSTR strB=strFile.AllocSysString();
int V1,V2,ADDR;
V1=5;
V2=4;
try
{
CoInitialize(NULL);
// Declare the Interface Pointer for your Visual Basic object. Here, _Class1Ptr is the Smart pointer wrapper class representing the
// default interface of the Visual Basic object.
_Class1Ptr ptr;
// Create an instance of your Visual Basic object, here, __uuidof(Class1) gets the CLSID of your Visual Basic object.
ptr.CreateInstance(__uuidof(Class1)); //Class1是vb制作的DLL中的类名
//vb DLL 中的函数声明(变量传参为byref):Public Function makeDir(dirPath As String) As Boolean
Ret=ptr->makeDir(&strC);
if (Ret==FALSE)
{
printf("Call vb function MakeDir Failed\n");
}
else
{
printf("Call vb function MakeDir Successed\n");
}
//vb DLL 中的函数声明(变量传参为byval:Public Function DeleteFile(ByVal filePath As String) As Boolean
Ret=ptr->DeleteFile(strB);
if (Ret==FALSE)
{
printf("Call vb function DeleteFile Failed\n");
}
else
{
printf("Call vb function DeleteFile Successed\n");
}
//vb DLL 中的函数声明:Public Function ADD(ByVal val1 As Long, ByVal val2 As Long) As Long
ADDR=ptr->ADD(V1,V2);
printf("add result:%d",ADDR);
}
catch(_com_error &e)
{
printf("exception occur in Calling vb DLL \n");
}
CoUninitialize();
SysFreeString(strC);//释放内存
SysFreeString(strB);//释放内存
return 0;
}
本文详细介绍了如何在VS2008的C++项目中调用VB6.0创建的ActiveX DLL。首先,通过VB6.0生成并注册DLL文件PRO.dll,然后在C++的控制台程序中使用main函数调用该DLL的功能。

2949

被折叠的 条评论
为什么被折叠?



