CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jun 2003
    Location
    Bangalore,INDIA
    Posts
    122

    Structures in C#

    Hello Everyone,

    I have made a DLL in VC++, in which one function requires a struct variable to be passed, and i want to use that DLL in my c# application. When i try to use that DLL in c# application, it is giving error as Bad Pointer...
    Does anybody have solution to this....


    Thanks in Advance,
    Poornima .S

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Structures in C#

    either use C++/CLI to expose the C++ dll to C#, or p/invoke it and create structures in C# that are laid out the same as the structures in C++ and use that to pass data into the p/invoked function.

  3. #3
    Join Date
    Apr 2006
    Posts
    220

    Re: Structures in C#

    Quote Originally Posted by MadHatter View Post
    either use C++/CLI to expose the C++ dll to C#, or p/invoke it and create structures in C# that are laid out the same as the structures in C++ and use that to pass data into the p/invoked function.
    What do you mean by "use C++/CLI to expose the C++ dll to C#". Is not it the same thing the poster tried.

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Structures in C#

    no, you can create a .net assembly using C++ which is different than a native C++ dll which the OP has.

  5. #5
    Join Date
    Apr 2006
    Posts
    220

    Re: Structures in C#

    Quote Originally Posted by MadHatter View Post
    no, you can create a .net assembly using C++ which is different than a native C++ dll which the OP has.
    Now I get your point. You wanted to say Managed C++.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

  7. #7
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Structures in C#

    paste your code here did you used the extern when prototyping the function in c#?
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

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

    Re: Structures in C#

    If you want C# to talk to a native C++ dll then try using the interop assistant from Microsoft.

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

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

    Re: Structures in C#

    Hello Everyone thanks for your help..

    This what my code looks like

    //Structure declared in the VC++ DLL
    struct EmpDetails
    {
    unsigned char * bNAME;
    unsigned char * bAGE;
    unsigned char * bDOJ;
    unsigned char * bDESIGNATATION;
    unsigned char * bGENDER;
    unsigned char * bSALARY;

    };

    //A Function in the same DLL that takes the above declared structure as input
    int __stdcall FnWriteEmpDetails(const EmpDetails &stEmpDtls, unsigned char * bReturnVal);

    I have done a Wrapper class for that, the function declaration is as below.

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

    And my structure definition in Wrapper class in c# is :

    public struct EmpDetails
    {
    public byte [] bNAME;
    public byte [] bAGE;
    public byte [] bDOJ;
    public byte [] bDESIGNATATION;
    public byte [] bGENDER;
    public byte [] bSALARY;

    };

    When i try to use this function in C#, it is not taking the structure variable, my test app is giving the following error

    An unhandled exception of type 'System.NullReferenceException' occurred in TEST.exe

    Additional information: Object reference not set to an instance of an object.

    Is there any solution for this?
    Last edited by cheery_poori; January 27th, 2009 at 05:09 AM.

  10. #10
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Structures in C#

    Quote Originally Posted by nabeelisnabeel View Post
    Now I get your point. You wanted to say Managed C++.

    Technically speeking "Managed C++" is the language that was used with .NET 1.X and is no longer used.

    The current language (.NET 2.0 thru 4.0) is C++/CLI.

    The two are radically different. Originally Microsoft did not follow the "Extensibility guidelines" for C++ and created a very strange beast. C++/CLI on the other hand properly (well almost) follows the guidelines.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  11. #11
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Structures in C#

    C++/CLI is integration of native and managed programming.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

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

    Re: Structures in C#

    Your marshalling is incorrect.

    I'm presuming that your structure is made up of null terminated C++ strings ? In which case your structure should look like :

    Code:
    [StructLayout(LayoutKind.Sequential)]
    struct EmpDetails
    {
        [MarshalAs(UnmanagedType.LPStr)]
        string bNAME;
    
        [MarshalAs(UnmanagedType.LPStr)]
        string bAGE;
    
        [MarshalAs(UnmanagedType.LPStr)]
        string bDOJ;
    
        [MarshalAs(UnmanagedType.LPStr)]
        string bDESIGNATATION;
    
        [MarshalAs(UnmanagedType.LPStr)]
        string bGENDER;
    
        [MarshalAs(UnmanagedType.LPStr)]
        string bSALARY;
    };
    The reason why you were getting a null-reference exeption in your case was you probably didn't assign anything to the byte [] arrays, so they were all null when passed into the method hence 'null exception'.

    I could write a book on interop so I'm not going into details about this solution. Just remember you have to set each of the fields in the structure to a string before you pass it into your function.

    Your function signature is also incorrect : but without knowing what 'bReturnVal' is (whether it is a single character to be output, or whether you have to pass an already allocated block of unsigned char memory to be filled by the function) I can't give you an interop signature for this.

    i.e. in C++ whether you do

    Code:
    EmpDetails details;
    char returnVal = 0;
    FnWriteEmpDetails(stEmpDtls, &returnVal);
    or

    Code:
    EmpDetails details;
    std::vector<char> returnVal;
    returnVal.resize(10); // or some size
    FnWriteEmpDetails(stEmpDtls, &returnVal.front());
    If it's a single character it would be :

    Code:
    [DllImport("TEST_DLL.dll")]
    public static extern int FnWriteEmpDetails(ref EmpDetails stEmpDtls, [Out] out char bReturnVal);
    If it's a character array it would be :

    Code:
    [DllImport("TEST_DLL.dll")]
    public static extern int FnWriteEmpDetails(ref EmpDetails stEmpDtls, 
        [In][MarshalAs(UnmanagedType.LPArray)] byte [] bReturnVal);
    
    // call like this :
    EmpDetails stEmpDtls = new EmpDetails();
    stEmpDtls.bNAME = "Name";
    // fill in the other fields
    
    byte [] buffer = new byte[10]; // or whatever size
    FnWriteEmpDetails(ref EmpDetails, buffer);
    Notice the 'ref' ? This is needed because the parameter stEmpDtls is being passed by reference.

    As I said before for most cases you can either use the microsoft interop assistant of go to www.pinvoke.net for information.

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

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

    Re: Structures in C#

    Hello Darwen,

    My Structure in C++ looks as below

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

    };

    So in C# wrapper class, for unsigned char array, which type i should take for marshalling?

    I tried taking different types and but it didn't work.
    bReturnVal is an output parameter...there is no problem with that

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

    Re: Structures in C#

    I gave you the EmpDetails struct in C# in my last post......

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

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

    Re: Structures in C#

    Hello Darwen,

    See if i want to give bDOJ as 22/01/2009, then in my dll function i want the day as 22, but if i pass as string in c# it won't take it as 22 instead it will take that as 2 so is there any other way to pass a byte value using marshalling.

    Thanks & Regards,
    Poornima.

Page 1 of 2 12 LastLast

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