快捷导航 上传作品

[VB] vb和C++调用VB编译的DLL,涉及参数传递

[复制链接]
梅雷工具箱发表于 2015-6-25 13:09:26 | 显示全部楼层 |阅读模式

vb和C++调用VB编译的DLL,涉及参数传递



在VC中调用VB编写的COM组件范例

VB调用VC的DLL大家应该不陌生,而VC如何调用VB编写的DLL这个问题对于搞VB开发的人来说可能却就是个问题。
为了广大的VB爱好者向VC进军,我就从VB开发者的角度来说说在VC中调用VB编写的COM组件的方法。我举个例子。
先说说VB开发一个COM组件。
用VB新建一个ActiveX DLL 工程,
然后修改工程名称为 MyVB
再修改默认的类名为 DEMO
然后再向类中添加一个函数作为测试用,如: Visual Basic Code

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
'取得当前系统登陆的用户名称
Public Function GetMyName() As String
  MsgBox "C++调用VB编译DLL,涉及参数传递"
  GetMyName = "C19920103梅雷批准通过"
End Function


Public Sub SayHello()
MsgBox "C++调用VB编译DLL"
End Sub



然后保存工程并生成DLL文件,这时已经可以通过VB动态调用这个COM组件了。

注意 生成的DLL文件 必须注册 不然在自己的计算机能用在别人的计算机出错


如果在VB里,可以这样调用:
Visual Basic Code
      Dim MyObj As Object
      Set MyObj   =   CreateObject ( "MyVB.DEMO" )  
      MsgBox MyObj.GetMyName
      Set MyObj   =   Nothing




这时可以看出,MyVB 是 COM 组件名称,DEMO 是 COM 组件的类名称,
而 VC 里调用可以这样做,如:
Visual C++ Code

新建一个 UF程序
//////////////////////////////////////////////////////////////////////////////
//
//  CC.cpp
//
//  Description:
//      Contains Unigraphics entry points for the application.
//
//////////////////////////////////////////////////////////////////////////////

//  Include files
#include <uf.h>
#include <uf_exit.h>
#include <uf_ui.h>
#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
#        include <strstream>
#   include <iostream>
        using std:strstream;
        using std::endl;       
        using std::ends;
        using std::cerr;
#else
#        include <strstream.h>
#   include <iostream.h>
#endif
#include "CC.h"
#include <string>

//下面是指定DLL的位置
#import "D://MyVB.dll"
using namespace MyVB;    //这里是COM组件名称
#include "stdio.h"


HRESULT ComInit();
mmm(char b[132])
{
  ComInit();
    _DEMOPtr MyObj;        //注意这里,类名虽然是DEMO,但是定义声明应该是“_类名Ptr”来定义对象
    MyObj.CreateInstance("MyVB.DEMO");

    _bstr_t vRsinfo;    //VB的String在VC里可以用_bstr_t类型,所以就用_bstr_t类型来接返回参数
    vRsinfo = MyObj->GetMyName();    //调用vb生成的dll 带参数
    strcpy(b,vRsinfo);
}


HRESULT ComInit()
{
    HRESULT hr = S_OK;
    if FAILED(CoInitialize(NULL))
    {
        CoUninitialize();
        hr = E_UNEXPECTED;
    }
    return hr;
}



//----------------------------------------------------------------------------
//  Activation Methods
//----------------------------------------------------------------------------

//  Unigraphics Startup
//      This entry point activates the application at Unigraphics startup
extern "C" DllExport void ufsta( char *param, int *returnCode, int rlen )
{
    /* Initialize the API environment */
    int errorCode = UF_initialize();

    if ( 0 == errorCode )
    {
        /* TODO: Add your application code here */
       int c;
       char a[132],b[132];
           mmm(b);
       strcpy(a,b);
       uc1601(a,1);   //在UG弹出显示框
      
      

        /* Terminate the API environment */
        errorCode = UF_terminate();
    }

    /* Print out any error messages */
    PrintErrorMessage( errorCode );
}



//----------------------------------------------------------------------------
//  Utilities
//----------------------------------------------------------------------------

// Unload Handler
//     This function specifies when to unload your application from Unigraphics.
//     If your application registers a callback (from a MenuScript item or a
//     User Defined Object for example), this function MUST return
//     "UF_UNLOAD_UG_TERMINATE".
extern "C" int ufusr_ask_unload( void )
{
    return( UF_UNLOAD_UG_TERMINATE );
}

/* PrintErrorMessage
**
**     Prints error messages to standard error and the Unigraphics status
**     line. */
static void PrintErrorMessage( int errorCode )
{
    if ( 0 != errorCode )
    {
        /* Retrieve the associated error message */
        char message[133];
        UF_get_fail_message( errorCode, message );

        /* Print out the message */
        UF_UI_set_status( message );

        // Construct a buffer to hold the text.
        ostrstream error_message;

        // Initialize the buffer with the required text.
        error_message << endl
                      << "Error:" << endl
                      << message
                      << endl << endl << ends;

            // Write the message to standard error
        cerr << error_message.str();
    }
}


_____________________________________________________________________________ _

中磊UG二次开发教程 梅雷著 qq1821117007
学UG就上UG网 http://www.9sug.com/
就上UG网淘宝直营店
回复

使用道具 评分 举报

您需要登录后才可以回帖 登录 | 注册UG网

本版积分规则