Hello, I am trying to get data from C library into Vb using marshalling I am able to get data for - int,foat,char,char*...
I am unable to Marshal char[] and char**. Can you please help me to understand how can I do it..
What I have tried:
Header File
extern struct listStandardTimeParameterAttributes {
int index;
int iMasterNum;
float fValue;
char parameterMethodName;
char* strUnits;
char AttrValu[256];
char** lstAttrdata;
};
extern "C" struct ParameterAttributes_C
{
listStandardTimeParameterAttributes StandardTimeParameterAttributes;
er;
};
return code -
ParameterAttributes_C ReadFile_C(char* filename) {
ParameterAttributes_C mParamNameAndValues;
mParamNameAndValues.StandardTimeParameterAttributes.iMasterNum = 1;
mParamNameAndValues.StandardTimeParameterAttributes.index = 10;
mParamNameAndValues.StandardTimeParameterAttributes.fValue = 11.1112;
char ch = 'h';
mParamNameAndValues.StandardTimeParameterAttributes.parameterMethodName = ch;
mParamNameAndValues.StandardTimeParameterAttributes.strUnits = new char[1024];
strcpy_s(mParamNameAndValues.StandardTimeParameterAttributes.strUnits, 10, "Hello2");
char attrsname[1024];
strcpy_s(attrsname, 7,"Hello2");
char** x ;
int iCnt = 0;
strcpy(x[iCnt], (char*)malloc(strlen(attrsname) * sizeof(char)));
strcpy(mParamNameAndValues.StandardTimeParameterAttributes.lstAttrdata[iCnt], x[iCnt]);
return mParamNameAndValues;
}
VB code -
<pre><StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure listStandardTimeParameterAttributes
Public index As Integer
Public iMasterNum As Integer
Public fValue As Single
Public parameterMethodName As Byte
Public strUnits As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)>
Public AttrValu As String
Public lstAttrdata As IntPtr
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)>
Public Structure ParameterAttributes_C
Public StandardTimeParameterAttributes As listStandardTimeParameterAttributes
End Structure
Declare Function ReadFile_C Lib "C:\Users\source\repos\customLibrary\x64\Debug\customLibrary.dll" (ByVal filename As String) As ParameterAttributes_C
VB calling code -
Dim si As testDR.ParameterAttributes_C = testDR.ReadFile_C(txtShowPath.Text)
MsgBox(si.StandardTimeParameterAttributes.iMasterNum, MsgBoxStyle.Information, "Return from Custom DLL")
MsgBox(si.StandardTimeParameterAttributes.index, MsgBoxStyle.Information, "Return from Custom DLL")
MsgBox(si.StandardTimeParameterAttributes.fValue, MsgBoxStyle.Information, "Return from Custom DLL")
MsgBox(Convert.ToChar(si.StandardTimeParameterAttributes.parameterMethodName), MsgBoxStyle.Information, "Return from Custom DLL")
Dim s1 As String = Marshal.PtrToStringAnsi(si.StandardTimeParameterAttributes.strUnits)
MsgBox(s1, MsgBoxStyle.Information, "Return from Custom DLL")
I tried couple of examples to marshal char[] and char** but it didn't worked.. can you please help me to understand how it can be done.