10

I want to import some functions from kernel32.dll, but I want to use different names. Example function:

[DllImport("kernel32.dll")] private static extern bool ReadProcessMemoryProc64 (...);

private static bool BetterReadableAndWriteableName (...) {
    ReadProcessMemoryProc64(...);
}

Wrapping the function is what I actually don't want, if there is another way.

Cubi73
  • 1,891
  • 3
  • 31
  • 52
  • You can do this but it's probably a bad idea. How will the next reader of your code know what these functions are? – David Heffernan Jan 08 '14 at 19:45
  • 2
    There are valid cases. For example, many Win32 APIs have LPVOID/LPARAM/etc parameters (e.g. SendMessage) that can take different data types depending upon other parameters. In this case, it is often necessary to create different method signatures to support different use cases. In that case, you must use EntryPoint to rename the function (or you could declare them in different classes to avoid the name collision, but that's not always appropriate). – Michael Gunter Jan 08 '14 at 19:59

2 Answers2

19

Use the EntryPoint property of DllImportAttribute.

[DllImport("kernel32.dll", EntryPoint="ReadProcessMemoryProc64")]
private static extern bool BetterReadableAndWriteableName (...);
Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
7
[DllImport("kernel32.dll", EntryPoint = "ReadProcessMemoryProc64")] 
private static extern bool MyName(...);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964