ketsh
asked on
Passing C# Structure to a C Dll as char*
Hi,
I have a C Dll Function with following declaration in C.
-------------------
int MyDllFunction(TuxMid_usr_h *usr_sysh,char *sdata,long slen,char *rdata, long *rlen)
-------------------
first parameter is a structure defined in C and in C# both. That is passed fine without any problems. Second parameter is a pointer to structure. It can point to different structures based on scneario. And this has different actions to perform based on the structure received.
This DLL works very well with Visual Basic 6.0. I have to use the same DLL without any change in C#.
First scneario where i have to use this is as following:
-------------------------- ---------- ---------- ---------- ---------- ----
Structure in C# -
[StructLayout(LayoutKind.S equential, CharSet=CharSet.Auto)]
public struct TuxMid_h
{
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=9)]
public string display_id;
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=9)]
public string tyouhyou_id ;
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=33)]
public string service ;
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=4)]
public string timer_flag ;
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=9)]
public string user_id ;
[MarshalAs(UnmanagedType.B yValTStr, SizeConst=21)]
public string msgid ;
}
[StructLayout(LayoutKind.S equential, CharSet=CharSet.Auto)]
public struct SendInfo
{
[MarshalAs(UnmanagedType.L PStr)]
public string Syori ; //As String * 1 ' Processing classification
[MarshalAs(UnmanagedType.L PStr)]
public string Message_Cd ; //As String * 6 ' Message Code
[MarshalAs(UnmanagedType.L PStr)]
public string intCnt; //As String * 5 ' Frequency of inquiry
}
-------------------------- ---------- ---------- ---------- ---------- -------
Function declared in following way in C#:
[DllImport("E:\\RawDLL\\JR _tpcltDll\ \Debug\\JR _tpcltDll. dll") ]
public static extern long MyDllFunction(
//[MarshalAs(UnmanagedType .Struct)]
ref TuxMid_h User_h,
//[MarshalAs(UnmanagedType .Struct)]
SendInfo sendData,
//[MarshalAs(UnmanagedType .I8)]
long slen,
//[MarshalAs(UnmanagedType .Struct )]
ref ReceiveInfo rdata,
//[MarshalAs(UnmanagedType .I8)]
ref long rlen);
-------------------------- ---------- ---------- ---------- ---------- ---------
I assigned values to my structure members.
For ex
senddata lsdata = new senddata();
lsdata.Syouri = "1";
lsdata.Message_Cd = "ABC";
lsdata.intCnt = "20";
And I pass this lsdata to function in second parameter.
When I debug this process using VC++ and exe of above code. It receives only "1" and other members of the structure are not received.
I have also tried marshling it to ByValTstr type. Still it gives us same output.
Can any one please point out where I'm going wrong in passing this structure as char * to C function. We are using .Net framework 1.1 version.
This is very urgent. Please suggest solutions as early as possible.
Thanks in advance.
Ketan
I have a C Dll Function with following declaration in C.
-------------------
int MyDllFunction(TuxMid_usr_h
-------------------
first parameter is a structure defined in C and in C# both. That is passed fine without any problems. Second parameter is a pointer to structure. It can point to different structures based on scneario. And this has different actions to perform based on the structure received.
This DLL works very well with Visual Basic 6.0. I have to use the same DLL without any change in C#.
First scneario where i have to use this is as following:
--------------------------
Structure in C# -
[StructLayout(LayoutKind.S
public struct TuxMid_h
{
[MarshalAs(UnmanagedType.B
public string display_id;
[MarshalAs(UnmanagedType.B
public string tyouhyou_id ;
[MarshalAs(UnmanagedType.B
public string service ;
[MarshalAs(UnmanagedType.B
public string timer_flag ;
[MarshalAs(UnmanagedType.B
public string user_id ;
[MarshalAs(UnmanagedType.B
public string msgid ;
}
[StructLayout(LayoutKind.S
public struct SendInfo
{
[MarshalAs(UnmanagedType.L
public string Syori ; //As String * 1 ' Processing classification
[MarshalAs(UnmanagedType.L
public string Message_Cd ; //As String * 6 ' Message Code
[MarshalAs(UnmanagedType.L
public string intCnt; //As String * 5 ' Frequency of inquiry
}
--------------------------
Function declared in following way in C#:
[DllImport("E:\\RawDLL\\JR
public static extern long MyDllFunction(
//[MarshalAs(UnmanagedType
ref TuxMid_h User_h,
//[MarshalAs(UnmanagedType
SendInfo sendData,
//[MarshalAs(UnmanagedType
long slen,
//[MarshalAs(UnmanagedType
ref ReceiveInfo rdata,
//[MarshalAs(UnmanagedType
ref long rlen);
--------------------------
I assigned values to my structure members.
For ex
senddata lsdata = new senddata();
lsdata.Syouri = "1";
lsdata.Message_Cd = "ABC";
lsdata.intCnt = "20";
And I pass this lsdata to function in second parameter.
When I debug this process using VC++ and exe of above code. It receives only "1" and other members of the structure are not received.
I have also tried marshling it to ByValTstr type. Still it gives us same output.
Can any one please point out where I'm going wrong in passing this structure as char * to C function. We are using .Net framework 1.1 version.
This is very urgent. Please suggest solutions as early as possible.
Thanks in advance.
Ketan
ASKER
TUXMid_h structure is declared in C code also as well as in C# code.
char* senddata is a pointer received in c dll to structure.
This structure which is sent differes from scnerio to scneario. In the example i gave above it has 1 structure. In other scnaerio it is another structure. For ex. When this function is called for say Search functionality it received SendInfo. When this function is called for Register functionality it received different structure. And there are 2 other scnerios like this. ( Modify and Delete).
So structure changes but still receving end parameter is char *sdata in C.
There was a ref argument before senddata also. But during our trial and error we removed it. Though we pass it with ref we get same result.
Is it clear?
This works very well with VB. I can pass different structures at different times from VB to same function. Though those structures are not declared in C dll as it is.
Any furthur queries?
Any suggestion?
char* senddata is a pointer received in c dll to structure.
This structure which is sent differes from scnerio to scneario. In the example i gave above it has 1 structure. In other scnaerio it is another structure. For ex. When this function is called for say Search functionality it received SendInfo. When this function is called for Register functionality it received different structure. And there are 2 other scnerios like this. ( Modify and Delete).
So structure changes but still receving end parameter is char *sdata in C.
There was a ref argument before senddata also. But during our trial and error we removed it. Though we pass it with ref we get same result.
Is it clear?
This works very well with VB. I can pass different structures at different times from VB to same function. Though those structures are not declared in C dll as it is.
Any furthur queries?
Any suggestion?
ASKER
One more information.
All structures have only String as data members.
All structures have only String as data members.
Do you have SendInfo C definition?
ASKER
I dont have C definition.
But I have Definition of how it is used in VB.
Type SEND_INF
Syori As String * 1
Message_Cd As String * 6
intCnt As String * 5
End Type
But I have Definition of how it is used in VB.
Type SEND_INF
Syori As String * 1
Message_Cd As String * 6
intCnt As String * 5
End Type
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think also you should pass the struct by reference, but I think you should pass it as IntPtr in c#
and get it as DWORD in c.
and get it as DWORD in c.
Please show also C SendInfo definition.