CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Apr 2010
    Posts
    11

    Angry C# System.String parameter passed into an unmanaged C++ DLL

    Lost..... How do I call an unmanaged C++ DLL class member function from C# code? I can change any and all of these definitions. I just need to get a string from the C# world to the C++ world without pulling out the rest of my hair.

    Using VS 2008

    Unmanaged C++ Code in a DLL:
    class MyClass
    {
    public:
    bool Foo( char* str);
    }

    C# code that won't compile:

    System.String test = "Hi there!";
    MyClass c = new MyClass();
    c.Foo( test );

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Well, if the string will be modified from within the C++ dll you should pass in a StringBuilder. Anyhow, that does not seem to be your problem as you say your code does not compile. If that is the case, your function definition in C# is incorrect, so show us that.

  3. #3
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Don't have one.

    Why, you ask? Since I have included the DLL as a reference, I didn't think I needed a C# function definition. I'm calling all kinds of other functions from the DLL with no problem. If I leave out the string param or change it to an int, everything works as expected. It's just this string stuff that's stopping me.

    Oh, and it won't be modified, just passed.

    Disclosure: Long time C/C++ guy. Not a C# expert. Feel free to learn me. I'm all ears.

    Thanks.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Could you include all relevant C# code? You're missing all the important bits...
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    I'm confused. What "important bits" am I missing? I thought I had included all that was needed, else I would have included the rest. Can you be more specific?

    Thanks.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    How about the compile error for starters?

  7. #7
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Der... yeah. That would make sense:

    Error The best overloaded method match for 'MyClass.MyClassClass.Foo(sbyte*)' has some invalid arguments
    Error Argument '1': cannot convert from 'string' to 'sbyte*'

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Quote Originally Posted by thumperj View Post
    I'm confused. What "important bits" am I missing? I thought I had included all that was needed, else I would have included the rest. Can you be more specific?

    Thanks.
    Pretty much everything

    Are you compiling your C++ as C++/CLI? How are you invoking the C++ function from C#. Whats the full class declaration in C# including the definition of the method you're calling. You've already provided the error which was also needed Can you also include the source for functions which you can call successfully?
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Hmmm.... I guess I'm even more confused than I thought I was. I thought I had answered all those questions. Let me try again.

    The C++ is unmanaged, which is what I thought meant it was not CLI. It's definitely not CLI. Just plain ol' C++.

    I'm invoking the function from C# exactly like in the example in my original post. I new the object and call the function.

    I don't have a class definition in C# for the C++ object. It's included as a Reference as the DLL. I never needed to put anything in the C# except "using MyClass;"

    As for an example of functions I can call successfully, just take out the char* param in the C++ DLL function and the string param out of the C# code and I can call that same function all day. In fact, change it all to an int and I can call it all day. It's the string parameter that's halting my progress.

  10. #10
    Join Date
    Aug 2008
    Posts
    902

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Quote Originally Posted by thumperj View Post
    Hmmm.... I guess I'm even more confused than I thought I was. I thought I had answered all those questions. Let me try again.

    The C++ is unmanaged, which is what I thought meant it was not CLI. It's definitely not CLI. Just plain ol' C++.

    I'm invoking the function from C# exactly like in the example in my original post. I new the object and call the function.

    I don't have a class definition in C# for the C++ object. It's included as a Reference as the DLL. I never needed to put anything in the C# except "using MyClass;"

    As for an example of functions I can call successfully, just take out the char* param in the C++ DLL function and the string param out of the C# code and I can call that same function all day. In fact, change it all to an int and I can call it all day. It's the string parameter that's halting my progress.
    If the DLL was written in "plain ol' C++" then you could only import functions using P/Invoke like so

    Code:
    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref POINT lParam);

  11. #11
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Yes, ok. The /clr flag is set so it apparently does support CLR.

  12. #12
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Instead of continuing the back and forth, I've created a simple example that demostrates the issue I'm trying to resolve. I've attached it to this thread.

    Check out Form1.cs, line 27. Just uncomment the line and you'll see the compiler error.

    Thanks for any direction.
    Attached Files Attached Files

  13. #13
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    just do a marshalling for the string parameter on the c# side like this and see :

    Code:
     
     
                [MarshalAs(UnmanagedType.LPStr)]System.String  test = "Hi there!";

  14. #14
    Join Date
    Apr 2010
    Posts
    11

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    Thanks, vcdebugger. After your suggestion I've been off reading about MarshalAs. All new to me.

    However, unless I've misunderstood or misplaced something, adding that to the code doesn't help with the compiler error referencing Foo:
    >> Error: The best overloaded method match for 'MyClass.Class1.Foo(sbyte*)' has some invalid arguments

    You can check out the attached zip file to see the error. It's all laid out nicely in the code, short, sweet and right to the point.

  15. #15
    Join Date
    Apr 2010
    Location
    Dayton, OH
    Posts
    16

    Re: C# System.String parameter passed into an unmanaged C++ DLL

    If you were calling an exported function from the DLL, you would use the PInvoke as described by Chris. However, since you want to execute a member function of a class inside your DLL, things may be a bit more complicated.

    Basically, you can either compile your C++ DLL with /clr. Or, you have to create another C++/CLI DLL that wraps your native DLL in a .NET class. The mechanism is frequently referred to as "It Just Works" or "IJW". Generally in these wrapper DLLs, you have to marshal the data types manually. All in all, it can be a real pain the first time you do it.

    This article seems to have the information you need. Be prepared, it's a long read:
    http://www.codeproject.com/KB/mcpp/quickcppcli.aspx

Page 1 of 2 12 LastLast

Tags for this Thread

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