Marshaling structure from C DLL (Need help!!!)

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?
 
G

Guest

Here is something I have used in the past

==
== CPP DLL source
==

#include <stdio.h>

extern "C" __declspec(dllexport) void TestDataPassingEntry

(unsigned char * pabData,
int iDataLength)

{
printf("Hello world\n");
for (int iNdx = 0; iNdx < 10; iNdx++)
*(pabData + iNdx) = 48 + iNdx;
*((unsigned int *) (pabData+64)) = 0xA1B2C3D4;
} // extern "C" __declspec(dllexport) void TestDataPassingEntry ()

==
== C# program
==

using System;
using System.Runtime.InteropServices;
namespace TestingDataProject
{
public class TestDataPassing
{
[DllImport("TestDataPassingDLL.dll")]
public static extern void TestDataPassingEntry(ref MyStruct TestData, int
iDataLength);
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.ByValArray,SizeConst=64)]
public byte [] abMyData;
public int iMyLength;
public MyStruct (int iDummy)
{
abMyData = new byte[64];
iMyLength = 64;
} // public MyStruct ()
} // public struct MyStruct
[STAThread]
static void Main(string[] args)
{
MyStruct MyTestData = new MyStruct(1);

TestDataPassingEntry(ref MyTestData, 64);
Console.WriteLine(MyTestData.abMyData[0]);
Console.WriteLine("{0,8:X}",MyTestData.iMyLength);
} // public static void Main ()
} // public class TestDataPassing
} // namespace TestingDataProject
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top