EDN Admin
Well-known member
I need some help passing a structure containing arrays to a C dll. The values are not being passed correctly to the C routine.
One thing that I know I havent done is define the fixed string length. How is that done?
I have not written the C code to return modified values yet, which I need to do.
VB code: <StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Ansi)> _
Public Structure ArrStruct
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=2, _
ArraySubType:=UnmanagedType.I4)> Public Element_int() As Integer
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=2, _
ArraySubType:=UnmanagedType.R4)> Public Element_real() As Single
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=2, _
ArraySubType:=UnmanagedType.LPStr)> Public Element_char() As String
End Structure
<DllImport("Test.dll", CallingConvention:=CallingConvention.Cdecl)> _
Sub pass_fill_StrStruct(<InAttribute(), Out()> _
ByRef Structr As ArrStruct)
End Sub
C code: struct Arr_Struct {
int *var_int[1];
float *var_real[1];
char *var_char[1][10];
};
__declspec(dllexport) void pass_fill_StrStruct(struct Arr_Struct *Structr)
{
int arr_int[1];
float arr_real[1];
char *arr_str[1];
arr_int[0] = *Structr->var_int[0];
arr_int[1] = *Structr->var_int[1];
arr_real[0] = *Structr->var_real[0];
arr_real[1] = *Structr->var_real[1];
strcpy(arr_str[0],*Structr->var_char[0]);
strcpy(arr_str[1],*Structr->var_char[1]);
}Regards, Mike
View the full article
One thing that I know I havent done is define the fixed string length. How is that done?
I have not written the C code to return modified values yet, which I need to do.
VB code: <StructLayout(LayoutKind.Sequential, Pack:=1, CharSet:=CharSet.Ansi)> _
Public Structure ArrStruct
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=2, _
ArraySubType:=UnmanagedType.I4)> Public Element_int() As Integer
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=2, _
ArraySubType:=UnmanagedType.R4)> Public Element_real() As Single
<MarshalAs(UnmanagedType.ByValArray, sizeconst:=2, _
ArraySubType:=UnmanagedType.LPStr)> Public Element_char() As String
End Structure
<DllImport("Test.dll", CallingConvention:=CallingConvention.Cdecl)> _
Sub pass_fill_StrStruct(<InAttribute(), Out()> _
ByRef Structr As ArrStruct)
End Sub
C code: struct Arr_Struct {
int *var_int[1];
float *var_real[1];
char *var_char[1][10];
};
__declspec(dllexport) void pass_fill_StrStruct(struct Arr_Struct *Structr)
{
int arr_int[1];
float arr_real[1];
char *arr_str[1];
arr_int[0] = *Structr->var_int[0];
arr_int[1] = *Structr->var_int[1];
arr_real[0] = *Structr->var_real[0];
arr_real[1] = *Structr->var_real[1];
strcpy(arr_str[0],*Structr->var_char[0]);
strcpy(arr_str[1],*Structr->var_char[1]);
}Regards, Mike
View the full article