Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using a legacy DLL file in a C# project

Options
  • 30-05-2011 12:05am
    #1
    Registered Users Posts: 892 ✭✭✭


    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,078 Mod ✭✭✭✭AlmightyCushion


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


  • Registered Users 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 Posts: 892 ✭✭✭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