Solved

Addtional, more specific, info on VB ActiveX dll called in C++

Posted on 2008-10-04
6
322 Views
Last Modified: 2013-11-25
I have a legacy system written in VS c++ 6.0, from this code I need to call a VB 6.0 ActiveX dll. I have searched and found sveral threads on the subject that have helped but I need a bit more info as my c++ skills are fairly limited.

The issues is the dll has parameters that are strings and passed in by value (byVal) and by reference (ByRef). I can't seem to get it all put togather! There are two public functions within the dll, I am really only interested in calling the CallFunction of the clsTest class; the Workercls was written as public which was an oversight by me.

I have imported the type library through the class wizard, what I need is th actual code to use this dll, passing the the ByVal strings and the ByRef strings so that I can read/manipulate them on the dll's return.

Below is all the code I can think of you Experts might need, if I have missed something please let me know.

Thanks in advance for any and all help!!

VB DLL

Project/dll name is dllTest. 

Class name is clsTest.

VB DLL public function declaration:

Function CallFunction(ByVal sHeaderInformation As String, ByVal sFunctionName As String, ByVal sInputs As String, ByRef sOutputs As String, ByRef sErrors As String) As Integer
 

C++ Header file created by Classview Wizard

// Machine generated IDispatch wrapper class(es) created with ClassWizard

/////////////////////////////////////////////////////////////////////////////

// _clsTest wrapper class
 

class _clsTest : public COleDispatchDriver

{

public:

	_clsTest() {}		// Calls COleDispatchDriver default constructor

	_clsTest(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}

	_clsTest(const _clsTest& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}
 

// Attributes

public:
 

// Operations

public:

	short CallFunction(LPCTSTR sHeaderInformation, LPCTSTR sFunctionName, LPCTSTR sInputs, BSTR* sOutputs, BSTR* sErrors);

};

/////////////////////////////////////////////////////////////////////////////

// _Workercls wrapper class
 

class _Workercls : public COleDispatchDriver

{

public:

	_Workercls() {}		// Calls COleDispatchDriver default constructor

	_Workercls(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}

	_Workercls(const _Workercls& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}
 

// Attributes

public:
 

// Operations

public:

	// method 'DoScale' not emitted because of invalid return type or parameter type

};
 

CPP file created by Classview Wizard

// Machine generated IDispatch wrapper class(es) created with ClassWizard
 

#include "stdafx.h"

#include "dlltest.h"
 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif
 
 
 

/////////////////////////////////////////////////////////////////////////////

// _clsTest properties
 

/////////////////////////////////////////////////////////////////////////////

// _clsTest operations
 

short _clsTest::CallFunction(LPCTSTR sHeaderInformation, LPCTSTR sFunctionName, LPCTSTR sInputs, BSTR* sOutputs, BSTR* sErrors)

{

	short result;

	static BYTE parms[] =

		VTS_BSTR VTS_BSTR VTS_BSTR VTS_PBSTR VTS_PBSTR;

	InvokeHelper(0x60030000, DISPATCH_METHOD, VT_I2, (void*)&result, parms,

		sHeaderInformation, sFunctionName, sInputs, sOutputs, sErrors);

	return result;

}
 
 

/////////////////////////////////////////////////////////////////////////////

// _Workercls properties
 

/////////////////////////////////////////////////////////////////////////////

// _Workercls operations

Open in new window

0
Comment
Question by:Jenslove
  • 2
  • 2
6 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22645184
>>>> LPCTSTR sHeaderInformation

That is a constant pointer to a string which is either an ANSI String (single-byte zero-terminated) or a UNICODE string (double-byte chars and two zero-bytes for termination).

Unfortunately, I don't know whether VB uses UNICODE or ANSI strings. In VC it depends on the project settings. But in any case in VB you need to have a BYREF AS STRING argument cause a pointer in C is an address ('reference') and argument strings in C never were passed 'by value'.
0
 

Author Comment

by:Jenslove
ID: 22650354
Thank you for the help, unfortunately it doesn't help this noob much. :(

My problem is I don't know c++, hardly at all. This is a legacy system I have to 'crack' open so I am looking for code examples on how to call this vb class/function from the c++ app.

And of course this is a 'had to have yesterday' project. *sigh*

Thanks again for you help though. Can you give me an example?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22654455
>>>> Can you give me an example?
As told I am weak at the VB side. But did you try to replace all these BYVAL arguments with BYREF strings? I am pretty sure that is the only way ...
0
 
LVL 22

Accepted Solution

by:
ambience earned 500 total points
ID: 22676110
Its been a very long time since I worked with MFC/COM but I think it should be something along the lines of

_clsTest clstest;
BOOL res = clstest.CreateDispatch(L"YouApp.ClassProgName", NULL);
if( res == TRUE) 
{
    BSTR outputs, errors;  
    try 
    {
         clstest.CallFunction("
sHeaderInformation
",
 
                         "sFunctionName
",
 "sInputs
",
 
                         &o
utputs
,
 &error
};
         // use output params
         // ...
         // done using stuff


         SysFreeString(outputs);


         SysFreeString(error);
 
    } 
    catch (com_exception& cx) 
    {
    }
}

Hope this helps ...

Open in new window

0
 
LVL 22

Expert Comment

by:ambience
ID: 22676115
Oh and BTW, you need to replace the parameters with actual values. I only used placeholders to show what goes where.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now