0

I have a dll for third party app and normally communicate with it via Named Pipes. But NPs works only when the other app is started. Can I directly call a method from the dll to see its version.

C++ VS2012

#ifdef  MTFST_EXPORTS
#define MTFST_API __declspec(dllexport)
#else
#define MTFST_API __declspec(dllimport)
#endif

#define LIBRARY_VERSION      "3.0"

    ....

using namespace std;

MTFST_API char *__stdcall FST_LibraryVersion()
{
    return LIBRARY_VERSION;
}

I tried the following code, but it doesn't work. .NET 4.

internal class Program
{
    [DllImport("Library.dll")]
    private static extern char[] FST_LibraryVersion();

    private static void Main(string[] args)
    {
        Console.WriteLine(new string(FST_LibraryVersion()));
    }
}
Miroslav Popov
  • 3,294
  • 4
  • 32
  • 55

1 Answers1

1

.NET arrays aren't compatible with raw pointers. You'll need to either use IntPtr or pass in a destination buffer to the function:

void __stdcall FST_LibraryVersion(char *dest)
{
    strcpy(dest, LIBRARY_VERSION);
}

Obviously, you'll need to include checks to prevent buffer overflow.

Also, see PInvoke for C function that returns char *

Community
  • 1
  • 1
Drew McGowen
  • 11,471
  • 1
  • 31
  • 57