VB调用DLL出错我的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()应该怎么写,谢谢!

解决方案 »

  1.   

    如果VB需要调用,调用约定应该是_cdecl
      

  2.   

    是不是我的的CApplication的类声明不对呀,是不是要声明为导出类型,我对DLL不熟悉,请高手指点一下。
      

  3.   

    Private declare Function CreateInstance Lib "TestVb.dll" () As long
      

  4.   

    To Zhymax
    谢谢!
    这样是可以,但我要调用getCount方法又要如何做了。
      

  5.   

    用Atl不就可以了。
    vc的对象导出以后vb可以直接使用?怀疑
      

  6.   

    在VB中调用微软的Excel 2000也是这样写的Dim VBExcel As Object
    Set VBExcel=CreateObject ("Excel.Application")
    X=VBExcel. Evaluate ("3+5*(cos (1/log (99. 9)))")
      

  7.   

    是不是的VB中不能调用DLL里的对象。