我的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.   

    set App = CreateInstance()
    count = App.getCount()--->App是VB的固有对象,不能定义和它同名的变量
    --->需要调用VC中的方法时,VC的方法必须声明为标准API的格式
      

  2.   

    Dim App As Object   ??????????????????????????????App在VB里类似于Delphi 里面的Application对象
    VC里我就不知道了!
    别这么定义好不?
    会死人的!
      

  3.   

    谢谢两位,我改了一下
    Dim product As Object
    但还是有问题,我刚才在CSDN上查找了一下,好像是我的
    class CApplication
    {
    public:
    CApplication(void) ;
    int getCounter();
    };
    这个声明有问题,我对DLL这一块不熟悉,还请帮忙。
      

  4.   

    首先在VC++中生成Win32 DLL工程。在这个工程中添加几个函数供VB用户调用。一个DLL中的函数要想被VB调用,必须满足两个条件:一是调用方式为stdcall,另一个是必须是export的。要做到第一条,只须在函数声明前加上__stdcall关键字。如: 
        short __stdcall sample(short nLen, short *buffer) 
        要做到第二条,需要在*.def文件中加上如下的几行: 
        EXPORTS 
         sample @1 
        这里的sample是你要在VB中调用的函数名,@1表示该函数在DLL中的编号,每个函数都不一样。注意这里的函数名是区分大小写的
      

  5.   

    我如果直接调用C++里的没有带返回对象对象的方法是可以的,但我现在CreateInsatnce返回是要求的是一个CApplication对象。在VB里调用CApplication对象的getCount()方法。
    上边的我改了一下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)))")谢谢
      

  6.   

    用VC写个COM类对象就可以那样调用了
      

  7.   

    谢谢!请问那儿有VB调用COM的一些资料。
      

  8.   

    Dim VBExcel As Object
    Set VBExcel=CreateObject ("Excel.Application")
    你不是已经会了吗?
      

  9.   

    老大,不是不帮你啊,好象你问错地方了!这里大部分都是VB程序员,VC不是很懂耶!
    要是VB方面还可以帮你参谋参谋,VC在我这反正是--------俺费了