How do I call win32 dll from .NET frame work? say kernal32.dll

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi all
How do I call win32 dll from .NET frame work? say kernal32.dll from .NET framework. (from C# and VB)
I heard that we have to use (RCW) Runtime Callable Wrapper for calling COM Components from .NET framework. but what about win32 dll
regards
Balamurali C
 
It's easy... just declare the win32 function in your application with
the attribute DllImport. Then you can call it like you would any other
method in your code. The framework will take care of marshalling the
call and any parameters to/from your application and the foreign dll.

This is how you would do it in C#:


// First you delcare the function that you want to call
// This should be done at class level
[DllImport("kernel32.dll")]
private static extern bool GetDiskFreeSpaceEx(string directory, ref
long UserSpaceFree, ref long TotalUserSpace, ref long TotalFreeSpace);

protected void SomeMethod()
{
bool ReturnedValue = GetDiskFreeSpaceEx(myExtractRoot, ref
myUserSpaceFree, ref myTotalUserSpace, ref myTotalFreeSpace);
}

In some cases, you may have to specify how you want the parameters to
be marshalled. This will be especially true when you need to send
structs. You can refer to MSDN on how to specify the marshalling
attributes. In most cases, like above, it simply works with no
problems.
 
Back
Top