Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ModuleBuilder::DefinePInvokeMethod Method (String^, String^, String^, MethodAttributes, CallingConventions, Type^, array<Type^>^, CallingConvention, CharSet)

 

Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public:
MethodBuilder^ DefinePInvokeMethod(
	String^ name,
	String^ dllName,
	String^ entryName,
	MethodAttributes attributes,
	CallingConventions callingConvention,
	Type^ returnType,
	array<Type^>^ parameterTypes,
	CallingConvention nativeCallConv,
	CharSet nativeCharSet
)

Parameters

name
Type: System::String^

The name of the PInvoke method. name cannot contain embedded nulls.

dllName
Type: System::String^

The name of the DLL in which the PInvoke method is defined.

entryName
Type: System::String^

The name of the entry point in the DLL.

attributes
Type: System.Reflection::MethodAttributes

The attributes of the method.

callingConvention
Type: System.Reflection::CallingConventions

The method's calling convention.

returnType
Type: System::Type^

The method's return type.

parameterTypes
Type: array<System::Type^>^

The types of the method's parameters.

nativeCallConv
Type: System.Runtime.InteropServices::CallingConvention

The native calling convention.

nativeCharSet
Type: System.Runtime.InteropServices::CharSet

The method's native character set.

Return Value

Type: System.Reflection.Emit::MethodBuilder^

The defined PInvoke method.

Exception Condition
ArgumentException

The method is not static or if the containing type is an interface or if the method is abstract of if the method was previously defined.

ArgumentNullException

name or dllName is null.

InvalidOperationException

The containing type has been previously created using CreateType

Some DLL import attributes (see the description of DllImportAttribute) cannot be specified as arguments to this method. Such attributes should be set by emitting a custom attribute for the method. For example, the DLL import attribute PreserveSig is set by emitting a custom attribute.

System_CAPS_noteNote

Starting with the .NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag::ReflectionEmit flag. (See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5 or later.

The following example illustrates the use of the DefinePInvokeMethod method to create a MethodBuilder for an external unmanaged method, MessageBoxA, in the Win32 API. The example displays a message box with Retry and Cancel buttons, and displays the return value from the message box.

System_CAPS_importantImportant

To get a non-zero return value, you must add MethodImplAttributes::PreserveSig to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder::GetMethodImplementationFlags and MethodBuilder::SetImplementationFlags methods.

This example uses a different overload of the DefinePInvokeMethod method, but the technique is the same.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

const int MB_RETRYCANCEL = 5;

void main()
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName("TempAssembly");

   // Define a dynamic assembly in the current application domain.
   AssemblyBuilder^ myAssemblyBuilder = 
      AppDomain::CurrentDomain->DefineDynamicAssembly(
                  myAssemblyName, AssemblyBuilderAccess::Run);

   // Define a dynamic module in "TempAssembly" assembly.
   ModuleBuilder^ myModuleBuilder = 
      myAssemblyBuilder->DefineDynamicModule("TempModule");

   array<Type^>^ paramTypes = 
      { int::typeid, String::typeid, String::typeid, int::typeid };

   // Define a PInvoke method.
   MethodBuilder^ piMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
      "MessageBoxA",
      "user32.dll",
      MethodAttributes::Public | MethodAttributes::Static | MethodAttributes::PinvokeImpl,
      CallingConventions::Standard,
      int::typeid,
      paramTypes,
      CallingConvention::Winapi,
      CharSet::Ansi);

   // Add PreserveSig to the method implementation flags. NOTE: If this line
   // is commented out, the return value will be zero when the method is
   // invoked.
   piMethodBuilder->SetImplementationFlags(
      piMethodBuilder->GetMethodImplementationFlags() | MethodImplAttributes::PreserveSig);

   // Create global methods.
   myModuleBuilder->CreateGlobalFunctions();

   // Arguments for calling the method.
   array<Object^>^ arguments = 
      { (Object^)(int) 0, "Hello World", "Title", MB_RETRYCANCEL };

   MethodInfo^ pinvokeMethod = myModuleBuilder->GetMethod("MessageBoxA");
   Console::WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
   Console::WriteLine("Message box returned: {0}", 
      pinvokeMethod->Invoke(nullptr, arguments));
};


/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show:
© 2017 Microsoft