Link to home
Start Free TrialLog in
Avatar of ErezMor
ErezMorFlag for Australia

asked on

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
Avatar of MedievalWarrior
MedievalWarrior
Flag of United States of America image

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

Avatar of ErezMor

ASKER

Thank you.
 I'll have a go with it with my cpp guy and get back to you
Avatar of ErezMor

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of MedievalWarrior
MedievalWarrior
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ErezMor

ASKER

that surely catapults us in the right direction.
thanks a lot, mate
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