G
Guest
I'm trying to receive a data structure from C dll from C# application . Since
there is an array of char this cause a lot of problems.
The C structure definition is as following:
//C typedef
typedef enum _T_GDS_Handler_TYPE{
GDS_Type = 0, // General Database Structure
TS_Type = 1 // Test Structure
}T_GDS_Handler_TYPE;
//// TLL GDS - General Data Structure
typedef struct _T_GeneralTestsDatabase {
char Name [256]; // TLL Module Name
T_GDS_Handler_TYPE handlerType;
int numberOfStructure; //number of structurs
void *ArrayOfStructurs; //handle array of T_GDS or TS depending on the
handlerType
} T_GeneralTestsDatabase;
//C dll function :
//Global data
/*
T_TestStructure *tt ;
handlerType;
numberOfStructure
Name
*/
_DLL_API int CALL_GetInitDatabaseList(T_GeneralTestsDatabase *TG)
{
__try
{
(*TG).ArrayOfStructurs = tt; //tt already includes data
(*TG).numberOfStructure =numberOfStructure;
(*TG).handlerType = handlerType;
////Here is the problem How to move the data ???
strncpy((*TG).Name,Name,min(strlen((*TG).Name),strlen(Name)));
return TRUE;
}
__finally
{
...
}
}
//C#
[ StructLayout( LayoutKind.Sequential ,CharSet = CharSet.Ansi)]
public struct GENERAL_DATABASE_structure //GDS
{
[MarshalAs(UnmanagedType.LPStr, SizeConst=256)]
public string Name ; // TLL Module Name */
public T_GDS_Handler_TYPE handlerType;
public int numberOfStructure; //number of structures public
IntPtr ArrayOfStructurs;//handle array of GDS or TS depending on the
handlerType
}
[DllImport("CALL_DLL.dll",
EntryPoint="CALL_GetInitDatabaseList",SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.Cdecl)]
public static extern int CALL_GetDB(ref GENERAL_DATABASE_structure TG);
CALL_GENERAL_DATABASE_structure my_GDS = new
CALL_GENERAL_DATABASE_structure();//[T_InitCount];
if(CALL_Access.CALL_GetDB(ref my_GDS)!=TRUE)...
If I try to use strncpy in DLL , I receive an exception if I remove this
line from dll I get garbage !
What should I do?
there is an array of char this cause a lot of problems.
The C structure definition is as following:
//C typedef
typedef enum _T_GDS_Handler_TYPE{
GDS_Type = 0, // General Database Structure
TS_Type = 1 // Test Structure
}T_GDS_Handler_TYPE;
//// TLL GDS - General Data Structure
typedef struct _T_GeneralTestsDatabase {
char Name [256]; // TLL Module Name
T_GDS_Handler_TYPE handlerType;
int numberOfStructure; //number of structurs
void *ArrayOfStructurs; //handle array of T_GDS or TS depending on the
handlerType
} T_GeneralTestsDatabase;
//C dll function :
//Global data
/*
T_TestStructure *tt ;
handlerType;
numberOfStructure
Name
*/
_DLL_API int CALL_GetInitDatabaseList(T_GeneralTestsDatabase *TG)
{
__try
{
(*TG).ArrayOfStructurs = tt; //tt already includes data
(*TG).numberOfStructure =numberOfStructure;
(*TG).handlerType = handlerType;
////Here is the problem How to move the data ???
strncpy((*TG).Name,Name,min(strlen((*TG).Name),strlen(Name)));
return TRUE;
}
__finally
{
...
}
}
//C#
[ StructLayout( LayoutKind.Sequential ,CharSet = CharSet.Ansi)]
public struct GENERAL_DATABASE_structure //GDS
{
[MarshalAs(UnmanagedType.LPStr, SizeConst=256)]
public string Name ; // TLL Module Name */
public T_GDS_Handler_TYPE handlerType;
public int numberOfStructure; //number of structures public
IntPtr ArrayOfStructurs;//handle array of GDS or TS depending on the
handlerType
}
[DllImport("CALL_DLL.dll",
EntryPoint="CALL_GetInitDatabaseList",SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.Cdecl)]
public static extern int CALL_GetDB(ref GENERAL_DATABASE_structure TG);
CALL_GENERAL_DATABASE_structure my_GDS = new
CALL_GENERAL_DATABASE_structure();//[T_InitCount];
if(CALL_Access.CALL_GetDB(ref my_GDS)!=TRUE)...
If I try to use strncpy in DLL , I receive an exception if I remove this
line from dll I get garbage !
What should I do?