I need to pass string from Delphi to C# DLL and return integer in return. Its been awhile since I worked with DLL and google is not helping much. Any idea how to do this. An example will help. I did managed to created DLL in C# and pass and return integer from Delphi to C# DLL. Thanks.

Comments

  1. Export the C# dll as COM, define your string as WideString in Delphi, and BSTR in C#.

    ReplyDelete
  2. Otherwise, you'll have to pass a PChar on the Delphi side, and something like a [MarshalAs(WideCharPtr)]StringBuilder  or something to that effect.

    ReplyDelete
  3. Thanks Bourchez, Martijn. I tried PChar from Delphi and below code in C# and still its not working . I keep getting external exception E0434352 error.

    public static int Test ([MarshalAs(UnmanagedType.BStr)] string teststr)  {return 1;}

    ReplyDelete
  4. Arun Singh There's a reason why Martijn Coppoolse specified "WideCharPtr" and not "BStr" like A. Bouchez did.

    A BStr is a special COM type with specific memory management: http://msdn.microsoft.com/en-us/library/windows/desktop/ms221069%28v=vs.85%29.aspx

    ReplyDelete
  5. You are using UnmanagedExports right?

    ReplyDelete
  6. My C# code looks like this
    using System;
    namespace TestDLL
    {
        public class Class1
        {
             [DllExport]
             public static string Test(string sString)
            {
                      return sString;
             }
            }
    }

    ReplyDelete
  7. That's not going to work. Can't we do this on Stack Overflow? It's much easier there and it lives on for the benefits of others.

    ReplyDelete
  8. David Heffernan Yes I did put there about my initial problem of working with DLL created by C#. Now I am stuck with managing string in DLL. So came here to my home town where its like family & people are less  rude :)

    ReplyDelete
  9. A. Bouchez Do I need to use Regasm.exe for exporting C# DLL as COM. This is really getting dirty :)

    ReplyDelete
  10. If you don't want to write COM by yourself. Just refer to below link:

    https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports

    it may be easier to you.

    ReplyDelete
  11. Thanks Sam Shaw . That's what originally David Heffernan mentioned and I thought that I did just that. I did Install-Package UnmanagedExports and changed code as in my above comment.  still getting errors.

    ReplyDelete
  12. well, If you decided to use that to export unmanaged function, you should really take deeply looking at the link  i posted, which include some sample codes and remark.

    There are some key words you should know about.

     [DllExport("XXXXXXX", CallingConvention = CallingConvention.Cdecl)]
     
     [return: MarshalAs(UnmanagedType.BStr)]

    I gotta go to bed. Good luck. Or you just post to stackoverflow, I think David will post full source code answer there. ;)

    ReplyDelete
  13. before I shutdown my computer, just googling an example which is similar to your case. 

    http://stackoverflow.com/questions/20776847/returning-a-string-from-a-c-sharp-dll-with-unmanaged-exports-to-inno-setup-scrip

    FYR 

    NOTE: calling convention is important and depends on delphi corresponding function decaration.

    Good night.

    ReplyDelete
  14. Thanks Sam Shaw . I will look into that.

    ReplyDelete
  15. Note that InnoSetup is written... in Delphi... :)

    ReplyDelete

Post a Comment