Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-04
6
Medium Priority
?
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

670 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