Solved

Passing C# Structure to a C Dll as char*

Posted on 2004-10-20
9
405 Views
Last Modified: 2010-04-15
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.Sequential, CharSet=CharSet.Auto)]
public struct TuxMid_h
      {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=9)]
            public string display_id;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=9)]
            public string tyouhyou_id ;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=33)]
            public string service ;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=4)]
            public string timer_flag ;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=9)]
            public string user_id ;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=21)]
            public string msgid ;
      }

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
      public struct SendInfo
      {
            [MarshalAs(UnmanagedType.LPStr)]
            public string Syori ;      //As String * 1       ' Processing classification
            [MarshalAs(UnmanagedType.LPStr)]
            public string Message_Cd ; //As String * 6       ' Message Code
            [MarshalAs(UnmanagedType.LPStr)]
            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




0
Comment
Question by:ketsh
  • 3
  • 3
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 12355795
What is the difference between TuxMid_h and SendInfo structures? In C code char* pointer is casted to SendInfo*. But C# code should pass these two structures by the same way. Your code passes TuxMid by reference and SendInfo by value. Why?
Please show also C SendInfo definition.
0
 

Author Comment

by:ketsh
ID: 12355957
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?

0
 

Author Comment

by:ketsh
ID: 12356037
One more information.
All structures have only String as data members.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 48

Expert Comment

by:AlexFM
ID: 12356063
Do you have SendInfo C definition?
0
 

Author Comment

by:ketsh
ID: 12356191
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
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 12356298
I think you should pass this structure by reference. To pass fixes size strings use the way described in this article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp
See: Structures with Embedded Char Arrays

It should be something like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct public struct SendInfo
{
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
   public string Syori;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 6)]
   public string Message;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
   public string Message;
}

I guess all strings in C structure are char* and not wchar*. This is a reason I select CharSet = CharSet.Ansi and not Auto.
0
 
LVL 11

Expert Comment

by:Buffon
ID: 12369887
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now