CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Structures in C#

    Sorry, I don't understand your last post.

    If you pass bDOJB as "22/01/2009" then it's a string right ?

    Or is there some kind of encoding which is used in C++ to turns the date 22/01/2009 into an unsigned char buffer ?

    Can you show the C++ code for doing this ?

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  2. #17
    Join Date
    Jun 2003
    Location
    Bangalore,INDIA
    Posts
    122

    Re: Structures in C#

    Hello Darwen,

    I will explain in detail..

    I have created a DLL Which has the following structure

    struct EmpDetails
    {
    unsigned char * bNAME;
    unsigned char * bAGE;
    unsigned char * bDOJ;
    unsigned char * bDESIGNATATION;
    unsigned char * bGENDER;
    unsigned char * bSALARY;

    };

    and a Function to which the above said structure is passed as input the function signature is as follows:

    int __stdcall FnWriteEmpDetails(const EmpDetails &stEmpDtls, unsigned char * bReturnVal);

    Here, the bReturnval is an output parameter there is no problem with this..

    So now i want to use the DLL in C# application.
    I have done a Wrapper Class in C# for the DLL.

    The structure in c# looks like below:

    public struct EmpDetails
    {
    [MarshalAs(UnmanagedType.LPStr)]
    public string bNAME;

    [MarshalAs(UnmanagedType.LPStr)]
    public string bAGE;

    [MarshalAs(UnmanagedType.LPStr)]
    public string bDOJ;

    [MarshalAs(UnmanagedType.LPStr)]
    public string bDESIGNATATION;

    [MarshalAs(UnmanagedType.LPStr)]
    public string bGENDER;

    [MarshalAs(UnmanagedType.LPStr)]
    public string bSALARY;

    };

    And the function signature in C# is

    [DllImport("TEST_DLL.dll")]
    //int __stdcall FnWriteEmpDetails( const EmpDetails &stEmpDetails, unsigned char * bReturnVal);
    public static extern int FnWriteEmpDetails(ref EmpDetails stEmpDetails, byte[] bReturnVal);


    The DLL function internally just takes the input provided and send it to some device, here the data needs to be sent in bytes,

    iCount = strlen((char*)stEmpDetails.bDOJ);
    for(i, j=0; j< (iCount+1); j++,i++)
    bData[i] = stEmpDetails.bDOJ[j];

    so if i pass the bDOJ as string "22/1/2009" incorrect data will be taken. I want to the day as "22" in one byte.

    I hope i have explained the problem properly...

    Thanks & Regards,
    Poornima .S

  3. #18
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Structures in C#

    Please use code tags in future.

    Try this for the struct :

    Code:
    using System.Runtime.InteropServices;
    
    public struct EmpDetails
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string bNAME;
    
        [MarshalAs(UnmanagedType.LPStr)]
        public string bAGE;
    
        public IntPtr bDOJ;
    
        [MarshalAs(UnmanagedType.LPStr)]
        public string bDESIGNATATION;
    
        [MarshalAs(UnmanagedType.LPStr)]
        public string bGENDER;
    
        [MarshalAs(UnmanagedType.LPStr)]
        public string bSALARY;
    };
    
    // code to translate byte [] to bDObj
    
    byte [] byteArray = new byte[4];
    GCHandle handle = GCHandle.Alloc(byteArray, GCHandleType.Pinned);
    
    try
    {
        EmpDetails empDetails = new EmpDetails();
        empDetails.bDOJ = handle.AddrOfPinnedObject();
        // make function call
    }
    finally
    {
        handle.free();
    }
    Usually I'd have bDOJ as a byte [] array in the struct, but since you have to pass it by reference I don't believe this will work. P/Invoke will try to marshal the byte [] array from native to managed code on the way out of the function which it can't do because it doesn't know how big it is. So P/Invoke will most probably throw an exception at this point.

    Darwen.
    Last edited by darwen; January 28th, 2009 at 07:13 AM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured