Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Using a legacy DLL file in a C# project

  • 30-05-2011 12:05AM
    #1
    Registered Users, Registered Users 2, Paid Member Posts: 893 ✭✭✭


    Hi, I'm trying to use a legacy dll file in a Visual C# project.

    This is how the code was provided for use in a C++ environment:
    [DllImport("OldDll.dll")]
    public static extern uint DLL_SaveInfo(void * objptr, Char* filename);
    
    ... it is called later as:
    DLL_SaveInfo(instance, "C:\\Logfile.txt")))
    
    Of course, C# is not happy with "Char*", so I am not sure of the correct way to use this in C#.

    I have tried replacing Char* with String - and it will compile, but the data is not put in the path specified (not sure where in memory it ends up?!). :confused:

    I have also tried using a variable instead of a String Literal as the path, but this has the same result.

    I don't have access to the source of the DLL.

    Is there another method of getting this to work in C# that I am missing?

    Any help is appreciated!


Comments

  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 35,257 Mod ✭✭✭✭AlmightyCushion


    You use string for const char*. For char* stringbuilder should work.


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Something like this?
    [DllImport("OldDll.dll")]
    public static extern uint DLL_SaveInfo(IntPtr objptr, [MarshalAs(UnmanagedType.LPStr)] string filename);
    

    The alternative is to write a C++/CLI wrapper DLL that can call the native DLL functions without any P/Invoke stuff.

    So: C# App <-- Wrapper DLL --> Native DLL

    I prefer P/Invoke personally, but it can take a few shots to get the param marshalling right.

    Regarding using Stringbuilder. That's only required for out parameters.


  • Registered Users, Registered Users 2, Paid Member Posts: 893 ✭✭✭Doodlebug


    Thanks for the replies - I will post an update when I get a solution up and running - still getting my head around this! I'm learning that I have a lot more to learn! :)


Advertisement