DllImport and char*
Asked Answered
E

4

8

I have a method I want to import from a DLL and it has a signature of:

BOOL GetDriveLetter(OUT char* DriveLetter)

I've tried

    [DllImport("mydll.dll")]
    public static extern bool GetDriveLetter(byte[] DriveLetter);

and

    [DllImport("mydll.dll")]
    public static extern bool GetDriveLetter(StringBuilder DriveLetter);

but neither returned anything in the DriveLetter variable.

Eduino answered 2/4, 2010 at 18:35 Comment(0)
R
8

It appears the function GetDriveLetter is expecting a char* which points to sufficient memory to contain the drive letter.

I think the easiest way to approach this problem is to pass a raw IntPtr and wrap the calls to GetDriveLetter in an API which takes care of the resource management and conversion to a string.

[return:MarshalAsAttribute(UnmanagedType.Bool)]
private static extern bool GetDriveLetter(IntPtr ptr);

public static bool GetDriveLetter(out string drive) {
  drive = null;
  var ptr = Marshal.AllocHGlobal(10);
  try {
    var ret = GetDriveLetter(ptr);
    if ( ret ) {
      drive = Marshal.PtrToStringAnsi(ptr);
    }
    return ret;
  } finally { 
    Marshal.FreeHGlobal(ptr);
  }
}
Rialto answered 2/4, 2010 at 18:41 Comment(3)
@Malfist, The value I1 tells the CLR to marshal the value as a 1 byte integer. It was actually incorrect in this sample as I4 is the correct value (updated a bit ago). As to why check out this blog entry I wrote on marshalling bool values: blogs.msdn.com/jaredpar/archive/2008/10/14/…Rialto
When I make it I4, I get MarshalDirectiveExceptionEduino
@Malfist, sorry, it should be UnmanagedType.Bool. Been one of those days.Rialto
S
1

The StringBuilder is probably the way to go, but you have to set the capacity of the string builder before calling the function. Since C# has no idea how much memory that GetDriveLeter will use, you must make sure the StringBuilder has enough space. The marshaller will then pass a char* allocated to that length to the function and marhsall it back to the StringBuilder.

[return:MarshalAsAttribute(UnmanagedType.I4)]
private static extern bool GetDriveLetter(StringBuilder DriveLetter);

public static bool GetDriveLetter(out string driverLetter) {
  StringBuilder buffer = new StringBuilder(10);
  bool ret = GetDriveLetter(buffer);
  driveLetter = buffer.ToString();
  return ret;
}

See the p/invoke sample for GetWindowText(), for an example.

Studious answered 2/4, 2010 at 18:56 Comment(3)
Doesn't return the correct string, unlike the accepted answer.Eduino
What if you add CharSet=CharSet.Ansi to the DllImport attribute? That is if you care since you have a working solution.Studious
adding CharSet.Ansi to the DllImport makes no difference.Eduino
W
0
[DllImport("mydll.dll")]
public static extern bool GetDriveLetter([MarshalAs(UnmanagedType.LPStr)]  string DriveLetter)
Whereto answered 8/3, 2013 at 16:15 Comment(0)
M
0

I had a related situation where the function had a char * parameter and I was missing the CharSet assignment:

[DllImport("library.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr MyFunc(string myString);
Mensa answered 23/4, 2020 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.