Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will be writing several .dll's to act as loadable Parser's for user settable hardware devices (somewhat similar to plugins). The .DLL's will be selected and loaded at run time by the user.

Several of the functions within the .DLL will be required to return a series of strings such as a list of supported features... or group of accessible locations... etc...

The returned strings will be displayed in a ListBox or Combobox within a vb.net application (which i will also be writing)...


I have spent a considerable length of time searching online (google, forums etc) and although I am new to VB.net, I do have some experience with Pascal, Delphi, C, C++ and VB and I understand the mechanics of dynamically loading using the LoadLibrary, FreeLibrary, GetProcAddress, etc... But I am now getting totally confused and frustrated by the Marshalling, importing, exporting, SafeArray, COleSafeArray, etc


Am i going about this the right way, or is there a better way of doing what i am trying to achieve.

What I have tried:

C++
// in Driver.h
__declspec(dllexport) SAFEARRAY* _stdcall DriverGetLOT();

// in Driver.cpp
void FillSafeArray(OLECHAR FAR *sz, int iRow, COleSafeArray *sa)
{
	VARIANT v;
	long index[1];
	index[0] = iRow;
	VariantInit(&v);
	v.vt = VT_BSTR;
	v.bstrVal = SysAllocString(sz);
	sa->PutElement(index, v.bstrVal);
	SysFreeString(v.bstrVal);
	VariantClear(&v);
}


SAFEARRAY* _stdcall DriverGetLOT()
{
	DWORD saBounds[] = {4};

	COleSafeArray saRet;

	saRet.Create(VT_BSTR, 1, saBounds);
	FillSafeArray(strBatchName.AllocSysString(),0,&saRet);
	FillSafeArray(strWaferName.AllocSysString(),1,&saRet);
	FillSafeArray(strExpectedYield.AllocSysString(),2,&saRet);
	FillSafeArray(strOperatorName.AllocSysString(),3,&saRet);
	return saRet.Detach(); // <=== gives a cant convert VARIANT to SAFEARRAY error
}
VB
// in Vb.NET
Public Class myClass

Private Declare Function DriverGetLOT Lib "Driver.dll"() As string() ' also tried object and VARIANT here 


Sub DoStuff()
  Me.lbLOTinfo.Items.AddRange(DriverGetLOT())
end sub

end class
Posted
Updated 22-Apr-19 23:58pm
v2

1 solution

The best way is to return ONLY ONE string which is separated with control character like "\n" which seperates each single string. Than you can split this string and work with it.

Another approach is to have a function which returns the count of array and the array.

I strongly recommand that you make a local copy of the array to avoid memory glitches by allocate and delete in different runtimes!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900