c++调用vb6.0制作的activex dll

本文详细介绍了如何在VS2008的C++项目中调用VB6.0创建的ActiveX DLL。首先,通过VB6.0生成并注册DLL文件PRO.dll,然后在C++的控制台程序中使用main函数调用该DLL的功能。

    本文将介绍在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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值