• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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
0
ErezMor
Asked:
ErezMor
  • 3
  • 3
1 Solution
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now