Export (0) Print
Expand All

TypeBuilder.DefinePInvokeMethod Method

Defines a PInvoke method.

Overload List

Defines a PInvoke method given its 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.

[Visual Basic] Overloads Public Function DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type(), CallingConvention, CharSet) As MethodBuilder
[C#] public MethodBuilder DefinePInvokeMethod(string, string, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet);
[C++] public: MethodBuilder* DefinePInvokeMethod(String*, String*, MethodAttributes, CallingConventions, Type*, Type[], CallingConvention, CharSet);
[JScript] public function DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) : MethodBuilder;

Defines a PInvoke method given its 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.

[Visual Basic] Overloads Public Function DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type(), CallingConvention, CharSet) As MethodBuilder
[C#] public MethodBuilder DefinePInvokeMethod(string, string, string, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet);
[C++] public: MethodBuilder* DefinePInvokeMethod(String*, String*, String*, MethodAttributes, CallingConventions, Type*, Type[], CallingConvention, CharSet);
[JScript] public function DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) : MethodBuilder;

Example

[Visual Basic, C#, C++] The following code sample demonstrates how to create a PInvoke call to a specific external method signature inside a dynamic type using DefinePInvokeMethod.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of DefinePInvokeMethod. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Dim currentDomain As AppDomain
Dim myAssemblyName As AssemblyName
Dim myMethodBuilder As MethodBuilder = Nothing

' Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain
myAssemblyName = New AssemblyName()
myAssemblyName.Name = "TempAssembly"

' Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = currentDomain.DefineDynamicAssembly _ 
                    (myAssemblyName, AssemblyBuilderAccess.Run)
' Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
Dim myType As Type() = _ 
              {GetType(Integer), GetType(String), GetType(String), GetType(Integer)}

' Define a PInvoke method giving the name of  the entrypoint in the dll.
myMethodBuilder = _ 
          myModuleBuilder.DefinePInvokeMethod("MsgBox", "user32.dll", "MessageBox", _
          MethodAttributes.Public Or MethodAttributes.Static Or _ 
          MethodAttributes.PinvokeImpl, CallingConventions.Standard, Nothing, myType, _ 
          CallingConvention.Winapi, CharSet.Ansi)

myModuleBuilder.CreateGlobalFunctions()

[C#] 
AppDomain currentDomain;
AssemblyName myAssemblyName;
MethodBuilder myMethodBuilder=null;

// Get the current application domain for the current thread.
currentDomain = AppDomain.CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain.DefineDynamicAssembly
               (myAssemblyName, AssemblyBuilderAccess.Run);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
Type[] myType = {typeof(int),typeof(string),typeof(string),typeof(int)};

// Define a PInvoke method giving the name of  the entrypoint in the dll.
myMethodBuilder = myModuleBuilder.DefinePInvokeMethod("MsgBox",
  "user32.dll", "MessageBox",
  MethodAttributes.Public|MethodAttributes.Static|MethodAttributes.PinvokeImpl,
  CallingConventions.Standard,null,myType,CallingConvention.Winapi,
  CharSet.Ansi);

myModuleBuilder.CreateGlobalFunctions();

[C++] 
AppDomain* currentDomain;
AssemblyName* myAssemblyName;
MethodBuilder* myMethodBuilder=0;

// Get the current application domain for the current thread.
currentDomain = AppDomain::CurrentDomain;
myAssemblyName = new AssemblyName();
myAssemblyName->Name = S"TempAssembly";

// Define a dynamic assembly in the 'currentDomain'.
myAssemblyBuilder = 
   currentDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);
// Define a dynamic module in "TempAssembly" assembly.
myModuleBuilder = myAssemblyBuilder->DefineDynamicModule(S"TempModule");
Type* myType[] = {__typeof(int),__typeof(String),__typeof(String),__typeof(int)};

// Define a PInvoke method giving the name of  the entrypoint in the dll.
myMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
   S"MsgBox",
   S"user32.dll",
   S"MessageBox",
   static_cast<MethodAttributes>(MethodAttributes::Public
                                |MethodAttributes::Static
                                |MethodAttributes::PinvokeImpl),
   CallingConventions::Standard,
   0,
   myType,CallingConvention::Winapi,
   CharSet::Ansi);

myModuleBuilder->CreateGlobalFunctions();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

TypeBuilder Class | TypeBuilder Members | System.Reflection.Emit Namespace

Show:
© 2015 Microsoft