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

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
#include
#include
#if ! defined ( __hp9000s800 ) && ! defined ( __sgi ) && ! defined ( __sun )
# include
# include
using std::ostrstream;
using std::endl;
using std::ends;
using std::cerr;
#else
# include
# include
#endif
#include "CC.h"
#include

//下面是指定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;
}
ret

urn 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();
}
}


相关文档
最新文档