create a c++ function to be consumed by vb.net

hi experts
we have a working process in c++ that is to be wrapped as a com dll and be called by a vb.net application.
i (the vb.net side), want to send some parameters to the C++ dll (as plain text, no problem here...), then have the c++ return the result as an ARRAY OF STRINGS
whar are the parameters type on the c++ side? and how does the C++ function declaration should look like so when consumed by vb.net will be recognized as string array?
 
hope this is clear enough
thanx in advance
Erez
LVL 12
ErezMorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MedievalWarriorCommented:
You want to read into SAFEARRAY or CComSafeArray. I wasn't exactly sure if your reference to com dll was either an exported function or a true com library dll so I have example of a simple exported function using __stdcall convention like the windows API. You can use P/Invoke in .NET
// C++

SAFEARRAY* WINAPI MakeArray(LPWSTR param1, LPWSTR param2)
{
	LONG index;
	SAFEARRAY * psa;
	SAFEARRAYBOUND rgsabound[1];
	rgsabound[0].lLbound = 0;
	rgsabound[0].cElements = 2;
	psa = SafeArrayCreate(VT_BSTR, 1, rgsabound);
	SafeArrayLock(psa);
	index = 0;
	SafeArrayPutElement(psa, &index, SysAllocString(param1));
	index = 1;
	SafeArrayPutElement(psa, &index, SysAllocString(param2));
	SafeArrayUnlock(psa);
	return psa;

}


// VB.NET

Imports System.Runtime.InteropServices

Public Class Form1

    <DllImport("library.dll", CharSet:=CharSet.Unicode)> _
    Private Shared Function MakeArray(ByVal param1 As String, ByVal param2 As String) As <MarshalAs(UnmanagedType.SafeArray)> String()
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim data() As String = MakeArray("Hello", "VB.NET World!")
        Debug.Print(data(0))
        Debug.Print(data(1))

    End Sub

End Class

Open in new window

0
ErezMorAuthor Commented:
Thank you.
 I'll have a go with it with my cpp guy and get back to you
0
ErezMorAuthor Commented:
dear MedievalWarrior
we need the c++ dll to be com compliant (it's to be managed with com+ with many, MANY concurrent insances)
what are the changes required for your code snippet to have the c++ be compatible as com-dll

thanks again
Erez
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

MedievalWarriorCommented:
Here is how it might look when you place the code in your library of course without all the extra stuff only the key parts.
// Class1.idl

interface IClass1 : IDispatch
	{
		[id(1), helpstring("method MakeArray")]HRESULT MakeArray( [in] BSTR param1,[in] BSTR param2,[out, retval] SAFEARRAY(BSTR)* ppsa);
	};


// Class1.h
// IClass1
public:
	STDMETHOD(MakeArray)(/*[in]*/ BSTR param1, /*[in]*/ BSTR param2, /*[out,retval]*/ SAFEARRAY** ppsa);
};


// Class1.cpp

STDMETHODIMP CClass1::MakeArray(BSTR param1, BSTR param2, SAFEARRAY** ppsa)
{
	LONG index;
	SAFEARRAYBOUND rgsabound[1];
	rgsabound[0].lLbound = 0;
	rgsabound[0].cElements = 2;
	*ppsa = SafeArrayCreate(VT_BSTR, 1, rgsabound);
	SafeArrayLock(*ppsa);
	index = 0;
	SafeArrayPutElement(*ppsa, &index, SysAllocString(param1));
	index = 1;
	SafeArrayPutElement(*ppsa, &index, SysAllocString(param2));
	SafeArrayUnlock(*ppsa);

	return S_OK;
}

Open in new window

// .NET

 Dim c As New CPLUSCOMEXAMPLELib.Class1
 Dim s As Array = c.MakeArray("Hello", "World")
 Debug.Print(s(0))
 Debug.Print(s(1))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ErezMorAuthor Commented:
that surely catapults us in the right direction.
thanks a lot, mate
0
MedievalWarriorCommented:
Great! It appears .NET translates the SAFEARRAY to the Array Class but I want to point out that you could also use a string array variable if you wanted.
Dim s As Array = c.MakeArray("Hello", "World")

// Could also be represented as below

Dim s() as String = c.MakeArray("Hello", "World")

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.