Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
   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 -
C++
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 -
VB
<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 -
VB
Dim si As testDR.ParameterAttributes_C = testDR.ReadFile_C(txtShowPath.Text)
        MsgBox(si.StandardTimeParameterAttributes.iMasterNum, MsgBoxStyle.Information, "Return from Custom DLL") 'Integer
        MsgBox(si.StandardTimeParameterAttributes.index, MsgBoxStyle.Information, "Return from Custom DLL") 'Integer
        MsgBox(si.StandardTimeParameterAttributes.fValue, MsgBoxStyle.Information, "Return from Custom DLL") 'Float
        MsgBox(Convert.ToChar(si.StandardTimeParameterAttributes.parameterMethodName), MsgBoxStyle.Information, "Return from Custom DLL") 'Char
        Dim s1 As String = Marshal.PtrToStringAnsi(si.StandardTimeParameterAttributes.strUnits)
        MsgBox(s1, MsgBoxStyle.Information, "Return from Custom DLL") 'Char*

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.
Posted
Comments
[no name] 11-Nov-22 13:29pm    
Since you have access to the source code, I suggest an (extra) API that is more palatable to both parties.
AmitJain2302 14-Nov-22 0:11am    
Hello, Thank you for your response. But is there any way I can directly marshal those types.

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