我的VB代码如下
Private declare Function CreateInstance Lib "TestVb.dll" As Object
Dim App As Object
Dim count As longprivate Sub Command1_clikc()
set App = CreateInstance()
count = App.getCount()
End Sub我的Dll代码如下
TestVb.def文件内容TestVb.def - defines the exports for TestVb.dllLIBRARY TestVb
DESCRIPTION 'A C++ dll that can be called from VB'EXPORTS
CreateInstanceTestVb.h文件内容
class CTestVb {
public:
CTestVb(void);
// TODO: add your methods here.
};class CApplication
{
public:
CApplication(void) ;
int getCounter();
};extern int nTestVb;int fnTestVb(void);
CApplication* __stdcall CreateInstance();
TestVb.cpp文件内容#include "stdafx.h"
#include "TestVb.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
int nTestVb=0;// This is an example of an exported function.
int fnTestVb(void)
{
return 42;
}// This is the constructor of a class that has been exported.
// see TestVb.h for the class definition
CTestVb::CTestVb()
{
return;
}CApplication::CApplication()
{}int CApplication::getCounter()
{
return 10 ;
}CApplication* __stdcall CreateInstance()
{
CApplication *app = new CApplication ;
return app ;
}
但我在VB里一点Button的按钮就错了,如果CreateInstance没有返回对象,我在VB是可以调用的,请问有返回对象时并调用对象的方法如这里getCount()应该怎么写,谢谢!
Private declare Function CreateInstance Lib "TestVb.dll" As Object
Dim App As Object
Dim count As longprivate Sub Command1_clikc()
set App = CreateInstance()
count = App.getCount()
End Sub我的Dll代码如下
TestVb.def文件内容TestVb.def - defines the exports for TestVb.dllLIBRARY TestVb
DESCRIPTION 'A C++ dll that can be called from VB'EXPORTS
CreateInstanceTestVb.h文件内容
class CTestVb {
public:
CTestVb(void);
// TODO: add your methods here.
};class CApplication
{
public:
CApplication(void) ;
int getCounter();
};extern int nTestVb;int fnTestVb(void);
CApplication* __stdcall CreateInstance();
TestVb.cpp文件内容#include "stdafx.h"
#include "TestVb.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
// This is an example of an exported variable
int nTestVb=0;// This is an example of an exported function.
int fnTestVb(void)
{
return 42;
}// This is the constructor of a class that has been exported.
// see TestVb.h for the class definition
CTestVb::CTestVb()
{
return;
}CApplication::CApplication()
{}int CApplication::getCounter()
{
return 10 ;
}CApplication* __stdcall CreateInstance()
{
CApplication *app = new CApplication ;
return app ;
}
但我在VB里一点Button的按钮就错了,如果CreateInstance没有返回对象,我在VB是可以调用的,请问有返回对象时并调用对象的方法如这里getCount()应该怎么写,谢谢!
count = App.getCount()--->App是VB的固有对象,不能定义和它同名的变量
--->需要调用VC中的方法时,VC的方法必须声明为标准API的格式
VC里我就不知道了!
别这么定义好不?
会死人的!
Dim product As Object
但还是有问题,我刚才在CSDN上查找了一下,好像是我的
class CApplication
{
public:
CApplication(void) ;
int getCounter();
};
这个声明有问题,我对DLL这一块不熟悉,还请帮忙。
short __stdcall sample(short nLen, short *buffer)
要做到第二条,需要在*.def文件中加上如下的几行:
EXPORTS
sample @1
这里的sample是你要在VB中调用的函数名,@1表示该函数在DLL中的编号,每个函数都不一样。注意这里的函数名是区分大小写的
上边的我改了一下CApplication* __stdcall CreateInstance();返回指针好像是不对,在VB中对应的是一个Long类型了,我将其改为了
CApplication __stdcall CreateInstance();对应的实现文件也改了一下,我看在VB中调用Excel200是这样写的,我也想这样实现。
Dim VBExcel As Object
Set VBExcel=CreateObject ("Excel.Application")
X=VBExcel. Evaluate ("3+5*(cos (1/log (99. 9)))")谢谢
Set VBExcel=CreateObject ("Excel.Application")
你不是已经会了吗?
要是VB方面还可以帮你参谋参谋,VC在我这反正是--------俺费了